在Rails 3中的en.yml中指定标签值

时间:2012-07-18 07:55:40

标签: ruby-on-rails ruby ruby-on-rails-3 localization

我正在写一个网页,我需要有一些单选按钮供选择。

代码如下:

  - Person.food_type.options[:flags].each do |cs|
    = radio_button_tag "Person[food_type]", cs, @Person.food_type == cs ? true : false
    = label_tag(:food_type,cs.to_s.capitalize, :class => "checkbox_label")

对于生成的每个标签,我想在其中指定该标签的值 en.yml文件。

如何在en.yml中定义这些标签值?

例如,

代替标签:

[单选按钮]辛辣食物,

我希望它是:

[单选按钮]辣味食物。

1 个答案:

答案 0 :(得分:1)

你能做这样的事吗:

- Person.food_type.options[:flags].each do |cs|
  = radio_button_tag "Person[food_type]", cs, @Person.food_type == cs ? true : false
  = label_tag(:food_type,t('.label_#{cs}'), :class => "checkbox_label")

并在.yml文件中添加值。

您还可以在模型中更改或创建to_s方法。