一起使用 Formtastic 和 Globalize2

发布于 2024-08-15 16:35:51 字数 99 浏览 10 评论 0原文

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

I use Formtastic. Now I would like to add model translations for some fields. I look at Globalize2 and it seems like what I need. But I have no idea how to integrate it with Formtastic. Does anybody have such experience?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

近箐 2024-08-22 16:35:51

所以这很容易。您可以像没有 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。在早期版本的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 不适合我。所以我不得不使用插件。

So it's quite easy. You can use it in the same manner as you don't have a 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. In earlier versions of rails in Find methods and in named_scopes (for example) you should write:

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 %>

P.S: Globalize2 gem doesn't work for me. So I had to use plugin.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文