Vasseurth
Vasseurth

Reputation: 6486

Customizing User Show Route in Rails

I want, instead of /user/:id I want the default user route to be /user:created_at I was able to get /user:id to work (without the second /) however when I try to do :created_at I get an error.

Does anyone know how to fix this? Also, even though I have match 'users:id', :to => 'users#show', :as => :user, :via => :get /user/1 still is a valid link since I have resources :users in my config/routes.rb. Is there a way to remove the default /user/:id when rails compiles the resources :users?

Upvotes: 0

Views: 163

Answers (1)

Volker Pacher
Volker Pacher

Reputation: 1877

If I understand it correctly you can either use to_param: http://apidock.com/rails/ActiveRecord/Base/to_param

so if you used in your user model

def to_param
   "#{first_name}_#{last_name}"
end

then user_path(@user) would generate /users/planet_pluto for example

to prevent a route to be generated by map.resources simply use :except

map.resources :user, :except => [:show]

hope that helps

Upvotes: 1

Related Questions