Rails - FriendlyID&祖先

时间:2015-12-03 14:13:18

标签: ruby-on-rails friendly-id ancestry

我正在尝试使用自定义函数构建slugs。我遇到的问题是当我尝试连接到具有祖先的类别时,FriendlyID正在用/替换%2F

我这样做:

class Category < ActiveRecord::Base
  extend FriendlyId
  friendly_id :set_ancestry_slug, use: :slugged

  def normalize_friendly_id(string)
    string
  end

  def set_ancestry_slug
    url = ""
    if self.ancestry?
      self.ancestors.each do | an |
        url += "#{an.title.parameterize}/"
      end
    end
    url += self.title.parameterize
    return url
  end

end

控制台看起来不错

irb(main):025:0> Category.last.slug
  Category Load (0.5ms)  SELECT  "categories".* FROM "categories"  ORDER BY "categories"."id" DESC LIMIT 1
=> "vilken-hjalp-kan-jag-fa/vad-kan-jag-gora-sjalv/tips-trix"

连接到类别

http://localhost:3000/-/vilken-hjalp-kan-jag-fa%2Fvad-kan-jag-gora-sjalv%2Ftips-trix

https://github.com/norman/friendly_id

https://github.com/stefankroes/ancestry

0 个答案:

没有答案