Rails: simple_form association with i18n

纵然是瞬间 提交于 2019-12-08 08:55:58

问题


I have a question regarding the simple_form gem.

If I have two languages and use the t('') function from to translate text, which I have put into my .yml files.

When using <%= f.association :type %> , is it possible to let simple_form render

<select value="database_value1">t('database_value1')</select> ?

Thank you


回答1:


Possibly, it'll translate collections for you: https://github.com/plataformatec/simple_form
Association docs: http://rubydoc.info/github/plataformatec/simple_form/master/SimpleForm/FormBuilder:association

From the docs
Simple Form also has support for translating options in collection helpers. For instance, given a User with a :gender attribute, you might want to create a select box showing translated labels that would post either male or female as value. With Simple Form you could create an input like this:

f.input :gender, collection: [:male, :female]

And Simple Form will try a lookup like this in your locale file, to find the right labels to show:

en:
  simple_form:
    options:
      user:
        gender:
          male: 'Male'
          female: 'Female'

Maybe try: <%= f.association :type, collection: [:some, :collection, :options]%>



来源:https://stackoverflow.com/questions/22581490/rails-simple-form-association-with-i18n

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!