Pablo's blog

A bit of this, a bit of that and a lot about computers

Show a devise log in form in another page

Devise create various forms, among them one for signing up and one for logging in of course. These are the forms as they are generated in Devise 1.0.8:

<h2>Sign up</h2>

<% form_for resource_name, resource, :url => registration_path(resource_name) do |f| -%>
  <%= f.error_messages %>
  <p><%= f.label :email %></p>
  <p><%= f.text_field :email %></p>

  <p><%= f.label :password %></p>
  <p><%= f.password_field :password %></p>

  <p><%= f.label :password_confirmation %></p>
  <p><%= f.password_field :password_confirmation %></p>

  <p><%= f.submit "Sign up" %></p>
<% end -%>

<%= render :partial => "shared/devise_links" %>


<h2>Sign in</h2>

<% form_for resource_name, resource, :url => session_path(resource_name) do |f| -%>
  <p><%= f.label :email %></p>
  <p><%= f.text_field :email %></p>

  <p><%= f.label :password %></p>
  <p><%= f.password_field :password %></p>

  <% if devise_mapping.rememberable? -%>
    <p><%= f.check_box :remember_me %> <%= f.label :remember_me %></p>
  <% end -%>

  <p><%= f.submit "Sign in" %></p>
<% end -%>

<%= render :partial => "shared/devise_links" %>

If you try to put them somewhere else you’ll run into some problem. There are some variables/methods those forms use that you’ll be lacking, specifically: resource_name, resource and for logging in also devise_mapping. I’ve recently tried to put them both in the homepage for an upcoming project of mine and this is how I’ve solved it:

module ContentHelper
  def resource_name

  def resource
    @resource ||=

  def devise_mapping
    @devise_mapping ||= Devise.mappings[:user]
About these ads

Single Post Navigation

18 thoughts on “Show a devise log in form in another page

  1. Jason on said:

    Thanks, man! Just what I was looking for.

  2. Pingback: Devise login form in another controller |

  3. rnr Tom on said:

    Devise does a great “demo,” but the default behaviors just don’t do the trick for most apps. Thanks for solving this one in an elegant way!

  4. Brian Bokor on said:

    This is awesome. Thank you for posting it. It saved me big time. The only issue I have is when I submit the form, it actually takes me to the actual devise page and not my controller. Looks like the redirects are off for errors. Any ideas how to solve that with your brilliant ways.

  5. The Ultimation on said:

    Good stuff, thanks!

  6. Thank you very much for that! Awesome solution!

  7. Anonymous on said:

     Thanks for this!  I was trying to do the same thing and couldn’t figure out how to get it working.

  8. Apsharkey on said:

    sorry for the n00b question here, but where do I place this module in my rails project?

    • You don’t have to create this module anywhere. You have to find the Helper module for whatever controller you are working in. If you controller is called BlahController then in your project you should have a BlahHelper.

  9. Jase on said:

    Thanks! Worked great for my project.

  10. ankur on said:

    Thanks dude, i was doing it by passing in local variables to the partial, but this is good too. I didn’t know what the devise_mappings was.

  11. Karthikeyan on said:

    Thank you So much!! But the Records are not saved. What to do for it ?

  12. Thank you, this was of great help.

  13. Andrew on said:

    Is there a similar solution to handling form errors when the forms are on other pages? So that the redirect containing error messages doesn’t go to the default form pages?

  14. Ben Polinsky on said:

    Isn’t this incorrect?
    def resource
    @resource ||=

    Doesn’t resource point to the current instance in some cases:
    if devise_mapping.confirmable? && resource.pending_reconfirmation?

    This would end up as:

    Ah, let me know if I have misinterpreted this.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Get every new post delivered to your Inbox.

Join 377 other followers

%d bloggers like this: