0

This is probably a simple question. I'm reading the form helpers guide on rubyonrails.org ( http://guides.rubyonrails.org/form_helpers.html ). In section 7.3 it shows how to make a form that returns a hash of params like the following:

 {'person' => {'name' => 'Bob', 'address' => {'23' => {'city' => 'Paris'}, '45' => {'city' => 'London'}}}}

Bob has 2 addresses in the hash. So what goes in the controller in order to update both addresses for Bob at the same time?

Thanks.

Update:

Here's what I assume the models would look like, but I still don't know what the controller should look like to update both of Bob's addresses simultaneously.

person.rb

attr_accessible :name, :addresses_attributes 

has_many :addresses
accepts_nested_attributes_for :addresses

address.rb

attr_accessible :city

belongs_to :person

Thank you.

4

1 回答 1

0

按照惯例,我可以建议尝试以下方法:

def update
  @person = Person.find(params[:id])
  if @person.update_attributes(parmas[:person])
    #update successful, now do what you wanna do
  else 
    #update was unsuccessful 
  end
end

试试这个,如果你有任何错误就回来。但这可能不是学习事物的正确方法。您应该遵循入门和后续教程。

于 2012-11-07T19:36:34.327 回答