创建一个灵活的本地化的Ruby-on-Rails值列表 [英] Create a flexible, localized, Ruby-on-Rails list-of-values

查看:39
本文介绍了创建一个灵活的本地化的Ruby-on-Rails值列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个想要的值列表(初学者,中级,高级,流利,本机):

I have a list of values (Beginner, Intermediate, Advanced, Fluent, Native) that I would like to:

  • 充当SELECT列表的模型
  • 用作将ID转换为HTML表中的值的模型
  • 在多个控制器和视图中使用
  • 保持保留业务规则的顺序(按技能级别排序)
  • 在将来的某个时间本地化

有没有一种方法可以实现此列表来满足我的全部或大部分需求?

Is there a way of implementing this list to address all or most of my needs?

推荐答案

您可以将数组的定义放在/config/locales/some_language.yml

You can put the definition of an array in /config/locales/some_language.yml

例如,您可以在en.yml中添加以下内容

for example you could have the following in en.yml

en:
   experience_levels: 
          1: Beginner 
          2: Intermediate
          3: Advanced 
          4: Fluent
          4: Native

要将键值对放入帮助器中,应执行以下操作:

To get the key value pairs into a helper, the following should work:

def experience_levels 
    I18n.t(:experience_levels).map { |key, value| [ value, key ] } 
end

要在选择框中使用它,只需将以下内容放入form_for

To use this in a select box,you would just put the following in your form_for

<%= f.select :experience_levels, experience_levels %>

如果您使用的是form_tag,则需要以下内容:

If you are using form_tag instead you need the following:

<%= select_tag :experience_levels, options_for_select(experience_levels) %>

这篇关于创建一个灵活的本地化的Ruby-on-Rails值列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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