# Are nested resources nescessary in this case?

**URL:** <https://www.ruby-forum.com/t/are-nested-resources-nescessary-in-this-case/131713>\
**Category:** Rails\
**Created:** [March 12, 2008, 6:15pm UTC](https://www.ruby-forum.com/t/are-nested-resources-nescessary-in-this-case/131713 "2008-03-12T18:15:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![betaband](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9d8465/32.png) [@betaband](https://www.ruby-forum.com/u/betaband)\
**Post date:** [March 12, 2008, 6:15pm UTC](https://www.ruby-forum.com/t/are-nested-resources-nescessary-in-this-case/131713/1 "2008-03-12T18:15:28Z")

</div>

Hi, I previously had a nested resources system with user having many  
events and events having many signups. But i thought it was not needed  
and am now trying a new approach without nested resources.  
The problem is now that when i try to create a new signup, which  
should be related to the id of the event with event\_id in the signups  
table, i get a “Couldn’t find Event without an ID” error, which i  
understand because the url contains just signups/new and the action  
wants to find event\_id in the params.  
In otherwords i was looking for a way to send the event id to the  
signup controllers new action without having nested resources. is it  
possible?

---

<div class="post-metadata">

**Author:** ![betaband](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9d8465/32.png) [@betaband](https://www.ruby-forum.com/u/betaband)\
**Post date:** [March 12, 2008, 7:07pm UTC](https://www.ruby-forum.com/t/are-nested-resources-nescessary-in-this-case/131713/2 "2008-03-12T19:07:03Z")

</div>

betaband wrote:

> possible?
> 
> >

sure.  
new\_signup\_path(:event\_id =\> 1) will generate…  
/signups/new?event\_id=1  
which will get the :event\_id into your params

but… would you prefer something more elegant??? (like what you had)

/events/1/signups/new

–  
[http://www.5valleys.com/](http://www.5valleys.com/)  
[http://www.workingwithrails.com/person/8078](http://www.workingwithrails.com/person/8078)

---

<div class="post-metadata">

**Author:** ![betaband](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9d8465/32.png) [@betaband](https://www.ruby-forum.com/u/betaband)\
**Post date:** [March 12, 2008, 7:57pm UTC](https://www.ruby-forum.com/t/are-nested-resources-nescessary-in-this-case/131713/3 "2008-03-12T19:57:51Z")

</div>

its true, it is more elegant. I just wanted to try to keep the  
resources un-nested to save confusion when linking because its one of  
my first rails apps. But its good anyways to know that its possible  
with both. thanks.

---

<div class="post-metadata">

**Author:** ![betaband](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9d8465/32.png) [@betaband](https://www.ruby-forum.com/u/betaband)\
**Post date:** [March 13, 2008, 5:50pm UTC](https://www.ruby-forum.com/t/are-nested-resources-nescessary-in-this-case/131713/4 "2008-03-13T17:50:37Z")

</div>

You can actually nest resources in different combinations. For  
example, you could have:

map.resources :users do |user|  
user.resources :events  
end

map.resources :events do |event|  
event.resources :signups  
end

map.resources :locations do |location|  
location.resources :events  
end

You’re not limited to listing a ‘resource’ in only one place in the  
routing map, nor are you restricted to its position (ie., once nested,  
always nested).

With the routing above, you can do the following:

SignupsController  
def new  
@event = Event.find(params[:event\_id]) if params[:event\_id]  
@signup = @event ? @event.signups.build : Signup.new  
end

view:  
new\_event\_signup\_path(@event)
