San Backups
San Backups

Reputation: 503

Ruby on Rails - Hide user_id in URL

I would like to hide the user_id in the URL if I can.

http://domain.com:3000/users/1

Here is the page info.


action: show controller: users id: '1'

Upvotes: 2

Views: 4801

Answers (3)

Gabe M
Gabe M

Reputation: 545

There is a new Railscast that uses the friendly_id, a great gem that provides URL renaming: http://railscasts.com/episodes/314-pretty-urls-with-friendlyid

Upvotes: 1

CrazyCoderMonkey
CrazyCoderMonkey

Reputation: 433

To do this. In your user model make sure you have:

is_sluggable :whatever-attribute-you-want-to-mask-users/1

In your user controllers use:

@user = User.find_using_slug(params[:user])

Upvotes: 0

ronalchn
ronalchn

Reputation: 12335

The id in the URL is required so that the controller/action knows which user it should display on the page. It shows the user with an id of 1 in this case, but in other cases, you might want to show the details of another user.

It is possible to substitute the id in the URL with other identifying information, for example username.

To do this, see http://railscasts.com/episodes/63-model-name-in-url. You simply have to override the to_param method in your model.

Upvotes: 2

Related Questions