Reputation: 650
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:
Edit: (Extract of my form)
f.translated_inputs 'Translated fields', switch_locale: true do |t|
t.input :title
t.input :content
end
Upvotes: 0
Views: 511
Reputation: 394
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
Upvotes: 2