1
votes

I'm using devise+omniaouth for authenticanton. My users also have a profile, with the following relationship;

class User < ActiveRecord::Base
 has_many :authentications
 has_one :profile
 accepts_nested_attributes_for :profile
 attr_accessible :email, :password, :password_confirmation, :remember_me, :profile_attributes
etc.

So my registrations/new.html.erb looks like this:

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

<%= f.fields_for :profile do |profile_form| %>
  <h2><%= profile_form.label :name %></h2>
  <p><%= profile_form.text_field :name %></p>
<% end %>

<h2><%= f.label :email %></h2>
<p><%= f.text_field :email %></p>

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

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

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

<br/>
<%= render :partial => "devise/shared/links" %>
<% end %>

<%= render "links" %>

As you can see, the only attribute that I'm trying to fill from the profile is 'name'. This works correctly and when you try to sign up from /users/sign_up, you have to fill name, email and password.

Now, I'm trying to display this registration window from a modal window, so I created: /views/devise/menu/_registration_items.html.erb with the same info:

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

<%= f.fields_for :profile do |profile_form| %>
  <h2><%= profile_form.label :name %></h2>
  <p><%= profile_form.text_field :name %></p>
<% end %>

<h2><%= f.label :email %></h2>
<p><%= f.text_field :email %></p>

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

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

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

<br/>

<% end %>

I also added this to the application controller (it works the same way if I put it in the helper):

 def resource_name
   :user
 end

 def resource
   @resource ||= User.new
 end

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

As explained at https://github.com/plataformatec/devise/wiki/How-To%3a-Display-a-custom-sign_in-form-anywhere-in-your-app

Everything works fine except that the name field doesn't dislpay in the form.(For example, the login form that doesn't have that field name works correctly). I guess that I have to map something else, but I don't know what it is.

Thanks!

1

1 Answers

1
votes

I forgot to add the

<% resource.build_profile %>

line at the top of the form.