Devise plus Simple Form - accepts_nested_attributes_for

482 views
Skip to first unread message

Bharat

unread,
Nov 27, 2010, 10:13:34 PM11/27/10
to Devise
I am trying to keep the authentication information generated by Devise
separate from other user related information in "users" and "profiles"
models as follows:

class User < ActiveRecord::Base

has_one :profile
accepts_nested_attributes_for :profile
belongs_to :role

# Include default devise modules. Others available are:
# :token_authenticatable, :lockable and :timeoutable
devise :database_authenticatable, :registerable, :confirmable,
:recoverable, :rememberable, :trackable, :validatable

# Setup accessible (or protected) attributes for your model

attr_accessible :email, :password, :password_confirmation, :remember_me, :role_id, :active
end

class Profile < ActiveRecord::Base

belongs_to :user
attr_accessible :first_name, :last_name

end

I generated the views and customized the app/views/devise/
registrations/new.html.erb as shown below:

<h2>Sign up</h2>

<%= simple_form_for(resource, :as => resource_name, :url =>
registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<%= f.input :email %>
<%= f.input :password %>
<%= f.input :password_confirmation, :required => true, :hint =>
"confirmation must match the password" %>

<% fields_for :profile, Profile.new do |pf| %>
<%= pf.text_field :first_name %>
<%= pf.text_field :last_name %>
<% end %>

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

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

However, when I try to bring up the form by typing
http://localhost:3000/users/sign_up
I do not see the nested fields for the Profile model at all, neither
on the browser screen nor in the source code as in view page source.

What do I need to do to get this to work?

Thanks.

Bharat

Bharat

unread,
Nov 28, 2010, 6:41:14 PM11/28/10
to Devise
So I have been able to make this work, but not using Siimple Form.
Here is my solution for the view:

<h2>Sign up</h2>

<% @user.build_profile %>

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

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

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

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

<%= f.fields_for :profile, @user.profile do |pf| %>
<p><%= pf.label :first_name %><br />
<%= pf.text_field :first_name %></p>

<p><%= pf.label :last_name %><br />
<%= pf.text_field :last_name %></p>
<% end %>

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

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

This works fine. I believe that the Simple_form for wrapper does not
support has_one accepts_nested_attributes_for association. I would
like to stay with Simple forms in all my forms so If anyone has a
solution, please let me know.

At first, I thought this was a Devise issue so I posted here, but it
now seems like a Simple Form issue.

In any case, both have been developed by the same core group of
developers so I do not see a need for cross-posting?
Thanks.
Bharat

Bharat

unread,
Nov 28, 2010, 6:44:59 PM11/28/10
to Devise
Forgot to mention one more thing:
attr_accessible should include profile_attributes in the class User as
shown below:

class User < ActiveRecord::Base
...
attr_accessible profile_attributes (along with other attributes)
...
end

Carlos Antonio da Silva

unread,
Nov 28, 2010, 8:01:39 PM11/28/10
to plataforma...@googlegroups.com
Hey, this is probably not a SimpleForm issue. I can see the first fields for didn't use <%= in your example, which should not print the  content. Another issue might be that the first example using simple form does not use f.fields_for, it calls fields_for directly.

Please try to convert your working example with form for to simple form and try again to see if everything goes fine =).
--
At.
Carlos A. da Silva

Bharat

unread,
Nov 28, 2010, 9:44:10 PM11/28/10
to Devise
Carlos,
My apologies man. I just have not been thinking right today. Here is
the Simple Form based code which works fine:

<h2>Sign up</h2>

<% @user.build_profile %>

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

<%= f.input :email %>
<%= f.input :password %>
<%= f.input :password_confirmation, :required => true, :hint =>
'confirmation must match the password' %>

<%= f.simple_fields_for :profile, @user.profile do |pf| %>
<%= pf.input :first_name %>
<%= pf.input :last_name %>
<% end %>

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

<% end %>

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

Thanks.
Bharat

Carlos Antonio da Silva

unread,
Nov 28, 2010, 10:02:16 PM11/28/10
to plataforma...@googlegroups.com
No problem, good to know it's working fine =).
Reply all
Reply to author
Forward
0 new messages