Rails4 Friendly_id Unique Slug Formatting

前端 未结 4 2091
抹茶落季
抹茶落季 2021-02-15 22:15

I am using friendly_id gem for slugging my models. Since the slug has to be unique when i enter the same data to check i get a long hashed appending in the slug.



        
4条回答
  •  没有蜡笔的小新
    2021-02-15 23:06

    I would recommend using the :scoped module if you want to avoid UUIDs in your slugs when dealing with collisions. Here's the documentation along with an example:

    http://norman.github.io/friendly_id/file.Guide.html#Unique_Slugs_by_Scope

    Try using :scope => :id since each id will be unique anyway and see if that works for you.

    UPDATE:

    To get exactly what you want, you now have candidates for that purpose in version 5:

    class YourModel < ActiveRecord::Base
      extend FriendlyId
      friendly_id :slug_candidates, use: :slugged
    
      # Try building a slug based on the following fields in
      # increasing order of specificity.
      def slug_candidates
        [
          :name,
          [:name, :id],
        ]
      end
    end
    

提交回复
热议问题