代码:
User.rb
class User < ActiveRecord::Base
# So we can reference the user's Client information with user.as_client
has_one :as_client, :class_name => 'ClientAccount'
accepts_nested_attributes_for :as_client
end
form.html.erb
<%= f.label :first_name %>
<%= f.text_field :first_name %>
<%= f.fields_for :as_client do |c| %>
<%= c.label :website %>
<%= c.text_field :website %>
<%= end %>
现在,当我提交表单时,这是发送的内容:
{"user"=>{"first_name"=>"Name", "as_client_attributes"=> "website"=>"http://website.com"}}, "commit"=>"Update User"}
我得到这个错误:
Can't mass-assign protected attributes: as_client_attributes
我怎样才能使它工作?谢谢!
您需要在User模型中添加attr_accessible :as_client_attributes
,以便您可以通过accepts_nested_attributes_for
批量分配属性。