21

I want the ability to display the sign_up form on the homepage of my app home#index as well as the default page Devise creates.

Devise has the instruction to do this for the sign_in page but how can I do it with sign_up? https://github.com/plataformatec/devise/wiki/How-To:-Display-a-custom-sign_in-form-anywhere-in-your-app

Thanks!

bcackerman
  • 1,486
  • 2
  • 22
  • 36

2 Answers2

51

Paste this in your home#index view code

<h2>Sign up</h2>

<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
  <%= devise_error_messages! %>

  <div><%= f.label :email %><br />
  <%= f.email_field :email %></div>

  <div><%= f.label :password %><br />
  <%= f.password_field :password %></div>

  <div><%= f.label :password_confirmation %><br />
  <%= f.password_field :password_confirmation %></div>

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

<%= render "links" %>

and

 def resource_name
    :user
  end

  def resource_class 
     User 
  end

  def resource
    @resource ||= User.new
  end

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

in your Application Helper file.
You are good to go.

Oskar Eriksson
  • 845
  • 1
  • 9
  • 28
Jatin Ganhotra
  • 6,825
  • 6
  • 48
  • 71
  • For the last line I had to use `<%= render "devise/shared/links" %>`, otherwise all good! – Nathan Bashaw Dec 23 '12 at 07:58
  • Also add `def resource_class User end` to the `ApplicationHelper` – Koen. Jun 04 '13 at 22:35
  • I recommend manually setting the ids of the form and fields to be syntactically correct when you have identical forms on the same page (ie. when the user is on the sign in page). – d_rail Sep 05 '13 at 22:27
  • If there is server_side validation that fails, how do we know when page to render next? – Jonathan Feb 09 '18 at 15:04
1

Here is the explanation from Devise How To: Display a custom sign_in form anywhere in your app

Another example with form_for and posting to user_session_path:

<%= form_for(:user, :url => session_path(:user)) do |f| %>
  <%= f.text_field :email %>
  <%= f.password_field :password %>
  <%= f.check_box :remember_me %>
  <%= f.label :remember_me %>
  <%= f.submit 'Sign in' %>
  <%= link_to "Forgot your password?", new_password_path(:user) %>
<% end %>

I placed it in my new view

user3719779
  • 129
  • 4