2009-12-23 7 views
0

나는 Formtastic을 사용합니다. 이제 일부 필드에 모델 번역을 추가하고 싶습니다. 나는 Globalize2를보고 그것이 필요한 것처럼 보인다. 그러나 Formtastic과 어떻게 통합 할 수 있는지 전혀 알지 못합니다. 아무도 그런 경험이 있습니까?함께 Formtastic 및 Globalize2 사용

답변

0

매우 간단합니다. Formtastic이없는 것과 같은 방식으로 사용할 수 있습니다. 마이그레이션에서

:

class CreateCategories < ActiveRecord::Migration 
    def self.up 
    create_table :categories do |t| 
     t.timestamps 
    end 
    Category.create_translation_table! :name => :string 
    end 
    def self.down 
    drop_table :categories 
    Category.drop_translation_table! 
    end 
end 

모델에서 :

class Category < ActiveRecord::Base 
    attr_accessible :name 
    translates :name 

    default_scope :include => :globalize_translations 

    named_scope :top_categories, {:conditions => {:category_translations => {:locale => I18n.locale}}, 
           :order => 'name asc'} 
end 

한 발언 : 레일 2.3 이후는 default_scope 대신 를 사용할 수 있습니다 조인 => : globalize_translations. 찾기 방법과 named_scopes에서 레일의 이전 버전 (예) 당신은 작성해야 :

named_scope :top_categories, {:joins => :globalize_translations, 
           :conditions => {:category_translations => {:locale => I18n.locale}}, 
           :order => 'name asc'} 

보기에서 :

<% semantic_form_for @category do |f| %> 
    <% f.inputs do %> 
    <%= f.input :locale, :as => :hidden, :value => I18n.locale %> 
    <%= f.input :name %> 
    <% end %> 
    <%= f.buttons %> 
<% end %> 

P.S을 : Globalize2 보석 나를 위해 작동하지 않습니다. 그래서 나는 플러그인을 사용해야했다.

+0

** Globalize2 버전 0.2.0부터 다음을 사용할 수 있습니다. ** default_scope : include => : translations – Voldy