Rails3 Routes - Passing parameter to a member route
Asked Answered
S

2

28

I would like to pass an extra parameter to a member route of a resource

something like:

resources :events do
  member do
    get 'register/:participant_type_id'
  end
end

I could only accomplish it using a static match statement

Looking around the internet I saw that this might be possible in Rails 3.0.2. I'm using 3.0.1 and it certanlly is not.

Am I doing something wrong? or is it really not possible?

thanks

Schroeder answered 24/11, 2010 at 20:5 Comment(2)
This is supposed to be possible with 3.0.1, are you getting an error?Flaxman
found this, not sure if its related ... rails.lighthouseapp.com/projects/8994/tickets/…Schroeder
S
41

Try this:

resources :events do
  member do
    get 'register/:participant_type_id', :action => 'register'
  end
end
Simony answered 24/11, 2010 at 20:34 Comment(1)
I don't know, I just supply whatever my app tells me it's missing. It said 'missing action', I give them action.Fur
S
18

Just to complete the answer with my little findings. It also confused me for quite a while.

In Rails3, the member route with parameters will not have the automatic generated xx_yy_path helper. You need to add it providing the :as => part, omitted the resources name.

Regarding the example provided, to get register_event_path and register_event_url, you need to define it like the following:

resources :events do
  member do
    get 'register/:participant_type_id', :action => 'register', :as => 'register'
  end
end
Sentry answered 2/4, 2012 at 13:32 Comment(2)
how to get an event id inside of register action?Ludwog
@Ludwog within resources :events and member I believe an id parameter representing event id is automatically in the generated path.Sentry

© 2022 - 2024 — McMap. All rights reserved.