【发布时间】: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