界面语言选择器在轨道形式? [英] interface language selector in rails form?

查看:160
本文介绍了界面语言选择器在轨道形式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个带有列lng的用户模型,我在那里存储一个区域选择en,fr等i18n语言环境字符串。



我的目标是在所有语言列出英语,法语和在表单更新时,它会在用户表中存储正确的en,fr值。



这将是什么方法呢?

解决方案

您可以简单地使用选择标记 http://guides.rubyonrails.org/form_helpers.html#the-select-and-option-tags

  = form_for @user do | f | 
= f.select:lng,options_for_select([['English','en'],['French','fr']],@ user.lng)

我也建议将数组移到常量。例如,在它自己的模型User中的方法。例如:

 #models / user.rb 
def self.lng_list
[['English' ,'en'],['French','fr']]
end

#form
= form_for @user do | f |
= f.select:lng,options_for_select(User.lng_list,@ user.lng)

edit



以简单的形式,您可以使用Rails窗体助手,如 https://github.com/plataformatec/simple_form#wrapping-rails-form-helpers

  = f.input:lng do 
= f.select:lng,options_for_select(User.lng_list,@ user.lng)

或者您可以使用集合选项 https://github.com/plataformatec/simple_form#collections

  = f.input:lng,:collection => User.lng_list 


What would be the way to go on having a locale selection drop down in a form?

I have a User model with a column "lng" where I store the "en","fr", etc i18n locale strings.

My goal is to have a drop down with all the languages listed " English ", " French " and on form update it stores the correct "en", "fr" value in the user table.

What would be a way to go on this?

解决方案

You can simply use the select tag http://guides.rubyonrails.org/form_helpers.html#the-select-and-option-tags:

= form_for @user do |f|
  = f.select :lng, options_for_select([['English', 'en'], ['French', 'fr']], @user.lng)

I also suggest to move the array somewhere to the constant. For example, in its own method of model User. For example:

#models/user.rb
def self.lng_list
  [['English', 'en'], ['French', 'fr']]
end

#form
= form_for @user do |f|
  = f.select :lng, options_for_select(User.lng_list, @user.lng)

edited

In simple form you can use rails form helpers like this https://github.com/plataformatec/simple_form#wrapping-rails-form-helpers:

 = f.input :lng do
   = f.select :lng, options_for_select(User.lng_list, @user.lng)

Or you can use collection option https://github.com/plataformatec/simple_form#collections:

= f.input :lng, :collection => User.lng_list

这篇关于界面语言选择器在轨道形式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆