Ask A Question


You’re not receiving notifications from this thread. style URLs for username

Nino Rosella asked in General

Hi everyone,

I'm implementing a username system in my app, and I'm trying to make the URLs pretty. I'd rather not have anything like:

but rather

If I want the pretty version I'm going to have to whitelist a whole load of paths, which I'd rather not do. I noticed that and do the following to solve this:

How do I go about creating the same in a Rails 6 app? Pretty sure I have to namespace the routes, but unsure how to implement it exactly.



I haven't tested it, but in your routes try:
get "/:username" => "user#show"

In your user controller:

  def show
    @user = User.where(username: params[:username])
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 79,047+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more.

    © 2023 GoRails, LLC. All rights reserved.