# RESTful Rails + Collections

**URL:** <https://www.ruby-forum.com/t/restful-rails-collections/173309>\
**Category:** Rails\
**Created:** [August 6, 2009, 4:21pm UTC](https://www.ruby-forum.com/t/restful-rails-collections/173309 "2009-08-06T16:21:12Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![corywilkerson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/aca169/32.png) [@corywilkerson](https://www.ruby-forum.com/u/corywilkerson)\
**Post date:** [August 6, 2009, 4:21pm UTC](https://www.ruby-forum.com/t/restful-rails-collections/173309/1 "2009-08-06T16:21:12Z")

</div>

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
