I am just getting started with Rails 3 and I do not quite understand how to go about renaming routes.
What I want:
To rename the path to the users#show controller/action pair. So instead of the URL being www.example.com/users/show/1 it would just be www.example.com/1/home
In the future, I’d also like to be able to add additional paths onto the end such as:
www.example.com/1/home/profile/
How my user resources are set up:
resources :users, :except => [:destroy] do
resources :favorites, :only => [:show, :update]
resources :profiles, :only => [:show, :update]
end
What I tried:
match :home, :to => 'users#show'
What happened:
ActiveRecord::RecordNotFound in UsersController#show
Couldn't find User without an ID
What’s in the development.log file:
Started GET "/home" for 127.0.0.1 at 2011-03-10 13:36:15 -0500
Processing by UsersController#show as HTML
[1m[35mUser Load (1.6ms)[0m SELECT "users".* FROM "users" WHERE ("users"."id" = 101) LIMIT 1
Completed in 192ms
ActiveRecord::RecordNotFound (Couldn't find User without an ID):
app/controllers/users_controller.rb:19:in `show'
What’s in the user controller:
def show
@user = User.find(params[:id])
respond_to do |format|
format.html # show.html.haml
end
end
So, apparently it is storing the user id, as shown in the development log as 101 but for whatever reason I am still getting this error?
Any help you can provide is greatly appreciated!
You should provide a segment key in your match:
But with this renaming you will get not RESTful routes.
Another thing is with user profiles. If one user can have only one profile it’s better to declare singular resource routes: