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 3795222
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T13:03:02+00:00 2026-05-19T13:03:02+00:00

I have a category model and I’m routing it using the default scaffolding of

  • 0

I have a category model and I’m routing it using the default scaffolding of resources :categories. I’m wondering if there’s a way to change the paths from /category/:id to /category/:name. I added:

match "/categories/:name" => "categories#show"

above the resources line in routes.rb and changed the show action in the controller to do:

@category = Category.find_by_name(params[:name])

it works, but the ‘magic paths’ such as link_to some_category still use the :id format.

Is there a way to do this? If this is a bad idea (due to some possible way in which rails works internally), is there another way to accomplish this? So that /categories/music, for example, and /categories/3 both work?

  • 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-05-19T13:03:03+00:00Added an answer on May 19, 2026 at 1:03 pm

    Rails has a nifty model instance method called to_param, and it’s what the paths use. It defaults to id, but you can override it and produce something like:

    class Category < ActiveRecord::Base
      def to_param
        name
      end
    end
    
    cat = Category.find_by_name('music')
    category_path(cat)  # => "/categories/music"
    

    For more info, check the Rails documentation for to_param.

    EDIT:

    When it comes to category names which aren’t ideal for URLs, you have multiple options. One is, as you say, to gsub whitespaces with hyphens and vice versa when finding the record. However, a safer option would be to create another column on the categories table called name_param (or similar). Then, you can use it instead of the name for, well, all path and URL related business. Use the parameterize inflector to create a URL-safe string. Here’s how I’d do it:

    class Category < ActiveRecord::Base
      after_save :create_name_param
    
      def to_param
        name_param
      end
    
      private
        def create_name_param
          self.name_param = name.parameterize
        end
    end
    
    # Hypothetical
    cat = Category.create(:name => 'My. Kewl. Category!!!')
    category_path(cat)  # => "/categories/my-kewl-category"
    
    # Controller
    @category = Category.find_by_name_param(param[:id]) # <Category id: 123, name: 'My. Kewl. Category!!!'>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have two tables... Category and Pet. Is there a way in LINQ
I have category model, in which I want to load some default data. How
I have two models Category and Entry . There is another model ExtEntry that
I have a products table that contains a FK for a category, the Categories
I have a Category model, and a Project model, which contains a ForeignKey to
I have a two models: class Category(models.Model): pass class Item(models.Model): cat = models.ForeignKey(Category) I
I have a model Category which has a FK reference to itself. How can
Say I have a post and category model, with each post belonging to a
I have a Category, a Subcategory and a Product model. I have: Category has_many
I have this class class Category(models.Model): title = models.CharField(max_length=60) created = models.DateTimeField() def __unicode__(self):

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.