【问题标题】:Using Rails 5, how can I make FriendlyId append a -"count+1" to duplicate slugs instead of a UUID?使用 Rails 5,如何让 FriendlyId 附加 -"count+1" 来复制 slug 而不是 UUID?
【发布时间】:2018-09-25 09:53:26
【问题描述】:

显然 FriendlyId 已将其以前的默认方法将数字序列附加到重复 slug(这是我想要的)更改为现在使用 UUID:

Previous versions of FriendlyId appended a numeric sequence to make slugs unique, but this was removed to simplify using FriendlyId in concurrent code.

目前我对这个功能不感兴趣,我更希望使用能够产生更清晰 URL 的原始方法。我发现了一个类似的问题,其中someone provided 下面的代码覆盖了friendlyId normalize_friendly_id 方法以获取我所追求的功能,但使用它会导致错误(wrong number of arguments (given 1, expected 0)):

def normalize_friendly_id
  count = self.count "name = #{name}"
  super + "-" + count if name > 0
end

我试图将其“转换”为友好的“候选人”,但我真的不知道自己在做什么,以下内容不起作用。关于如何调整 name_candidate 方法以产生我想要的结果的任何想法?

class Folder < ApplicationRecord
  extend FriendlyId
  friendly_id :name_candidates, use: [ :slugged, :scoped ], scope: :account_id

  has_ancestry

  belongs_to :account
  has_many :notes, dependent: :destroy

  validates :name, presence: true

  # # https://stackoverflow.com/a/25380607/523051
  # # overrride friendlyId to append -number to duplicate folders instead of uuid's
  # def normalize_friendly_id
  #   count = self.count "name = #{name}"
  #   super + "-" + count if name > 0
  # end

  def name_candidates
    append_number = self.count "name = #{name}" if name > 0
    [
      :name,
      :name, append_number
    ]
  end
end

请注意,我正在使用friendlyId 的:scoped 功能,因此检查现有文件夹名称的范围应正确地限定为:account_id

【问题讨论】:

    标签: ruby-on-rails ruby friendly-url slug friendly-id


    【解决方案1】:

    friendly_id 5 现在有一个 slug_candidates,可让您自定义 slug。

    所以要生成一个连续的 slug,你可以这样做:

    friendly_id :slug_candidates, use: :slugged
    
    def slug_candidates
      [:name, :name_and_sequence]
    end
    
    def name_and_sequence
      slug = normalize_friendly_id(name)
      sequence = Model.where("slug like '#{slug}--%'").count + 2
      "#{slug}--#{sequence}"
    end
    

    这将在以下问题中讨论: https://github.com/norman/friendly_id/issues/480

    According to the author, sequential slugs are bad for performance.

    【讨论】:

      【解决方案2】:

      我在另一个线程中遇到了this answer,并且由于该模块已被实现为friendlyId,我所要做的就是将use: :slugged 更改为use: sequentially_slugged 以产生我所追求的功能。

      class Folder < ApplicationRecord
        extend FriendlyId
        friendly_id :name, use: [ :sequentially_slugged, :scoped ], scope: :account_id
      end
      

      【讨论】:

      • friendly_id :name, use: :sequentially_slugged,谢谢!
      【解决方案3】:

      因为 normalize_friendly_id 需要一个参数: http://www.rubydoc.info/github/norman/friendly_id/FriendlyId%2FSlugged%3Anormalize_friendly_id

      调用super时必须将名称作为参数传递

      【讨论】:

      • 谢谢。仍然被困在哪里/我传入什么。如果我添加名称作为参数,如 def normalize_friendly_id(name) 那么我会通过错误的参数数量错误并得到“未定义的方法`count' for #<0x00007f97fc812680>
      猜你喜欢
      • 2018-04-09
      • 2016-09-26
      • 1970-01-01
      • 2014-06-05
      • 1970-01-01
      • 2018-03-11
      • 2013-09-19
      • 2016-04-17
      • 2014-11-19
      相关资源
      最近更新 更多