翻译ActiveRecord的集合下拉 [英] Translating activerecord collection for a dropdown

查看:96
本文介绍了翻译ActiveRecord的集合下拉的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个模式,我有一些州

In a model I have some states

STATES = ["in_progress", "active", "archived"]

在我的表单我希望有一个下拉/使用

In my form I want a drop down/select with

In Progress, Active, Archived  for english 

ชำระ , ขัน , ยื่น in Thai 

什么是处理这一问题的最佳方法是什么?

What is the best way to handle this ?

一个选择,我还以为是以下

One option I have thought of is the following

def self.states
  @states = {}
  STATES.each do |s|
    @states[s] = I18n.t(s)
  end
  @states
end

有没有更好的办法?

Is there a better way?

推荐答案

这是支持simple_form:

This is supported by simple_form:

在你看来,你应该只使用

In your view, you should just use

<%= f.input :state, collection: ["in_progress", "active", "archived"] %> 

在你的YAML文件,你应该有

In your yaml file, you should have

simple_form:
  options:
    defaults:
      state:
        in_progress: In progress
        active: Active
        archived: Archived

这篇关于翻译ActiveRecord的集合下拉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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