Eike Cochu
Eike Cochu

Reputation: 3429

Rails 5 rename single resource route name

I am trying to create a separate :show route, to use route globbing on the :id parameter. For this, I created a resource route without the show route and also a separate show route:

resource :test, except: [:show]
get 'test/*id', to: 'test#show', as: :test

the problem is that I receive the error: You may have defined two routes with the same name using the:asoption, or you may be overriding a route already defined by a resource with the same naming.

If I remove as: :test it works. rails routes shows:

    tests POST   /tests(.:format)
 new_test GET    /tests/new(.:format)
edit_test GET    /tests/:id/edit(.:format)
     test PATCH  /tests/:id(.:format)     <-- WHY??
          DELETE /tests/:id(.:format)
          GET    /tests/*id(.:format)

as you can see, resources renamed the PATCH route to :test. If I remove that route, the DELETE route is named test, and so on. How can I stop resources from using the test route name specifically? I cannot move my globbing route above the resource block obviously because then all other routes are globbed too.

What I want:

    tests POST   /tests(.:format)
 new_test GET    /tests/new(.:format)
edit_test GET    /tests/:id/edit(.:format)
          PATCH  /tests/:id(.:format)
          DELETE /tests/:id(.:format)
     test GET    /tests/*id(.:format)

Upvotes: 0

Views: 1533

Answers (3)

Shefalee Chaudhary
Shefalee Chaudhary

Reputation: 602

First of all,

 test PATCH  /tests/:id(.:format)     <-- WHY??
      DELETE /tests/:id(.:format)
      GET    /tests/*id(.:format)
  1. Patch is for your Update method routes.
  2. Delete is for destroy method route.
  3. that add by your custom route generated by get 'test/*id', to: 'test#show', as: :test

So, here you can make your show route with different alias. Ex like using as :show_test

Upvotes: 0

Rooby Doo
Rooby Doo

Reputation: 176

Rails uses the same prefix (say, in your case "test") for all those four routes[show(GET), update(PUT/PATCH), destroy(DELETE)] and it recognises the different routes with the HTTP Verbs.

Upvotes: 1

piton4eg
piton4eg

Reputation: 1106

I don't understand your problem, but if you look on Rails Guides "Singular Resources" you can see:

Path      Controller      #Action           Used for
GET       /geocoder/new   geocoders#new     return an HTML form for creating the geocoder
POST      /geocoder       geocoders#create  create the new geocoder
GET       /geocoder       geocoders#show    display the one and only geocoder resource
GET       /geocoder/edit  geocoders#edit    return an HTML form for editing the geocoder
PATCH/PUT /geocoder       geocoders#update  update the one and only geocoder resource
DELETE    /geocoder       geocoders#destroy delete the geocoder resource

show, create, update and destroy use the same route but with different HTTP verbs. And in your case, test wrote with PATCH verb, because this verb earlier in the table, empty name means that it uses the same name as upper line.

Upvotes: 0

Related Questions