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

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -