简体   繁体   English

Rails form_for multiple collection_select,表单失败时未选择值

[英]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.

相关问题 Rails form_for 和 collection_select - Rails form_for with collection_select Rails,collection_select-提交表单后,用:select记住值 - Rails, collection_select - remembering values with :selected after form submitted Ruby on Rails-form_for collection_select选项不可见 - Ruby on Rails - form_for collection_select options not visable Rails 7.0.4- form_for - collection_select 在编辑操作中有多个选项 - Rails 7.0.4- form_for - collection_select with multiple options in edit action 使用RoR form_for和collection_select - Using RoR form_for with collection_select Rails-如何将form_for collection_select转换为simple_form? - Rails - How to translate form_for collection_select to simple_form? UJS,AJAX,Rails 4,form_for collection_select将值传递给方法并将值返回给表单 - UJS, AJAX, Rails 4, form_for collection_select to pass value into method and return value back to form :selected与rails形式的collection_select一起使用时不起作用 - :selected is not working when used with collection_select in rails form Rails collection_select表单将所选值添加到数组 - Rails collection_select form adds selected value to an array Rails form_for collection_select忽略select_tag接受的远程ajax调用 - Rails form_for collection_select ignoring remote ajax call that select_tag accepts
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM