Are nested resources nescessary in this case?

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?

betaband wrote:

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? >

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

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.

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)