[英]accepts_nested_attributes_for when using class_name on association
Here's the code: 这是代码:
User.rb 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 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 %>
Now, when I submit the form, this is what gets sent: 现在,当我提交表单时,将发送以下内容:
{"user"=>{"first_name"=>"Name", "as_client_attributes"=> "website"=>"http://website.com"}}, "commit"=>"Update User"}
And I get this error: 我得到这个错误:
Can't mass-assign protected attributes: as_client_attributes
How can I make this work? 我该如何进行这项工作? Thanks! 谢谢!
You need to add attr_accessible :as_client_attributes
in the User model so you can mass-assign attributes via accepts_nested_attributes_for
. 您需要在用户模型中添加attr_accessible :as_client_attributes
,以便可以通过accepts_nested_attributes_for
批量分配属性。
http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.