【问题标题】:Using Formtastic and Globalize2 together一起使用 Formtastic 和 Globalize2
【发布时间】:2010-12-29 16:05:28
【问题描述】:

我使用 Formtastic。现在我想为某些字段添加模型翻译。我看着 Globalize2,它似乎是我需要的。但我不知道如何将它与 Formtastic 集成。有人有这样的经历吗?

【问题讨论】:

    标签: ruby-on-rails formtastic globalize2


    【解决方案1】:

    所以这很容易。您可以像没有 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
    

    备注:从 Rails 2.3 开始,您可以使用 default_scope 代替 :joins => :globalize_translations。在 Find 方法和 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 gem 不适合我。所以我不得不使用插件。

    【讨论】:

    • 从 Globalize2 0.2.0 版开始,我们只能使用: default_scope :include => :translations
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多