以下是代码:
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
我该如何使这项工作?谢谢!
答案 0 :(得分:4)
您需要在用户模型中添加attr_accessible :as_client_attributes
,以便通过accepts_nested_attributes_for
批量分配属性。
http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html