My routes keep going to the ‘show’ page instead of the page I want



Okay, this one is a pretty simple fix, but it’s a difficult problem to Google and it has tripped me up time and time again.

What do you do when your routes look good, the URL says you’re going to app_url/posts/index (or /new, or /something else), but all you’re seeing is the show page?

Well, if you’re me, you spend a lot of quality time with Google and some rough language until Kerri reminds you of some basics in route listing.

If you’re in this situation, your routes probably look something like this:

get 'posts/:id' => 'posts#show', as: :post
get 'posts/new' => 'posts#new', as: :new_post
post 'posts/create' => 'posts#create', as: :create_post
get 'posts/index' => 'posts#index', as: :posts


The problem is that the route with the id key as part of the path name is listed in order before get ‘posts’. Once it hits get ‘posts/:id’, it converts any route with get ‘posts/foo’ into that post_path, passing foo as the id parameter. Even if foo is a keyword, like new or index, rails interprets it as get ‘posts/:id’. To resolve this issue, just put the line with the variable in the routing at the bottom of that group of routes, like so:

get 'posts/new' => 'posts#new', as: :new_post
post 'posts/create' => 'posts#create', as: :create_post
get 'posts' => 'posts#index', as: :posts
get 'posts/:id' => 'posts#show', as: :post