简体   繁体   中英

Rails :: Generate slugs with FriendlyId, Globalize and slug_candidates

I am building a Rails application and I am stuck when generating slug for an article with the second locale defined.
For the main locale (french), it checked if an article already have the title and if it's the case add an integer (id) at the end but for the second locale (english) it's just generate the slug without checking if article exists (which give me duplicated slugs).

Here is my model :

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

How can I get the id added to slug for secondary locales when article already exists with same title ?

Thanks for your help !

My Project :

  • Rails 4.2.6
  • ActiveAdmin 1.0.0.pre2
  • Globalize 5.0.1
  • FriendlyId 5.1.0
  • FriendlyId-globalize 1.0.0.alpha2

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

def slug_candidates
  [[:title, :deduced_id]]
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM