Rails routes with :name instead of :id url parameters

前端 未结 7 1943
渐次进展
渐次进展 2020-12-05 13:05

I have a controller named \'companies\' and rather than the urls for each company being denoted with an :id I\'d like to have the url use their :name such as: url/comp

7条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-05 13:39

    There's actually no magic to implement this, you have to either build it yourself by correctly implementing to_param at your model (not recommended) or using one of the gems available for this like:

    • friendly_id
    • has_permalink

    I use friendly_id and it does the job nicely.

提交回复
热议问题