0
controller.rb

@user = User.where(:id => params[:id]).take

html.erb

<a href="/home/layout02_03/<%=@interest.id%>">
 something
</a>

click that a tag then url is "/home/layout02_03/3"

3 is user's id,

but I don't want to expose that "id"

how can I do that, I'm using ruby on rails

2 Answers2

2

You need to have any other unique attribute on the User model, e.g. some random GUID value, once you have it, just use it instead of the ID:

<a href="/home/layout02_03/<%= @interest.obscure_uniq_identifier %>">

Or define it to be the default URL representation for the User:

class User

def to_param
  obscure_uniq_identifier
end

You can generate one using:

before_create :generate_guid
def generate_guid
  self.obscure_uniq_identifier = SecureRandom.hex
end
Dmitry Sokurenko
  • 6,042
  • 4
  • 32
  • 53
0

For anyone who is having the same issue, I would also suggest you to use a gem like Visibilize which offers more options on creating exposable friendly ID's.

Just install the gem and call visibilize inside your model.

F.E.A
  • 307
  • 1
  • 12