2

我正在构建一个 Rails 应用程序,并且在为定义了第二个语言环境的文章生成 slug 时卡住了。
对于主要语言环境(法语),它检查文章是否已经有标题,如果是这样,则在末尾添加一个整数(id),但对于第二语言环境(英语),它只是生成 slug 而不检查文章是否存在(这给了我重复的蛞蝓)。

这是我的模型

class Post < ActiveRecord::Base
  translates :title, :slug, :content, fallbacks_for_empty_translations: true 
  active_admin_translates :title, :slug, :content, fallbacks_for_empty_translations: true 

  extend FriendlyId 
  friendly_id :slug_candidates, use: [:slugged, :globalize, :finders]

  private

  def slug_candidates
    [:title, [:title, :deduced_id]] # works for main locale but not others
  end

  def deduced_id
    self.class.where(title: title).count + 1
  end
end

当文章已经存在具有相同标题时,如何将 id 添加到辅助语言环境的 slug 中?

谢谢你的帮助 !

我的项目

  • 导轨 4.2.6
  • ActiveAdmin 1.0.0.pre2
  • 全球化 5.0.1
  • 友好ID 5.1.0
  • FriendlyId-全球化 1.0.0.alpha2
4

1 回答 1

1

I finally get it working updating the slug_candidates method like this:

def slug_candidates
  [[:title, :deduced_id]]
end
于 2016-06-05T00:41:53.680 回答