Rails form_for multiple collection_select, values not selected on form failure -
i'm not sure why, form not showing options selected on submit, though params hash shows information being returned page.
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 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 specify element selected third parameter
<%= select_tag 'post_topic_ids', options_for_select(posttopic.all.collect{ |p| [p.name, p.id] }, --->selected_element<---), multiple: true %>
look @ http://apidock.com/rails/actionview/helpers/formoptionshelper/options_for_select examples.
Comments
Post a Comment