[Rails] Editing an object with associations
David Heinemeier Hansson
david at loudthinking.com
Thu Mar 17 11:24:59 GMT 2005
>> So to restate your problem. An employee has many phone numbers, phone
>> numbers belong to a PhoneNumberType. You want to edit the employee &
>> their phone numbers & their types in one page?
I think something like this would work:
View:
<% @employee.phone_numbers.each do |@phone_number| %>
<%= collection_select "phone_number[]", "phone_number_type_id",
PhoneNumberType.find_all, :id, :name %>
<%= text_field "phone_number[]", "number" %>
<% end %>
Controller:
@employee.phone_numbers.each_with_index do |phone_number, idx|
phone_number.update_attributes(@params[:phone_number][idx])
end
P.S.: The Ruby naming convention for variables is underscored, not
camel case.
--
David Heinemeier Hansson,
http://www.basecamphq.com/ -- Web-based Project Management
http://www.rubyonrails.org/ -- Web-application framework for Ruby
http://www.loudthinking.com/ -- Broadcasting Brain
More information about the Rails
mailing list