问题
I have two models: user and profile. I would like to use the same form to input data to both at the same time. I was following railscast 196# about Nested Model Form (revised). The problem is, the first part of the form is being generated just fine. It's the second part (where field_for is used), that does not show in the view. I searched stackoverflow for a solution and it was suggested to use a 'build' action somehow. That, however, did not work, due to an error.
I would really appreciate it if someone could explain how I can make it work. I have been struggling with this issue for a few days now.
user.rb
class User < ActiveRecord::Base
attr_accessible :email, :first_name, :last_name, :orientation, :gender, :password, :password_confirmation, :date_joined, :last_online, :date_of_birth, :location, :profiles_attributes
has_one :profiles
accepts_nested_attributes_for :profiles, allow_destroy: true
has_secure_password
validates_confirmation_of :password
#.....#
end
profile.rb
class Profile < ActiveRecord::Base
attr_accessible :height, :weight
belongs_to :user
end
user/new.html.erb
<%= form_for @user do |f| %>
<% if @user.errors.any? %>
<div class="error_messages">
<h2>Form is invalid</h2>
<ul>
<% for message in @user.errors.full_messages %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.label :gender, "I am a:" %><%= f.select :gender, options_for_select([["Man", "Male"], ["Woman", "Female"]]) %><br />
<%= f.label :orientation, "Sexsual Orientation" %><%= f.select :orientation, options_for_select([["Straight", "Straight"], ["Gay", "Gay"], ["Bi","Bi"]]) %><br />
<%= f.label :first_Name %><br /><%= f.text_field :first_name %><br />
<%= f.label :last_name %><br /><%= f.text_field :last_name %><br />
Date of Birth:<%= f.date_select( :date_of_birth, :start_year => 1920, :prompt => { :day => 'day', :month => 'month', :year => 'year' }) %><br />
<%= f.label :location %><br /><%= f.text_field :location %><br />
<%= f.label :email %><br /><%= f.text_field :email %><br />
<%= f.label :password %><br /><%= f.password_field :password %><br />
<%= f.label :password_confirmation %><br /><%= f.password_field :password_confirmation %><br />
<%= f.fields_for :profiles do |builder| %>
<fieldset>
<%= builder.label :height, "My height is: (cm)" %><%= builder.text_field :height %><br />
<%= builder.label :weight, "My weight is: (kg)" %><%= builder.text_field :weight %>
</fieldset>
<% end %>
<%= f.submit "Next" %>
<% end %>
<%= link_to 'Back', users_path %>
edit: new action in user_controller: (as you can see it's pretty standard)
def new
@user = User.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @user }
end
end
回答1:
I think you need
has_one :profile
in your user model.
Make the form do
fields_for :profile
And in your users_controller do
@user = User.new
@user.build_profile
has_one relationships work a little differently to has_many - see http://guides.rubyonrails.org/association_basics.html#has_one-association-reference
来源:https://stackoverflow.com/questions/12961168/ruby-on-rails-using-accepts-nested-attributes-for-does-not-generate-a-part-of-t