RoR - collection_select from array [英] RoR - collection_select from array

查看:133
本文介绍了RoR - collection_select from array的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组:

@dates= ['2013-11-01', '2013-11-02', '2013-11-03', '2013-11-04', '2013-11-05']

我如何把这些数组在视图中的collection_select?我尝试了:

how can i put these array in an collection_select in the view? I tried:

...    
<%= f.collection_select :day, Day.order(:date), :id, @dates, include_blank: false %>
...


推荐答案

对选择的值(从表单返回)和文本(在下拉列表中显示)使用日期字符串,然后

Assuming that you mean to use the date strings for both the value (returned from form) and text (displayed in drop-down) of the select then

= f.collection_select :day, @dates, :to_s, :to_s, include_blank: false

将传递:to_s到@dates集合的每个元素,并使用选择的文本(param 3)和值(param 4)的结果。

This will pass :to_s to each element of the the @dates collection and use the results for the text (param 3) and value (param 4) of the select.

这篇关于RoR - collection_select from array的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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