问题描述
显然FriendlyId已更改,它是以前的默认方法,该方法是将数字序列附加到重复的段塞(这是我想要的),以现在使用UUID:
Apparently FriendlyId has changed it's previously default method of appending a numeric sequence to duplicate slugs (which is what I want) to now use 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的原始方法.我发现了一个类似的问题,其中有人提供了下面的代码来覆盖friendlyId normalize_friendly_id
方法以获得我的功能m之后,但使用它会导致错误(wrong number of arguments (given 1, expected 0)
):
This functionality is not something I'm interested in at this time and would much prefer to have the original method that results in a cleaner URL. I found a similar question where someone provided the below code to override the friendlyId normalize_friendly_id
method to get to the functionality I'm after, but using it results in an error (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方法以产生我所得到的结果的任何想法?
I attempted to "convert" this into a friendlyId "candidate" but I don't really know what I'm doing and the below doesn't work. Any thoughts on how I could tweak the name_candidate method to produce the result I'm afer?
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
.
Note I am utilizing the :scoped
functionality of friendlyId, so checks for existing folder names should be correctly scoped to :account_id
.
推荐答案
friendly_id 5现在具有slug_candidates,可让您自定义该slug.
friendly_id 5 now has a slug_candidates that lets you customize the slug.
因此,要生成顺序的段塞,您可以执行以下操作:
So to generate a sequential slug you could do:
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
This is discussed in the following issue:https://github.com/norman/friendly_id/issues/480
这篇关于使用Rails 5,如何使FriendlyId附加-"count + 1"?复制弹头而不是UUID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!