Ruby on rails collection_select. Multiple and remote not working together - ruby-on-rails

Ok, I have a multi select box controlled by chosen jquery plugin.
I can get multi select working without ajax, and ajax working without multiselect, but not the two together.
Here multi select works, but reloads the whole page when an item is selected (remote not working)
<%= f.collection_select :genre_cont_any, [t('genre.alternative'), t('genre.blues'), t('genre.children'), etc etc etc.............. ], :to_s, :to_s, {}, { :multiple => true, remote: true, onchange: "this.form.submit();" } %>
Here ajax works fine, reloading my list only, but I can only select one option at a time (multiple not working)
<%= f.collection_select :genre_cont_any, [t('genre.alternative'), t('genre.blues'), t('genre.children'), etc etc etc.............. ], :to_s, :to_s, {},:data => { :multiple => true, :remote => true, onchange: "this.form.submit();" }} %>
I want to be able to multi select, and with each new addition, to send an ajax request and reload the list.
Any advice on linking everything would be great! Thanks!

Figured it out:
<%= f.collection_select :genre_cont_any, [t('genre.alternative'), t('genre.blues'), t('genre.children'), etc etc etc.............. ], :to_s, :to_s, {},:data => { :remote => true, onchange: "this.form.submit();" }, :multiple => true %>

Related

How to save multiple check box into database in rails 6

I want to save multiple check box category[] into database :-
<label><%= f.check_box :category,{class: 'chk'},1,0%>Apple</label>
<label><%= f.check_box :category,{class: 'chk'},1,0%>Orange</label>
<label><%= f.check_box :category,{class: 'chk'},1,0%>Banana</label>
Here my create form
Edit form
controller
database structure
There is a :multiple option, if that's what you need? It's hard to understand exactly what you want
check_box("puppy", "commands", {:multiple => true}, "sit", nil)
check_box("puppy", "commands", {:multiple => true}, "fetch", nil)
check_box("puppy", "commands", {:multiple => true}, "roll_over", nil)
Further examples here: https://apidock.com/rails/ActionView/Helpers/FormHelper/check_box

How to disable a collection_select dropdown in ruby

Is it possible to have the collection_select dropdown be unclickable(disabled)? I would like the collection_select to initially have a selection displaying but be disabled and then when some other button is clicked, the collection_select is re-enabled again(via javascript) and user can now scroll through the dropdown and click on something else. I tried :disabled => true like in the following but this did not work for me:
Embedded ruby in my html
<%=
collection_select(
:post,
:post_name,
Post.all,
:post_name,
:post_name,
{:selected => Post.where(:p_address => #parentpost.p_address).select("post_name").first.post_name,
},
{:id=>'post_collection_select',
:onchange => "DoStuff(this.value); return false;",
:autocomplete => "off",
:disabled => true
}
)
%>
So far adding the :disabled => true does nothing for me. The collection_selection is behaving exactly as it was before which is the following: it displays many post names in the drop down and one is selected based on the ActiveRecord query provided
Use
:disabled => 'disabled'
instead of
:disabled => true
Then when you want to enable the select box use the following jQuery command:
$('#post_collection_select').prop('disabled',false);

How to control :selected for collection_select?

Currently if you call #team.user_ids it returns "1,3,7" as opposed to [1,3,7]. I need it to be like that for other reasons, but it's breaking the front-end collection_select. I've tested and it works fine with an array, so now the only step left is to manually choose what items are selected considering I can't do it the Rails default way.
= f.collection_select :user_ids, #company.users, :id, :username,
{ :include_blank => true }, { :multiple => true, :class => "chzn-select" }
How would I go about doing this? Thanks!
I solved it by adding :selected => #team.users.map(&:id)

Using form_for multi-select fields with acts_as_taggable_on

I can't figure out the right code for using a predetermined set of options for a multi-select field. I want to have a list of skills in a drop down that users can select from. Here is the code I am using, it works fine as a single select field, but not as a multi-select:
<%= form_for(#user, :html => { :class => "form-stacked" } ) do |f| %>
...
<div class="clearfix"><%= f.select :skill_list, options_for_select(["Asst", "dir", "pres"]),
{
:multiple => true,
:class => "chzn-select",
:style => "width:450px;" } %></div>
...
<% end %>
Anyone have any suggestions? Eventually, I will want to store all of the options for the multi-select form elsewhere because there will be a bunch, but this is the first challenge I can't figure out..
Thanks.
EDIT
I have also tried:
:html => { :multiple => true, :class => "chzn-select", :style => "width:450px;" } and it doesnt work either
There needs to be two pairs of brackets, one for the options, and one for html_options, like so:
<%= f.select :skills_list, options_for_select(["Asst", "dir", "pres"]), {}, {:multiple => true, :class => "chzn-select", :style => "width:450px;" } %>
See the docs for the select helper.

display previously selected choices in multiple select form (rails)

I've built a multi-select form (from within form_for) like this:
<div class="rounded-block quarter-wide radio-group">
<h4>Exclude customers from source:</h4>
<%= f.select :excluded_sources, options_for_select(User.select(:source).group(:source).order(:source).map {|u| [u.source,u.source]}), {:include_blank => false}, {:multiple => true} %>
<%= f.error_message_on :excluded_sources %>
</div>
this works well for what I need. The only problem is that when i go back to the page that displays the choices, I don't see what was previously selected (i.e. what is already in the DB at time of rendering). Is there an easy way to get rails to display what's previously been selected? I'd MUCH prefer not to switch to check boxes.
in my matching profiles model (corresponding to the table that stores excluded_sources), i have this:
serialize :excluded_sources
this ended up being the relevant piece:
:selected => matching_profile.send(:excluded_sources)
here:
<div class="rounded-block quarter-wide radio-group">
<h4>Exclude customers from source:</h4>
<%= f.select :excluded_sources, options_for_select(User.select(:source).group(:source).order(:source).map {|u| [u.source,u.source]}, :selected => matching_profile.send(:excluded_sources)), {:include_blank => false}, {:multiple => true} %>
<%= f.error_message_on :excluded_sources %>

Resources