Rails3 Routes - Passing parameter to a member route

吃可爱长大的小学妹 提交于 2019-12-18 02:01:32

问题


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


回答1:


Try this:

resources :events do
  member do
    get 'register/:participant_type_id', :action => 'register'
  end
end



回答2:


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


来源:https://stackoverflow.com/questions/4271023/rails3-routes-passing-parameter-to-a-member-route

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!