Change the name of the :id parameter in Routing resources for Rails

前端 未结 5 1438
难免孤独
难免孤独 2020-12-04 10:11

I looked around on how to change the dynamic params slot and found this post that does the exact thing. The post is https://thoughtbot.com/blog/rails-patch-change-the-name-o

5条回答
  •  旧巷少年郎
    2020-12-04 10:27

    There's a gem for that, just like there's a gem for everything ;)

    I've been using FriendlyId for this kind of behaviour in rails 3.

    It will require you to add some code to your model though, like this:

    class Client < ActiveRecord::Base
      has_friendly_id :name
    end
    

    ...and if your clients don't have URI compatible names, you might want to use a slug for that, which you can do with has_friendly_id :name, :use_slug => true. When using slugs you'll obviously need to persist them to the database as well though.

    And as already mentioned, you can still use the to_param trick with rails 3, as documented here. I find FriendlyId a bit more versatile though.

提交回复
热议问题