RoR - collection_select from array

ⅰ亾dé卋堺 提交于 2020-01-14 07:23:08

问题


I've a little array:

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

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 %>
...

回答1:


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

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.



来源:https://stackoverflow.com/questions/20122257/ror-collection-select-from-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!