2

这是代码:

用户.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

我怎样才能使这项工作?谢谢!

4

1 回答 1

4

您需要添加attr_accessible :as_client_attributesUser 模型,以便您可以通过accepts_nested_attributes_for.

http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html

于 2013-01-12T03:08:42.460 回答