Rails route to username instead of id

后端 未结 4 1600
情书的邮戳
情书的邮戳 2020-12-05 05:07

I am trying to change the rails routes from /users/1 to /username. I currently set this up so it works for the actions of showing and editing. The actual issue is that when

4条回答
  •  孤街浪徒
    2020-12-05 05:37

    In your user model:

    def to_param
      username
    end
    

    The to_param method on ActiveRecord objects uses, by default, just the ID of the object. By putting this code in your model, you're overwriting the ActiveRecord default, so when you link to a User, it will use the username for the parameter instead of id.

提交回复
热议问题