我的模型中有以下内容:
class Dispenser < ActiveRecord::Base
extend FriendlyId
friendly_id :slug_candidates, use: :slugged
def slug_candidates
[
:full_name,
[:full_name, :id]
]
end
end
这会生成如下的 slugs:
=> 'bob-barker-bob-barker-15'
确实应该是这样bob-barker
or bob-barker-15
,但不能两者兼而有之。
https://github.com/norman/friend_id https://github.com/norman/friendly_id
此处为FriendlyId 作者。如前所述,为此您需要FriendlyId 5,它不适用于4.0。
另请注意,您不能使用 id 字段作为 slug 的一部分,因为 slug 是在创建记录之前生成的。
如果您愿意在 slug 中包含 id 字段,那么除了FriendlyId 之外还有更简单的解决方案,例如覆盖to_param
所描述的方法here https://gist.github.com/cdmwebs/1209732.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)