1

Consider this route setup:

  resources :potatos do
    get "foo", to: "foo#foo"
  end

This generates a show route like this:

/potatos/:id

However the generated foo route is different:

/potatos/:potato_id/foo

I want the foo route to use the :id param, not :potato_id. This is because I have existing (shared) controller logic which expects an :id param

From the advice of Naming params of nested routes I tried resources(:potatos, param: :id) but it had the same result.

Community
  • 1
  • 1
max pleaner
  • 26,189
  • 9
  • 66
  • 118
  • 3
    Unrelated, but that's not the plural of "potato". – Dave Newton Dec 01 '16 at 22:03
  • Why not pull the get route out of the nested resource like this `get '/potatos/:id/foo', to: 'foo#foo'` and `resources :potatos` – user3366016 Dec 01 '16 at 22:31
  • @user3366016 that's what I did as a workaround, but I'm fishing to see if there's another way – max pleaner Dec 01 '16 at 22:33
  • @maxpleaner makes sense. I think this should work to keep it nested and a bit cleaner. I tend to pull this type of stuff out of the nested routes though. `resources :potatos do get "foo", to: "foo#foo", on: :member end` – user3366016 Dec 01 '16 at 22:59
  • @user3366016 getting `undefined local variable or method 'on' for # – max pleaner Dec 01 '16 at 23:12
  • I swear i tested it but with a slightly different format `resources :potatos do member do get "foo", to: "foo#foo" end end` – user3366016 Dec 01 '16 at 23:15

1 Answers1

1

Since you're just looking for various ways to achieve this. I thought I'd start an answer based on the comments to show the various ways I've tried that work for me.

The desired format is:

"/potatos/:id/foo" # :id refers to potato id

Outside of the resources block

If you do not need a nested route in your routes.rb you can achieve This can be achieved by creating a custom route outside of the resources block.

# routes.rb
# Custom routes    
get '/potatos/:id/foo', to: 'foo#foo'
# Resources
resources :potatos

Rake routes shows:

GET potatos/:id/foo(.:format) foo#foo

I personally like this format as it allows me to see whats a typical resource and what I've added on. That said, I know the OP needs to follow a predefined style guide and needs it in the resources block (based on comment).

Inside of the resources block

If you need this done inside of the resources block, so far I've found adding a member route to a different controller#action to work for me in a simple app.

  resources :potatos do
    member do
      get "foo", to: "foo#foo"
    end
  end

Or a simplified version if you only have one member.

  resources :potatos do
      get "foo", to: "foo#foo", on: :member
  end

both show the following when I rake routes:

foo_potatos GET /potatos/:id/foo(.:format) foo#foo

However, the OP recieves an error when trying this format, but in my testing it works fine.

user3366016
  • 1,267
  • 2
  • 18
  • 31
  • I didn't realize that `member` was some kind of special variable. I actually tried `on: :id` which didn't work. thanks, will try this today – max pleaner Dec 02 '16 at 16:13