patrickS patrickS - 3 months ago 10
Ruby Question

Is the order of the routes in the routes.rb important?

My

routes.rb
file looks like:

resources :contents, only: [:show]
get 'contents/by_hardware', to: 'contents#show_by_hardware'


With this setup I am not able to access the
contents/by_hardware
route.

But if I setup my
routes.rb
file in a different order, everthing works.

get 'contents/by_hardware', to: 'contents#show_by_hardware'
resources :contents, only: [:show]


Is the order in the
routes.rb
file important?

Answer

Yes, order matters very much.

It works like this: resources :contents, only: [:show] creates this route

content GET    /contents/:id(.:format)       contents#show

So when you request, for example, http://localhost:3000/contents/by_hardware, it is this route that matches this url. It invokes ContentsController#show action with params {'id' => "by_hardware"}. Your custom action is not considered, because matching route is already found.

Comments