Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8272979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T07:19:00+00:00 2026-06-08T07:19:00+00:00

Currently I have a route that looks like this: resources :posts I want to

  • 0

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) %>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T07:19:01+00:00Added an answer on June 8, 2026 at 7:19 am

    You have two routes which point to posts#show (you should be able to confirm this by running rake routes), and your link is using the wrong one.

    When you call link_to('show', post) the URL of the link is generated by calling url_for(post) which (eventually, after passing through several other methods on the way) calls post_path(post). Since the route to posts#show that was created by your call to resources(:posts) is named post, that is the route that post_path generates.

    You also currently have inconsistent routes for the show, update and destroy actions which will probably cause you problems later on.

    You can fix this by changing your routes to the following:

    resources :posts, :except => ['show', 'update', 'destroy']
    get    'posts/:id/:slug' => 'posts#show', :as => 'post'
    put    'posts/:id/:slug' => 'posts#update'
    delete 'posts/:id/:slug' => 'posts#destroy'
    

    Unfortunately you still can’t use link_to('show', post) just yet, because it relies on being able to use post.to_param as the single argument needed to build a path to a post. Your custom route requires two arguments, an id and a slug. So now your link code will need to look like this:

    link_to 'show', post_path(post.id, post.slug)
    

    You can get around that problem by defining your own post_path and post_url helpers in app/helpers/posts_helper.rb:

    module PostsHelper
      def post_path(post, options={})
        post_url(post, options.merge(:only_path => true))
      end
    
      def post_url(post, options={})
        url_for(options.merge(:controller => 'posts', :action => 'show',
                              :id => post.id, :slug => post.slug))
      end
    end
    

    Which means we’re finally able to use:

    link_to 'show', post
    

    If that all seems like too much work, a common alternative is to use URLs that look more like posts/:id-:slug, in which case you can stick with the standard RESTful routes and just override the to_param method in your Post class:

    def to_param
      "#{id}-#{slug}"
    end
    

    You’ll also need to do a little bit of work splitting up params[:id] into an ID and a slug before you can look up the relevant instance in your show, edit, update and destroy controller actions.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a route that currently looks like this: map.resources :regions, :collection => {:select_for_payroll
I currently have a routes.rb file that looks like this: map.resources :profiles do |profile|
Currently, I have URLs that look like this: http://www.example.com/user/create http://www.example.com/user/edit/1 But now, I have
I have a .htaccess file that looks like this: SetEnv APPLICATION_ENV development RewriteEngine On
Currently have a drop down menu that is activated on a hover (from display:none
I have two tables that look like this Train +----------+-------------+------+-----+---------+-------+ | Field | Type
I currently have: @Html.EditorFor(model => model.PurchasePrice) I would like to split this into 2
In my routes I currently have resources :users and so I get the routes
Currently have password protection on my main and sub directories, however I'd like to
I currently have a XSLT 2.0 Stylesheet that I am trying to remove empty

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.