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?
params[:id] is meant to be the string that uniquely identifies a (RESTful) resource within your Rails application. It is found in the URL after the resource's name.
Rails routes are matched in the order they are specified, so if you have a resources :photos above a get 'photos/poll' the show action's route for the resources line will be matched before the get line. To fix this, move the get line above the resources line so that it is matched first.
This is the simple option. When you use namespace , it will prefix the URL path for the specified resources, and try to locate the controller under a module named in the same manner as the namespace.
In your route you can use
resources :user, param: :username
If I understand you correctly, what you want is to have the username
instead of id
in your url, right?
You can do that by overriding the to_param
method in your model. You can get more information here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With