2016-10-18 26 views
0

我正在使用ActiveAdmin,Globalize和FriendlyId構建Rails應用程序。FriendlyId不更新標題更改slug

在我的模型我解決了全球化與FriendlyId參數(摘錄):

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 

然而,當我更新ActiveAdmin的文章標題中,嵌入從未被friendly_id更新,所以我加了這個方法:

def should_generate_new_friendly_id? 
    title_changed? || super 
end 

當我這樣做,title_changed?始終是錯誤的,因爲新標題沒有發送到模型,原因我不知道,但對於其他參數未翻譯,他們得到正確。

例:

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

這怎麼可能,該模型不知道翻譯屬性的更新?
我錯過了什麼?

感謝您的幫助!

我的項目

  • Rails的4.2.7.1 /紅寶石2.3.0
  • ActiveAdmin 1.0.0pre4
  • 全球化5.0.1
  • FriendlyId 5.1.0
  • FriendlyId Globalize的1.0.0.alpha2

編輯:(我的形式摘錄)

f.translated_inputs 'Translated fields', switch_locale: true do |t| 
    t.input :title 
    t.input :content 
end 

回答

0

但是當你保存模型ActiveAdmin,如果你有一個金屬塊場的形式,並沒有覺得它有什麼會包含空字符串和不會產生slu。。 如何解決它?覆蓋模型蛞蝓setter方法是這樣的:

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



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

試試這個

+0

謝謝,但我沒有我的任何形式的「彈頭」輸入字段(我編輯我的問題添加表格提取物)。 當我創建一個新帖子時,slug爲所有語言設置正確。 – anthony