Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RoR - collection_select from array

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 %>
...
like image 821
ciaodarwin Avatar asked Nov 21 '13 13:11

ciaodarwin


1 Answers

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.

like image 98
AndyV Avatar answered Oct 16 '22 22:10

AndyV