5

我正在尝试使用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
4

1 回答 1

14

您可以在模型中覆盖而不是使用friendly_id to_param,以包含标题

class YourModel < ActiveRecord::Base
  def to_param
    "#{id} #{title}".parameterize
  end
end

这应该具有您所追求的效果,而无需使用friendly_id。

于 2012-02-07T00:03:44.287 回答