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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:54:36+00:00 2026-06-13T01:54:36+00:00

Possible Duplicate: making a pretty url via ruby I have looked around but I

  • 0

Possible Duplicate:
making a pretty url via ruby

I have looked around but I can’t find an answer. I am either not asking the right question and/or I am not looking in the right place.

I have the following defined:

config/routes.rb
resources :members

members_controller.rb
def show
@member = Member.find(params[:id])
end

As of now when showing a member, the URL is http://myapp.tld/members/1. Let’s say the member’s name was cooldude. How hard would it be to show http://myapp.tld/members/cooldude or even http://myapp.tld/cooldude if possible?

Thanks for your help.

  • 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-13T01:54:38+00:00Added an answer on June 13, 2026 at 1:54 am

    I recommend friendly_id but if you want to roll a solution of your own, this should get you started:

    1. Add a slug field to your model
    2. Set the slug field before saving
    3. Override to_param in your model to return the slug instead of id

    Setup your model:

    # member.rb
    before_save do
      self.slug = name.parameterize
    end
    
    def to_param
      slug
    end
    

    Change your finder to:

    # members_controller.rb
    def show
      @member = Member.find_by_name!(params[:id])
    end
    

    Note that find_by_name! (see the bang?) will raise a ActiveRecord::RecordNotFound exception rather than returning nil. In production, this will automatically show a 404 page.

    In your views, you can use member_path(@member) which will now return /members/cool-dude instead of /members/1.

    Check out the docs:

    • http://apidock.com/rails/ActiveRecord/Integration/to_param
    • http://apidock.com/rails/ActiveRecord/Base (section on dynamic attribute-based finders)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: What types of executables can be decompiled? It's pretty popular to decompile
Possible Duplicate: Making an undefined class as friend, and defining it later. I have
Possible Duplicate: Java, GUI builder or hand coding? I have been making GUI hand-coded
Possible Duplicate: Making the iPhone vibrate How can you programmatically make the iPhone vibrate?
Possible Duplicate: how to make “pretty rounding” in R? I have a number, say
Possible Duplicate: Making a generic property I'm not quite sure how to do that,
Possible Duplicate: I am making some parts of java image transparent by some code,
Possible Duplicate: CSS Opacity Property I'm making a layer on an image which has
Possible Duplicate: PHP take all combinations I'm thinking of making something in PHP that
Possible Duplicate: Why use getters and setters? Is there any advantage to making methods

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.