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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T01:21:00+00:00 2026-05-15T01:21:00+00:00

In some book, it is recommended that to_param is changed to class Story <

  • 0

In some book, it is recommended that to_param is changed to

class Story < ActiveRecord::Base

  def to_param
    "#{id}-#{name.gsub(/\W/, '-').downcase}"
  end

end

so that the URL is

http://www.mysite.com/stories/1-css-technique-blog

instead of

http://www.mysite.com/stories/1

so that the URL is more search engine friendly.

So probably to_param() doesn’t need to be used by other parts of Rails that changing it may have any side effect? Or maybe the only purpose is to construct a URL for linking?

Another thing is, won’t it require to limit the URL size to be less than 2k in length — will it choke IE if it is more than 2k or maybe the part more than 2k is just ignored by IE and so the URL still works. It might be better to be limited to 30 or 40 characters or something that will make the URL not exceedingly long.

Also, the ri doc of to_param:

       class User < ActiveRecord::Base
         def to_param  # overridden
           name
         end
       end

if to_param is changed like that, then the link actually won’t work, as

http://www.mysite.com/stories/1-css-technique-blog

will work, but

http://www.mysite.com/stories/css-technique-blog

will not work as the ID is missing. Are there other ways to change the to_param method?

Update: on second thought, maybe

http://www.mysite.com/stories/css-technique-blog

won’t work well if there are many webpages with similar title. but then

http://www.mysite.com/user/johnchan

will work. Will it be params[:id] being “johnchan”? So then we will use

user = User.find_by_login_name(params[:id])

to get the user. So it just depends on how we use the param on the URL.

C:\ror>ri ActiveRecord::Base#to_param
-------------------------------------------- ActiveRecord::Base#to_param
     to_param()
------------------------------------------------------------------------
     Returns a String, which Action Pack uses for constructing an URL to
     this object. The default implementation returns this record's id as
     a String, or nil if this record's unsaved.

     For example, suppose that you have a User model, and that you have
     a +map.resources :users+ route. Normally, +user_path+ will
     construct a path with the user object's 'id' in it:

       user = User.find_by_name('Phusion')
       user_path(user)  # => "/users/1"

     You can override +to_param+ in your model to make +user_path+
     construct a path using the user's name instead of the user's id:

       class User < ActiveRecord::Base
         def to_param  # overridden
           name
         end
       end

       user = User.find_by_name('Phusion')
       user_path(user)  # => "/users/Phusion"
  • 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-15T01:21:00+00:00Added an answer on May 15, 2026 at 1:21 am

    If you want to make your url more search engine friendly, you can use the friendly_id gem which makes exactly what you want. Is the easier way I’ve found to generate search engine friendly permalinks.

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

Sidebar

Related Questions

Is there any book or links that I can read some tutorial about the
I am doing some exercises in my object-oriented javascript book, I notice that this:
can you recommend some good ASP.NET tutorials or a good book? Should I jump
I've got a file with some book data in MARC format, of which some
I remember reading in some Java book about any operator other than 'instanceof' for
I just recently started diving into Real World Haskell and the book provides some
some of our customers have Quick book online and some of our customers have
I am working through some examples in a WCF book. There is a Host
while doing some homework in my very strange C++ book, which I've been told
I'm currently working through some exercises in a c++ book, which uses text based

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.