[英]Rails form_for multiple collection_select, values not selected on form failure
I'm not sure why, but my form is not showing the options selected on submit, even though the params hash shows that the information is being returned to the page.我不知道为什么,但我的表单没有显示提交时选择的选项,即使 params 哈希显示信息正在返回到页面。
Collection select code:收藏选择代码:
<%= f.collection_select :post_topic_ids, PostTopic.all, :id, :name, {}, { multiple: true, class: 'form-control' } %>
Which renders:其中呈现:
<select multiple="multiple" class="form-control" name="post[post_topic_ids][]" id="post_post_topic_ids">
<option value="1">Psychology</option>
<option value="2">Engineering</option>
<option value="3">Nanotechnology</option>
</select>
Params returned after form validation error表单验证错误后返回的参数
params = {"post"=>{"post_topic_ids"=>["", "1"]}}
Update更新
I have also tried:我也试过:
<%= select_tag 'post_topic_ids', options_for_select(PostTopic.all.collect{ |p| [p.name, p.id] }), multiple: true %>
and:和:
<%= select_tag 'post_topic_ids', options_from_collection_for_select(PostTopic.all, "id", "name"), multiple: true %>
Which renders:其中呈现:
<select name="post_topic_ids[]" id="post_topic_ids" multiple="multiple"><option value="1">Psychology</option>
<option value="2">Engineering</option>
<option value="3">Nanotechnology</option></select>
you need to specify which element is selected a third parameter您需要指定选择哪个元素的第三个参数
<%= select_tag 'post_topic_ids', options_for_select(PostTopic.all.collect{ |p| [p.name, p.id] }, --->selected_element<---), multiple: true %>
look at http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/options_for_select for some examples.查看http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/options_for_select以获取一些示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.