Update: there’s a new version of this post covering Devise 4.0.0: Show a devise log in or sign up forms 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 %><%= f.label :email %>

<%= f.text_field :email %>

<%= f.label :password %>

<%= f.password_field :password %>

<%= f.label :password_confirmation %>

<%= f.password_field :password_confirmation %>

<%= f.submit "Sign up" %>

<% end -%>

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

and

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

<%= f.text_field :email %>

<%= f.label :password %>

<%= f.password_field :password %>

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

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

<% 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
    :user
  end

  def resource
    @resource ||= User.new
  end

  def devise_mapping
    @devise_mapping ||= Devise.mappings[:user]
  end
end

24 responses to “Show a devise log in form in another page”

  1. Jason Avatar
    Jason

    Thanks, man! Just what I was looking for.

  2. rnr Tom Avatar
    rnr Tom

    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!

  3. Brian Bokor Avatar
    Brian Bokor

    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.

    1. bhavish Avatar
      bhavish

      Hey any1 got a solution for this problem , that is redirecting the errors .. else if say the password is wrong i end up coming back to the original devise page

    2. Terfender Avatar
      Terfender

      same here… I put the code in ApplicationHelper file so I can sign in from anywhere. But when I press Sign in button, it redirects me to the original sign in form of devise…

      Any ideas?

  4. The Ultimation Avatar
    The Ultimation

    Good stuff, thanks!

  5. Brandon Hansen Avatar

    Thank you very much for that! Awesome solution!

  6.  Avatar
    Anonymous

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

  7. Apsharkey Avatar
    Apsharkey

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

    1. J. Pablo Fernández Avatar

      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.

  8. Jase Avatar
    Jase

    Thanks! Worked great for my project.

  9. ankur Avatar
    ankur

    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.

  10. Karthikeyan Avatar
    Karthikeyan

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

  11. paulmwatson Avatar

    Thank you, this was of great help.

  12. Andrew Avatar
    Andrew

    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?

    1. joel Avatar
      joel

      Did you ever find a solution, i am having the same issue

    2. maha Avatar
      maha

      Even i have the same issue… Let me know if u ve found a solution..

    3. Sophie Avatar
      Sophie

      Did you find a solution? I’m having the same issue as well

    4. Marlon José da Veiga (@marlonvsantos) Avatar

      The trick is overrides the custom behaviour of sessions controller and registrations controller.
      The link below is one good start:
      https://stackoverflow.com/questions/6240141/devise-redirect-on-sign-up-failure

  13. Ben Polinsky Avatar
    Ben Polinsky

    Isn’t this incorrect?
    def resource
    @resource ||= User.new
    end

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

    This would end up as:
    User.new.pending_reconfirmation?

    Ah, let me know if I have misinterpreted this.

  14. Milan Avatar
    Milan

    Can this be used to solve problem for registrations edit form ??

  15. Joshua Avatar
    Joshua

    Exactly what I needed. Thanks!

Leave a Reply

You may also like:

If you want to work with me or hire me? Contact me

You can follow me or connect with me:

Or get new content delivered directly to your inbox.

Join 5,043 other subscribers

I wrote a book:

Stack of copies of How to Hire and Manage Remote Teams

How to Hire and Manage Remote Teams, where I distill all the techniques I've been using to build and manage distributed teams for the past 10 years.

I write about:

announcement blogging book book review book reviews books building Sano Business C# Clojure ClojureScript Common Lisp database Debian Esperanto Git ham radio history idea Java Kubuntu Lisp management Non-Fiction OpenID programming Python Radio Society of Great Britain Rails rant re-frame release Ruby Ruby on Rails Sano science science fiction security self-help Star Trek technology Ubuntu web Windows WordPress

I've been writing for a while:

Mastodon

%d bloggers like this: