2016-10-18 3 views
0

ActiveAdmin, Globalize 및 FriendlyId를 사용하여 레일즈 애플리케이션을 구축 중입니다. 내가 ActiveAdmin을의 기사 제목을 업데이트 할 때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 

그러나, 슬러그가 friendly_id 업데이트되지 않습니다, 그래서 나는이 방법을 추가 : 내 모델에서

는 난의 세계화와 FriendlyId 매개 변수 (추출물)를 정착 :

def should_generate_new_friendly_id? 
    title_changed? || super 
end 

내가 그렇게 할 때, title_changed? 이유는 모르겠지만 새로운 제목이 모델에 전송되지 않았기 때문에 항상 거짓입니다. 그러나 번역되지 않은 다른 매개 변수는 제대로 getted됩니다.

예 :

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

이 어떻게 가능한 모델이 변환 된 속성의 업데이트에 대해 모르는 것이?
나는 무엇을 놓쳤는가?

도움 주셔서 감사합니다.

내 프로젝트 :

  • 레일 4.2.7.1/루비 2.3.0
  • ActiveAdmin을 1.0.0pre4
  • 5.0.1
  • 은 5.1.0
  • FriendlyId의 세계화를 FriendlyId 세계화 1.0.0.alpha2

편집 : (내 양식의 추출)

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

답변

0

하지만 ActiveAdmin을에 모델을 저장할 때, 당신은 형태로 슬러그 필드가 빈 문자열을 포함하는 것입니다 무엇을 생각하지 않는 경우 슬러그는 생성되지 않습니다. 어떻게 고칠 수 있습니까? 같은 모델의 슬러그 setter 메소드를 오버라이드 (override) :

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



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

+0

감사를 시도하지만 난 내 양식에 어떤 "슬러그"입력 필드가없는 (나는 내 ​​질문 양식 추출물을 추가 편집). 새 게시물을 만들 때 슬러그가 모든 언어에 대해 올바르게 설정됩니다. – anthony