0

私はFormtasticを使用しています。ここで、いくつかのフィールドにモデルの翻訳を追加したいと思います。Globalize2を見ると、必要なもののようです。しかし、それをFormtasticと統合する方法がわかりません。誰かそのような経験がありますか?

4

1 に答える 1

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

1 つの注意: Rails 2.3 以降、 :joins => :globalize_translationsの代わりにdefault_scopeを使用できます。Rails の以前のバージョンでは、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 %>

PS: Globalize2 gem が機能しません。そのため、プラグインを使用する必要がありました。

于 2009-12-24T09:04:23.773 に答える