I'm just starting with Devise and Rails3. I have the Authenetication down and working and understand the basics.
As of now in my Home Controller that represents my Home/Front Page i have two links one that links to Register => sign_up and one that changes depending on login/logout => sign_in/sign_out.
However i dont want to redirect my user to login page. Instead i want to display my login form on my front page. So i have seperated the devise/session/new
template int to a view and a _form.html.erb partial.
And now in my home view i render the login form as so:
<% if user_signed_in? %>
Welcome: <b><%= current_user.username %> (<%= current_user.group.name %>)</b>
<%= link_to "Logout", destroy_user_session_path, :method => 'delete' %>
<% else %>
<%= render 'devise/sessions/form' %> // Rendering the partial here
<%= link_to "Login", new_user_session_path %>
<% end %>
<hr>
This is the code of the partial:
<%= form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f| %>
<div><%= f.label :email %><br />
<%= f.email_field :email, :autofocus => true %></div>
<div><%= f.label :password %><br />
<%= f.password_field :password %></div>
<% if devise_mapping.rememberable? -%>
<div><%= f.check_box :remember_me %> <%= f.label :remember_me %></div>
<% end -%>
<div><%= f.submit "Sign in" %></div>
<% end %>
My error is as follows:
NameError in Home#index
Showing devise/sessions/_form.html.erb where line #1 raised:
undefined local variable or method `resource' for #<#<Class:0x614ec68>:0x6157ff8>
Extracted source (around line #1):
1: <%= form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f| %>
2: <div><%= f.label :email %><br />
3: <%= f.email_field :email, :autofocus => true %></div>
4:
I understand why the error happens. It's because the partial it not being given the resource attribute. I used dump(resource)
and it turns out it's a User model/object So i added the following before it:
<% if @user %>
<% resource = @user %>
<% end %>
And added following to my Home controller index method:
def index
@user = User.new
end
However after that i get resource_name error that turns out to simply be :user
and if i set that then i get a problem because of devise_mapping.rememberable?
which i dont know how to provide.
I want to do this properly so i want to avoid messy and unlogical approach that i started.