简体   繁体   English

一起使用Formtastic和Globalize2

[英]Using Formtastic and Globalize2 together

I use Formtastic. 我使用Formtastic。 Now I would like to add model translations for some fields. 现在,我想为某些字段添加模型翻译。 I look at Globalize2 and it seems like what I need. 我看着Globalize2,看起来好像我需要的。 But I have no idea how to integrate it with Formtastic. 但是我不知道如何将其与Formtastic集成。 Does anybody have such experience? 有人有这样的经验吗?

So it's quite easy. 所以这很容易。 You can use it in the same manner as you don't have a Formtastic. 您可以像没有Formtastic一样使用它。

In migration: 在迁移中:

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

In model: 在模型中:

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

One remark: since rails 2.3 you can use default_scope instead of :joins => :globalize_translations . 备注:从rails 2.3开始,您可以使用default_scope而不是:joins =>:globalize_translations In earlier versions of rails in Find methods and in named_scopes (for example) you should write: 在早期版本的Rails中,在Find方法和named_scopes中(例如),您应该编写:

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

In view: 鉴于:

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

PS: Globalize2 gem doesn't work for me. PS:Globalize2 gem对我不起作用。 So I had to use plugin. 所以我不得不使用插件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM