Here’s the code:
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 %>
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_attributesin the User model so you can mass-assign attributes viaaccepts_nested_attributes_for.http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html