Eike Cochu Eike Cochu - 1 month ago 17
Ruby Question

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
:as
option, 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)

Answer

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.