You can define the to_param
method in the user model.
class User ... def to_param name end ... end
Then each generated URL will have name
instead of id
as the user identifier.
sid = User.new :name => 'sid' user_path(sid) #=> "/users/sid"
Of course, in the controller you must find the user by name.
class UsersController ... def show @user = User.find_by_name(params[:id]) end ... end
I also suggest you take a look at friendly_id gem.
FriendlyId - "Swiss Army bulldozer" traffic jams and permalink plugins for ActiveRecord. This allows you to create beautiful URLs and work with user-friendly strings, as if they were numeric identifiers for the ActiveRecord Model.
source share