Search code examples
ruby-on-railsroutesexcept

Rails routing :except question


I'm trying to make a simple blog with a posts controller route to the root url rather than localhost/posts/:id.

I added the following to my routes file...

match '/:id', :to => 'posts#show', :as => 'post'

Which worked well enough. It has since broken my search route.

match 'search/:q', :to => 'posts#query', :as => 'search'

It looks like rails is trying to find a post with the id of my search query. Is there a way of doing something like the following to get the routes file to assume everything after the root url is a post id EXECPT anything starting with 'search'?

match '/:id', :to => 'posts#show', :as => 'post', :except => 'search'

Solution

  • Rails parses your routes from the top down and stops at the first match. I would put your match '/:id'... line below all your other routes.