带有i18n转换的Rails select_tag

时间:2017-03-30 00:54:01

标签: ruby-on-rails ruby-on-rails-4 internationalization rails-i18n

我有以下select_tag,并希望翻译标签。我正在使用Rails Internationalization(I18n)API

现在,此代码可用于转换选项标签。

<%=  select_tag "object", 
 "<option value=address>Address</option>
  <option value=Orden>Orden</option>".html_safe %> 

我试过了:

<%=  select_tag "object", 
 "<option value=address><%= t('address').capitalize %></option>
  <option value=Orden>Orden</option>".html_safe %> 

但是我收到语法错误。我也尝试过:

<%=  select_tag "object", 
 "<option value=address> t('address').capitalize </option>
  <option value=Orden>Orden</option>".html_safe %> 

最后一个不会翻译,只是把字符串't('address')。capitalize'

有什么建议吗?

2 个答案:

答案 0 :(得分:3)

您应该查看options_for_select(args)

http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-options_for_select

options_for_select([[t('address'), "address"], ["orden", "orden"]], params[:the_selected_one])

你会这样做:

<%=  select_tag("object", options_for_select([[t('address'), "address"], ["orden", "orden"]], param[:the_selected_one])) %>

答案 1 :(得分:0)

尝试使用options_for_select代替原始HTML选项。

检查以下代码。

select_tag "object", options_for_select([t('address').capitalize, "address"], [t('order').capitalize, "order"])

您还可以使用模型来构建“选择”选项。请查看以下文档以获取更多信息。

http://apidock.com/rails/ActionView/Helpers/FormTagHelper/select_tag