在collect_select上使用大写 [英] using capitalize on a collection_select

查看:355
本文介绍了在collect_select上使用大写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果在我找不到之前已回答。

If this has been answered before I cannot find it.

我有以下:

= f.collection_select :sex_id, @sexes, :id, :name

并在控制器中:

@sexes = Sex.all

性别都以小写形式存储,如下所示:

the sexes are all stored in lowercase, like this:

id|name
 1|steer
 2|heifer
 3|holstein

我需要他们输出与首字母字母:

I need them to output with Capital First letters:

Steer
Heifer
Holstein

我试过:

= f.collection_select :sex_id, @sexes, :id, :name.capitalize
= f.collection_select :sex_id, @sexes, 'id', 'name'.capitalize

但他们不工作,我真的不希望他们,但必须尝试他们发布之前。

but they do not work, and I didn't really expect them to, but had to try them before posting this.

推荐答案

collection_select 在每个对象上调用一个方法,选项值。您可以在模型中添加一个新方法以获得正确的值:

collection_select calls a method on each object to get the text for the option value. You can add a new method in the model to get the right value:

def name_for_select
  name.capitalize
end

然后在视图中:

= f.collection_select :sex_id, @sexes, :id, :name_for_select

这篇关于在collect_select上使用大写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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