Currently I have a route that looks like this:

resources :posts

I want to override the 'show' action so that I can display a url like this:

posts/:id/:slug

I am currently able to do this by adding a custom match route:

resources :posts
match 'posts/:id/:slug' => 'posts#show'

However, when I use the link_to helper, it does not use my custom show route.

<%= link_to 'show', post %>  # renders /posts/123

How can I define my show route so that I can still use the link_to helper?

Update: As you can read in the following answers, you can override the route to the 'show' action, but it's probably more work than it's worth. It's easier to just create a custom route:

# config/routes.rb
match 'posts/:id/:slug' => 'posts#show', as: 'post_seo'

# app/views/posts/index.html.erb
<%= link_to post.title, post_seo_path(post.id, post.slug) %>
Edit
Report