显然,FriendlyId 已经更改了之前将数字序列附加到重复 slugs 的默认方法(这就是我想要的),现在使用 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。我在哪里发现了类似的问题有人提供 https://stackoverflow.com/a/25380607/523051下面的代码来覆盖FriendlyIdnormalize_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
我试图将其“转换”为FriendlyId“候选人”,但我真的不知道我在做什么,并且以下内容不起作用。关于如何调整 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
注意我正在使用:scoped
FriendlyId 的功能,因此对现有文件夹名称的检查应正确限定在:account_id
.