I'm working with a nested model form that appears to be working from the browser experience. When I submit the form, however, only one subclass gets updated properly.
Here are the models:
User:
class User < ActiveRecord::Base
has_many :ducks
has_many :places
accepts_nested_attributes_for :ducks, :places
...
end
Duck:
class Duck < ActiveRecord::Base
belongs_to :user
attr_accessible :name, :selected
...
end
Place:
class Place < ActiveRecord::开发者_运维技巧Base
belongs_to :user
attr_accessible :name, :address
end
And the form:
<%= form_for(@user) do |user_form| %>
<div class="field">
<%= user_form.label :first_name %><br />
<%= user_form.text_field :first_name %>
</div>
.
.
.
<% user_form.fields_for :ducks do |duck_form| %>
<%= duck_form.label duck_form.object.name %>
<% unless duck_form.object.new_record? %>
<%= duck_form.check_box 'selected?' %>
<%= duck_form.label 'selected?', 'Enabled' %>
<br />
<% end %>
<% end %>
<% user_form.fields_for :places do |place_form| %>
<%= place_form.label place_form.object.name %>
<%= place_form.text_field :address %>
<br />
<% end %>
<div class="actions">
<%= user_form.submit "Update" %>
</div>
<% end %>
When I submit the form, the user data (parent class) is updated, and the ducks are properly selected/deselected (child class 1) but the places (child class 2) retain their prior value.
Why are the Place fields not updating?
Edit: Updated question to reflect migration from alias
to place
, thanks to rdvdijk's comment. Unfortunately this didn't fix the problem.
Edit 2: Here's the relevant bit from my log file.
Started POST "/users/1" for 127.0.0.1 at 2011-09-29 14:57:27 -0700
Processing by UsersController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"CILQ3U47rQtR9kJZ6ToAfJ7fgwqCRnrMALDZxELmFQg=", "user"=>{"first_name"=>"Foo", "last_name"=>"Man", "email"=>"foo@man.co", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "ducks_attributes"=>{"0"=>{"selected"=>"1", "id"=>"1"}, "1"=>{"selected"=>"1", "id"=>"2"}}, "places_attributes"=>{"0"=>{"address"=>"werwerwerwe", "id"=>"1"}, "1"=>{"address"=>"oiwneroinwer", "id"=>"2"}}}, "commit"=>"Update", "id"=>"1"}
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
WARNING: Can't mass-assign protected attributes: places_attributes
Clearly the last line is the problem. But even extending attr_accessible
to include all of Place's fields (:name, :address, :id, :user_id, :created_at, :updated_at
) doesn't solve the problem.
alias
is a reserved keyword in Ruby. Rename your model and try again.
It turns out my places
wasn't listed as attr-accessible
, and unfortunately I hadn't included these lines in my original question:
attr_accessor :password
attr_accessible :first_name, :last_name, :email, :password, :password_confirmation,
:ducks_attributes
Adding :places_attributes fixed the problem:
attr_accessor :password
attr_accessible :first_name, :last_name, :email, :password, :password_confirmation,
:ducks_attributes, :places_attributes
Hope this helps someone avoid the same issue; my apologies for missing that bit in my original question.
精彩评论