我正在使用 globalize_accessor gem 来呈现具有多语言输入:name
字段的表单,并希望根据已将哪些语言作为参数传递给该globalize_attribute_names
方法来限制用户可以输入的语言。这样的事情可能吗?
这是我到目前为止所拥有的:
chart.rb
class Chart < ActiveRecord::Base
belongs_to :chart_type
belongs_to :section
has_many :data_points, dependent: :destroy
translates :name
globalize_accessors :attributes => [:name]
validates :name, presence: true
accepts_nested_attributes_for :data_points
end
charts/new.html.haml
= simple_form_for chart, html: {multipart: true} do |f|
- Chart.globalize_attribute_names.each do |lang|
= f.input lang
= f.input :chart_type, collection: @chart_types
= f.input :section, collection: @sections, label_method: :heading
= f.simple_fields_for :data_points, chart.data_points.build do |c|
= c.input :file, as: :file
= f.submit
更新
据我所知,为翻译定义语言环境的唯一方法是在模型级别显式传递它们。然而,当尝试使用一种方法来定义这些时,我看到一个未定义的局部变量或方法错误。我目前使用的方法是用于实验,一个理想的用例是让这个方法调用图表的父级并为其返回可接受的语言。这是更新的代码:
class Chart < ActiveRecord::Base
belongs_to :chart_type
belongs_to :section
has_many :data_points, dependent: :destroy
translates :name
globalize_accessors :attributes => [:name], locales: languages
validates :name, presence: true
accepts_nested_attributes_for :data_points
def self.languages
[:en, :fr]
end