ruby-on-rails – 在collection_select上使用大写
发布时间:2020-12-17 04:17:40 所属栏目:百科 来源:网络整理
导读:如果在我找不到它之前已经回答了这个问题. 我有以下内容: = f.collection_select :sex_id,@sexes,:id,:name 这在控制器中: @sexes = Sex.all 性别都以小写形式存储,如下所示: id|name 1|steer 2|heifer 3|holstein 我需要他们用Capital First字母输出: S
如果在我找不到它之前已经回答了这个问题.
我有以下内容: = f.collection_select :sex_id,@sexes,:id,:name 这在控制器中: @sexes = Sex.all 性别都以小写形式存储,如下所示: id|name 1|steer 2|heifer 3|holstein 我需要他们用Capital First字母输出: Steer Heifer Holstein 我试过了: = f.collection_select :sex_id,:name.capitalize = f.collection_select :sex_id,'id','name'.capitalize 但它们不起作用,我并没有真正期待它们,但在发布之前不得不尝试它们. 解决方法
collection_select在每个对象上调用一个方法来获取选项值的文本.您可以在模型中添加新方法以获得正确的值:
def name_for_select name.capitalize end 然后在视图中: = f.collection_select :sex_id,:name_for_select (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |