Friendly_id 生成带有 ID 的 slug
我正在尝试使用Friendly_id gem 生成格式为“#{id}-#{title}”的slug,
看起来Friendly_id 使用before_save,并且无法访问ID 属性。
有解决办法吗?
# Permalinks
#-----------------------------------------------------------------------------
extend FriendlyId
friendly_id :id_and_title, :use => :slugged
def id_and_title
"#{id} #{title}"
end
I am trying to use the friendly_id gem to generate a slug in the format of "#{id}-#{title}"
It looks like friendly_id uses before_save, and won't have access to the ID attribute.
Is there a work around for this ?
# Permalinks
#-----------------------------------------------------------------------------
extend FriendlyId
friendly_id :id_and_title, :use => :slugged
def id_and_title
"#{id} #{title}"
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在模型中重写
to_param
,以包含标题This 应该具有您所追求的效果,而无需使用amilie_id,而不是使用Friendly_id。
Instead of using friendly_id for this, you could override
to_param
in your model, to include the titleThis should have the effect you're after without having to use friendly_id.