Alex Rivera | Logout

Changing the id parameter in Rails routing

Asked 2010-05-14T19:44:05.330
44

Using Ruby on Rails 3's new routing system, is it possible to change the default :id parameter

resources :users, :key => :username

come out with the following routes

/users/new
/users/:username
/users/:username/edit
...etc

I'm asking because although the above example is simple, it would be really helpful to do in a current project I'm working on.

Is it possible to change this parameter, and if not, is there a particular reason as to why not?

Edit
Report

1 Answer

0

Pass the user name as the input to the path helpers.

In your view code:

user_path(u.username) #/users/john

In your controller treat the id received as username:

def show
  user = User.find_by_username!(params[:id])
  ...
end
answered 2010-05-14T23:27:22.367

Your Answer