【问题标题】:friendly_id override SecureRandom uuid / appending sequence of numbers on slug urls Railsfriendly_id 覆盖 SecureRandom uuid / 在 slug url 上附加数字序列 Rails
【发布时间】:2013-09-19 08:42:49
【问题描述】:

我正在尝试绕过默认的 friendly_id 方法,该方法验证特定 slug 上的 uniq 值。简而言之,我想绕过 friendly_id 检查具有相同值的 slug 并且不附加 --1 、 --2 等。我需要能够创建具有相同 slug 文本的friendly_id 记录。

        class Product < ActiveRecord::Base
         extend FriendlyId
         friendly_id :title, use: :slugged # use the title column to create slug column

【问题讨论】:

    标签: ruby-on-rails friendly-id


    【解决方案1】:

    我认为这样做的唯一方法是使用范围来允许每个重复的friendly_id 存在于自己的“范围空间”中。

    https://github.com/norman/friendly_id/blob/master/lib/friendly_id/scoped.rb#L7

    我猜是这样的(假设重复的friendly_ids 属于不同的userss):

    class Product < ActiveRecord::Base
      extend FriendlyId
      friendly_id :tite, :use => [:scoped,:slugged], :scope => :user
    end
    

    【讨论】:

      【解决方案2】:

      感谢您的出色回答,范围解决方案绝对是正确的路径。虽然,只是为了帮助其他人,实际上并不需要对用户对象做一个完整的范围。我只是这样做了:

           friendly_id :title, :use => :scoped, :scope => :id
      

      并将范围放在表中的id上进行记录。由于每个 id 都是唯一的,所以 slug 将是相同的,不会附加 --1 等。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-02-12
        • 2012-06-13
        • 2012-06-26
        • 2013-09-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多