In my current rails app, I have a User model which has_one :user_detail. Currently, I cannot get the form to display the fields for the nested attribute unless I already have it seeded with the seeds.rb file.
Here is my User model
class User < ActiveRecord::Base
has_one :user_detail, :dependent => :destroy
accepts_nested_attributes_for :user_detail
attr_accessible :email, :password, :password_confirmation, :access_level, :first_name, :last_name, :user_detail_attributes
has_secure_password
end
and my UserDetail model
class UserDetail < ActiveRecord::Base
belongs_to :user
end
and my form
<%= form_for [:admin, @user] do |f| %>
<p><%= f.text_field(:name, :placeholder => 'Name')%></p>
<p><%= f.label(:email) %></p>
<p><%= f.email_field(:email, :placeholder => 'Your Email')%></p>
<p><%= f.password_field(:password, :placeholder => 'Choose a password') %></p>
<p><%= f.password_field(:password_confirmation, :placeholder => 'Confirm Password') %></p>
<%= f.fields_for :user_detail do |u| %>
<p><%= u.text_field(:country, :placeholder => 'Country') %></p>
<p><%= u.text_field(:state, :placeholder => 'State') %></p>
<p><%= u.text_field(:city, :placeholder => 'City') %></p>
<p><%= u.text_field(:phone, :placeholder => 'Phone') %> </p>
<% end %>
<p><%= submit_tag("Add Owner") %></p>
The fields won’t show up, until I seed them with this
seeds.rb
UserDetail.delete_all
owner1detail = UserDetail.create(:zip => "84770",
:country => "US",
:state => "Ut",
:city => "St. George",
:user_id => owner1.id
)
and I run rake db:seed. Once I do this and I edit an existing user, the fields show up, all filled in. However, when creating a user they don’t show up. Any ideas why?
I moved the build to my controller like so and it worked