使用Rails 5,我怎样才能使FriendlyId附加一个 – “count + 1”来复制slus而不是UUID?
显然,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.
此function不是我现在感兴趣的东西,而是更喜欢使用原始方法来生成更清晰的URL。 我发现了一个类似的问题,其中有人提供了以下代码来覆盖friendlyId normalize_friendly_id
方法以获得我之后的function,但使用它会导致错误( 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 0 # end def name_candidates append_number = self.count "name = #{name}" if name > 0 [ :name, :name, append_number ] end end
注意我正在使用friendlyId的:scoped
function,因此检查现有文件夹名称应该正确地限定为:account_id
。
因为normalize_friendly_id接受一个参数: http : //www.rubydoc.info/github/norman/friendly_id/FriendlyId%2FSlugged%3Anormalize_friendly_id
调用super时,必须将名称作为参数传递
我在另一个线程中遇到了这个答案 ,因为该模块已经实现了friendlyId,我所要做的就是更改use: :slugged
to use: sequentially_slugged
来生成我之后的function。
class Folder < ApplicationRecord extend FriendlyId friendly_id :name, use: [ :sequentially_slugged, :scoped ], scope: :account_id end