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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:28:29+00:00 2026-06-11T15:28:29+00:00

Scenario: I would like to have a url path where you could look someone

  • 0

Scenario:
I would like to have a url path where you could look someone up by an :id or :name:.
For user/5 or /user/tom would all point to the same user

routes.rb

controller 'user' do  
    get 'user/:id'  
    get 'user/:name'  
end 

test/routes/user_routes.rb

test "/user/:id" do  
  assert_routing "/user/5", :controller => "user", :action => "find_by_id", :id=>"5"  
end  

test "/user/:name" do  
  assert_routing "/user/tom", :controller => "user", :action => "find_by_name", :name=>"tom"  
end 

I am not exactly sure if this is the right design decision with URL paths.
Looking for guidance

  • 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-11T15:28:30+00:00Added an answer on June 11, 2026 at 3:28 pm

    I don’t think what you are doing is going to work, because the routes you gave are ambiguous, because rails routes can’t say that url like user/15 has name or id.

    Of course there is a way to do this, by specifying regular expression. Since id will always be a number, we can have regular expression check for it

    controller 'user' do  
        get 'user/:id', :id => /\d+/
        get 'user/:name', :name => /[A-Za-z][\w\s]+/
    end 
    

    The above statements put a constraint of regular expression. In your controller you can check like

    if params[:id]
    # Get by id
    else
    # Get by name
    

    You can also do this by passsing get parameters and handling it in controller in the same way.

    Thanks

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

Sidebar

Related Questions

Scenario is I have a application relative url like ~/path/to/page.aspx?query=string. I need to programatically
I would like to present a scenario where it would be nice to: have
I have an architecture scenario and I would like to discuss to get your
I have this scenario where I would like to redirect my domains using the
I have a performance sensitive scenario where I would like to write UTF-8 to
I have a scenario that looks like this: User A is a registered user.
I have a simple form for this scenario written in razor/webmatrix. I would like
I have a scenario where the models look like create_table :users do |t| t.string
Scenario I would like to save images with alpha transparency as .png and images
I would like to know a way to implement a scenario that I am

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.