7

I have question about param name for nested resources in rails routes For example i have:

resources :controller1, param: :controller_id do
  resources :controller2
end

and i have routes:

controller1/:controller_id/
...
controller1/:controller_controller_id/controller2/...
...

I want single :controller_id for controller1 I know it's looks bad, but How do this? Thanks!

Mikhail
  • 101
  • 1
  • 5
  • I'm in the same situation, how did you solved this issue? – bukk530 Feb 10 '15 at 15:19
  • remove 'param: :controller_id' and you can check route (action) in application controller with use params[:id] or params[:controller_id] for get data – Mikhail Feb 11 '15 at 17:39
  • Facing the same problem – calfzhou Mar 19 '15 at 06:28
  • Possible duplicate of [Change the name of the :id parameter in Routing resources for Rails](http://stackoverflow.com/questions/6592038/change-the-name-of-the-id-parameter-in-routing-resources-for-rails) – Nick Roz May 19 '17 at 16:20

2 Answers2

18

how about this:

resources :controller1, param: :controller_id do
  member do
    resources :controller2
  end
end

will generate

GET    /controller1/:controller_id
GET    /controller1/:controller_id/controller2
GET    /controller1/:controller_id/controller2/:id
...
calfzhou
  • 1,340
  • 10
  • 10
  • 1
    Then you have to fix your nested resource paths helpers, since they will be absolute, `new_controller2_path` instead of `new_controller1_controller2_path` – Nick Roz May 19 '17 at 16:05
-1

Try this and see;

resources :controller1, param: :controller_id, path: "" do
  resources :controller2, path: ""
end
acacia
  • 1,375
  • 1
  • 14
  • 40