Erin Walker
Erin Walker

Reputation: 739

How can I make my rails app display url's as the user name?

In my Rails app a user can create one registry and the url's is for example:

http://localhost:3000/registries/3

How can I make that url be for example:

http://localhost:3000/erinwalker

Thanks in advance.

Upvotes: 4

Views: 1829

Answers (2)

Nick Colgan
Nick Colgan

Reputation: 5508

The simplest way of doing this is by creating a new route at the bottom of your routes.rb file:

match "/:username" => "registries#show"

It's important to put this at the bottom of your routes.rb file as this will match any route of the format "/whatever" that doesn't match the routes preceding it.

This will point to the show action of your registries controller. So within that action you can just do

@user = User.find_by_username params[:username]
@registry = @user.registry

Upvotes: 4

Dave Newton
Dave Newton

Reputation: 160170

The friendly_id gem and other slugging gems is, IMO, the easiest approach.

(And here's a friendly_id railscast.)

That said, it's easy to create a route that accepts legal slug values and redirect or look it up yourself.

Upvotes: 1

Related Questions