Chiggins Chiggins - 2 years ago 64
Ruby Question

Rails Route based on something different than ID

So currently I have something like /users/1/ when I want to view a user profile. Dunno how easy or common of a question this is, I'm a bit of a Rails noob at this point, but how can I go through routes.rb to change that to /user/chiggins/ where chiggins is a unique username?

Thanks much!

Answer Source

You need is to override to_param method in User model:

class User
  def to_param

Then rails will use it automagically for routing. See

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download