0

I am building a Rails application using ActiveAdmin, Globalize and FriendlyId.

In my model I settled up the Globalize and FriendlyId parameters (extract):

class Post < ActiveRecord::Base
  translates :title, :slug, :content
  active_admin_translates :title, :slug, :content do
    validates :title, presence: true
  end

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

  private

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

  # Used to add prefix number if slug already exists
  def deduced_id
    count = Post.where(title: title).count
    return count + 1 unless count == 0
  end
end

However, when I update an article title in ActiveAdmin, the slug is never updated by friendly_id, so I added this method:

def should_generate_new_friendly_id?
   title_changed? || super
end

When I do that, title_changed? is always false as new title is not sent to the model for a reason I don’t know but for other parameters not translated they are getted properly.

Ex:

logger.debug(title) # => Give me new updated title value BUT
title_changed? # => Always nil
online_changed? # => Works

How is it possible that the model doesn’t know about the update of translated attributes ?
What did I miss ?

Thanks for your help !

My Project:

  • Rails 4.2.7.1 / Ruby 2.3.0
  • ActiveAdmin 1.0.0pre4
  • Globalize 5.0.1
  • FriendlyId 5.1.0
  • FriendlyId Globalize 1.0.0.alpha2

Edit: (Extract of my form)

f.translated_inputs 'Translated fields', switch_locale: true do |t|
    t.input :title
    t.input :content
end
anthony
  • 640
  • 1
  • 10
  • 32

1 Answers1

2

But when you're saving model in ActiveAdmin, if you have a slug field in a form and don't feel it, what will contain empty string and the slug won't be generated. How to fix it? Override the slug setter method in model like this:

 def should_generate_new_friendly_id?
    slug.blank? || title_changed?
 end



   def slug=(value)
    if value.present?
      write_attribute(:slug, value)
    end
  end

try this

Serhii Danovskyi
  • 385
  • 3
  • 17
  • Thanks but I don't have any "slug" input field in my form (I edited my question adding a form extract). And when I create a new post, slug is properly set for all languages. – anthony Oct 18 '16 at 10:56