controller - 2 render templates (from devise) on one page (rails 3) -
i've installed devise rails app, can go sign in page or sign page. want them both on welcome page...
so i've made welcome_page_controller.rb following function:
class welcomepagecontroller < applicationcontroller def index render :template => '/devise/sessions/new' render :template => '/devise/registration/new' end end
but when go welcome page error:
nameerror in welcome_page#index showing /users/tboeree/dropbox/rails_projects/rebasev4/app/views/devise/sessions/new.html.erb line #5 raised: undefined local variable or method `resource' #<#<class:0x104931c>:0x102749c> extracted source (around line #5): 2: <% @header_title = "login" %> 3: 4: 5: <%= form_for(resource, :as => resource_name, :url => session_path(resource_name)) |f| %> 6: <p><%= f.label :email %><br /> 7: <%= f.email_field :email %></p> 8:
does knows solution problem? in advance!
does have fact missing resource function? in welcome_page controller? it's somewhere in devise controller...?
regards, thijs
here's how managed did it.
i've put sign form in home#index
my files:
view/home/index.html.erb
<%= render :file => 'registrations/new' %>
helper/home_helper.rb
module homehelper def resource_name :user end def resource @resource = session[:subscription] || user.new end def devise_mapping @devise_mapping ||= devise.mappings[:user] end def devise_error_messages! return "" if resource.errors.empty? messages = resource.errors.full_messages.map { |msg| content_tag(:li, msg) }.join sentence = i18n.t("errors.messages.not_saved", :count => resource.errors.count, :resource => resource_name) html = <<-html <div id="error_explanation"> <h2>#{sentence}</h2> <ul>#{messages}</ul> </div> html html.html_safe end end
you need part because devise works called resource
, should defined can call registration#new
anywhere.
like that, should able register. however, needed display errors on same page. here's added:
layout/home.html.erb (the layout used index view)
<% flash.each |name, msg| %> # new code (allow flash elements arrays) <% if msg.class == array %> <% msg.each |message| %> <%= content_tag :div, message, :id => "flash_#{name}" %> <% end %> <% else %> # old code <%= content_tag :div, msg, :id => "flash_#{name}" %> <% end %> #don't forget end <% end %>
i found code here
and here's created: saved resource object if invalid in session user hasn't fill every field again. guess better solution exists works , it's enough me ;)
controller/registration_controller.rb
def create build_resource if resource.save if resource.active_for_authentication? # delete session created incomplete subscription if exists. if !session[:subscription].nil? session[:subscription] = nil end set_flash_message :notice, :signed_up if is_navigational_format? sign_in(resource_name, resource) respond_with resource, :location => redirect_location(resource_name, resource) else set_flash_message :notice, :inactive_signed_up, :reason => resource.inactive_message.to_s if is_navigational_format? expire_session_data_after_sign_in! respond_with resource, :location => after_inactive_sign_up_path_for(resource) end else clean_up_passwords(resource) # solution displaying devise errors on homepage found on: # https://stackoverflow.com/questions/4101641/rails-devise-handling-devise-error-messages flash[:notice] = flash[:notice].to_a.concat resource.errors.full_messages # store invalid object in session user hasn't fill every fields again. # session deleted if subscription becomes valid. session[:subscription] = resource redirect_to root_path #set path want here end end
i think didn't forget code. feel free use whatever need.
also, can add sign in form in same page (something that:)
<%= form_for("user", :url => user_session_path) |f| %> <%= f.text_field :email %> <%= f.password_field :password %> <%= f.submit 'sign in' %> <%= f.check_box :remember_me %> <%= f.label :remember_me %> <%= link_to "forgot password?", new_password_path('user') %> <% end %>
cheers !
Comments
Post a Comment