Multiple select box is not working in rails

时间秒杀一切 提交于 2019-12-08 02:34:26

问题


I am using the following code to select multiple values.

 <select name="video" multiple="multiple" id="form-field-select-2" class="form-control">
 <%video.each do |option|%>
 <option><%=option%> </option>
 <%end%>
 </select>

But after submitting form its not giving all selected values,instead of its giving only the last selected value.

Please share if you have any idea about this issue.


回答1:


 <select name="video[]" multiple="multiple" id="form-field-select-2" class="form-control">
 <%video.each do |option|%>
 <option><%=option%> </option>
 <%end%>
 </select>

You need to make sure that you are sending an array of answers by changing the name to video[]




回答2:


You can do this with select_tag too

<%= select_tag 'video', options_from_collection_for_select(Video.all,"id","name"),  :multiple => true, class: 'form-control'%>

For more details,see this API



来源:https://stackoverflow.com/questions/23646076/multiple-select-box-is-not-working-in-rails

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