Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何选择集合中的下拉选项文本?_Ruby On Rails - Fatal编程技术网

Ruby on rails 如何选择集合中的下拉选项文本?

Ruby on rails 如何选择集合中的下拉选项文本?,ruby-on-rails,Ruby On Rails,在Rails应用程序中,我试图在选择标记中格式化一个值 差不多 <%= f.collection_select(:country_id, Country.order(:name), :id, :name.to_s.downcase) %> downcase方法没有任何效果。我应该这样使用它吗?如果没有,我应该怎么做 谢谢您可以为您的模型添加一个方法,并为此添加一个符号 看 国家级 def名称_至_较低 self.name.downcase 结束 结束 或使用选择- 如果数

在Rails应用程序中,我试图在选择标记中格式化一个值

差不多

<%= f.collection_select(:country_id, Country.order(:name), :id, :name.to_s.downcase) %>

downcase方法没有任何效果。我应该这样使用它吗?如果没有,我应该怎么做


谢谢

您可以为您的模型添加一个方法,并为此添加一个符号

国家级
def名称_至_较低
self.name.downcase
结束
结束
或使用选择-


如果数据库区分大小写,则可能需要指定大小写排序

<%= f.select(:country_id, Country.order("UPPER(name)").map {|x| [x.name.downcase, x.id] } %>

<%= f.select(:country_id, Country.order(:name).map {|x| [x.name.downcase, x.id] } %>
<%= f.select(:country_id, Country.order("UPPER(name)").map {|x| [x.name.downcase, x.id] } %>