Reputation: 2562
My intent is to be able to edit a wine product and its associated awards/reviews in one form. So going to the page "http://mydomain.com/wines/cab-sauv/edit" shows the form in which I can edit the name, year, etc. It will also generate the list of awards and reviews so that I can add/remove/edit each award and review. Saving the wine will automatically save/update/delete the awards and reviews.
I'm having trouble generating the form for the nested objects (awards and reviews). The following is a modified example of what I'm currently attempting to do:
<% form_for @wine do |w| %>
Stock: <%= w.text_field :stock, :size => 12 %>
<% @wine.awards.each do |awd| %>
Award: <%= w.select ???, @medals %>
<% end %>
<% end %>
I'm not really getting nested resources when it comes to forms. Is there something I have to do in the routes as well?
Upvotes: 0
Views: 341
Reputation: 38062
I'd recommend you take a look at the Railscast for nested model forms:
Upvotes: 0
Reputation: 2281
Try this instead
<% form_for @wine do |w| %>
Stock: <%= w.text_field :stock, :size => 12 %>
<% w.fields_for :awards do |awd| %>
Award: <%= awd.select :awd_field_name, @medals %>
<% end %>
<% end %>
In the file app/models/wine.rb, make sure you call this line somewhere,
accepts_nested_attributes_for :awards
You can read this excellent tutorial for nested resources too. http://ryandaigle.com/articles/2009/2/1/what-s-new-in-edge-rails-nested-attributes. Good luck!
Upvotes: 2