Folks,
Trying to get my head around this - and I've made it work, but it feels rather hackish.
Let's say I have two resource types: User and Widget. A User has many Widgets, a Widget belongs to a User
In routes.rb I have Widgets mapped as a nested resource:
map.resources :users do |users| users.resources :widgets end
In an ideal world, I'd like to be able to submit many Widgets to users/1/widgets via user_widgets_url, which, for a single resource would look like:
<% form_for([@user, @widget], :url => user_widgets_url) do |f| %> <%= f.text_field :some_widget_field %> ... <% end %>
That would then post to user/1/widgets (invoking the create method) and all would be well with little controller code.
But what if I wanted to submit many distinct widgets in this form submit (effectively creating n number of new Widgets associated to a User)? I realize that RESTful resources are by-and-large created to work with single instances of a resource (outside of index) but what if that 'resource' is a collection.
I've seen the nested_resources examples in documentation but in most cases it sets up something like this in user.rb:
def widgets_attributes=(attributes) # Process the attributes hash end
And then utilizes fields_for in my template to write out the appropriate fields for a widget...
Is that really the convention? Seems like I'd want to post to the widgets controller as that's the 'resource' in question?
So - in summary - what's the current idiom for posting a collection of resources? Post to the controller associated with the resource (widgets_controller) *or* set up widgets_attributes in user.rb?
Too much? Thanks in advance folks! Cory