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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T20:03:36+00:00 2026-06-07T20:03:36+00:00

I am trying to replace user profile views of the sort /users/1 with /username

  • 0

I am trying to replace user profile views of the sort

/users/1

with

/username

I’m aware this means I’ll need to check for collisions of various kinds. I’ve consulted the following similar SO questions:

  • Ruby on rails routing matching username
  • customize rails url with username
  • Routing in Rails making the Username an URL:
  • routing error with :username in url

Here are the various failed routes.rb route definitions I’ve tried, and associated errors:

  1. match "/:username" => "users#show", via: "get"

    Here’s the error:

    ActiveRecord::RecordNotFound in UsersController#show
    
    Couldn't find User without an ID
    
    app/controllers/users_controller.rb:7:in `show'
    

    Here is my corresponding users_controller:

    6 def show
    7   @user = User.find(params[:id])
    8 end
    
  2. match "/:username" => 'users#show', :as => :profile

    Same error as above.

  3. match "/:username", :controller => "users/:id", :action => 'show'

     Routing Error
    
     uninitialized constant Users
    
     Try running rake routes for more information on available routes.
    
  4. match '/:username', :controller => 'users', :action => 'show'

    Same error as 1.

  5. match '/:username', to: 'users/:id', via: 'show'

    Server does not start.

  6. match "/:username" => redirect("/users/:id")

    Error:

     ActiveRecord::RecordNotFound in UsersController#show
    
     Couldn't find User with id=:id
    

Any idea why my routing is not working the same way that everyone else who asks this question’s is?

Update

Just to take this issue out of the comments and put it in the question more cleanly. After making the change by @Ryan Bigg below, I had a routing problem in my redirect to profile when a new one is created. Here’s my create code:

  def create
    @user = User.new(params[:user])
    if @user.save
        session[:user_id] = @user.id
        flash[:success] = "Thank you for signing up."
        redirect_to ('/'+@user.username)
        #redirect_to @user, notice: "Thank you for signing up!"
    else
        render "new"
    end
  end

And here is my user.rb

def to_param
    self.username
    #username
end

However, the commented out redirect, which I think should work with the to_param update, doesn’t work, while the ugly hackish one above it does. Why is the to_param overwrite, which worked for other people, not working on my app? My #update and #edit methods are also not working, as their redirects go to “users/1/edit” instead of “username/edit” if overwriting to_param doesn’t take care of this.

  • 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-07T20:03:38+00:00Added an answer on June 7, 2026 at 8:03 pm

    The first one is correct, but isn’t working because you’re still attempting to do something like this inside your controller:

    User.find(params[:username])
    

    When you should instead be doing this:

    User.find_by_username!(params[:username])
    

    The first one will attempt to find by the primary key of your table, where the second one will, correctly, query on the username field instead.

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

Sidebar

Related Questions

I'm trying to replace pieces of XML and need an accumulator along the way.
I'm trying to theme my Drupal site's user profile form at the moment. I'm
im trying to replace text in a text on blur so if the user
I'm trying to create simple ExtJs application that manages Users and User's Roles. Set
I'm trying to search and replace $data['user'] for $data['sessionUser'] . However, no matter what
Trying to add carrier wave based image uploading to my user model. This works
I'm trying to replace all dots found in a value entered by the user
I am trying to create a form for the user to change their profile
Here's a controller I'm trying to replace: @RequestMapping(/user) public @ResponseBody UserRsp callUserService(Principal principal) {
Trying to replace any non alpha-numeric characters with a hyphen. Can't see why it

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.