ruby on rails - Hide user id in the url bar -
in rails application have load of users each have registered user id.
if go in users index , click on users show page following example header.
localhost:3000/users/3
now don't allows people skip through users in header.
how go doing following shows user.username field instead e.g.
localhost:3000/users/adamwest
you can define to_param
method on user model.
class user ... def to_param name end ... end
then every generated urls have name
instead of id
user identifier.
sid = user.new :name => 'sid' user_path(sid) #=> "/users/sid"
of course, in controller, have find user name.
class userscontroller ... def show @user = user.find_by_name(params[:id]) end ... end
i suggest take @ friendly_id gem.
friendlyid “swiss army bulldozer” of slugging , permalink plugins activerecord. allows create pretty url’s , work human-friendly strings if numeric ids activerecord models.
Comments
Post a Comment