https://www.w3schools.com/html/tryit.asp?filename=tryhtml_elem_select_size
The select box I see is an area based select box as opposed to a drop down. However, In rails I can only get the drop down based select box. I have tried:
<%= f.collection_select :role_cont, Role.where(company: current_user.company), :name, :name, include_blank: true, :multiple => true, class: 'form-control chosen-it', :size => 10 %>
The above does not work. Note the :size attribute. From a gem I have:
<%= select_tag 'recipients', recipients_options(#chosen_recipient), multiple: true, class: 'form-control chosen-it' %>
This does work, it creates an area based select box that I want. How do I set my select boxes to be a scroll able area as opposed to a simple drop down? Also I want to be able to use scroll able select box even when I can only select a singular object
Here why you added ':multiple => true' if you want select only one option,
these option is used when you want to select multiple values from select box.
<%= f.collection_select :role_cont, Role.where(company: current_user.company), :name, :name, include_blank: true, :multiple => true, class: 'form-control chosen-it', :size => 10 %>
Remove :multiple => true and it work as you want without area based select box
Related
The answers to how do I use a default placeholder ... in a select_tag? show how to set a default value for the select field.
For example:
<%= f.select :country, countries, {include_blank: true, prompt: "United States"}, {required: true, class: "form-control"} %>
The problem here is that I only want the silhouette of the placeholder, not an actual value that could accidentally be submitted.
If we compare to the placeholder value in a text_field, the difference is much more clear. We see the placeholder:
But we cannot accidentally submit the placeholder.
How can I get similar behaviour with the .select field?
That is, so that there is some placeholder in the select field, but so it cannot be accidentally submitted?
Just change the boolean value of include_blank: true to the desired placeholder string:
<%= f.select :country,
countries,
{ include_blank: "- nothing selected -" },
{ required: true, class: "form-control" }
%>
This will create a pre-selected select option with no value, which will be interpreted by Rails controllers as nil.
According to https://developer.mozilla.org/en-US/docs/Web/HTML/Element/select a select has no placeholder option. If you wish to show a predefined value that should not be submitted you can add a disabled option to this specific item and combine it with selected.
<%= f.select :country, [['USA', 1], ['Canada', 2]], { disabled: '1', selected: '1' } %>
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 %>
Currently when I create a multi select the prompt option can be selected along with the other options. I would like to avoid the prompt being multi selected. Current code:
<%= collection_select(:vehicle_group ,:user_group_id, VehicleGroup.all, :id, :group_name, {:prompt => true}, {:multiple => true})%>
I have also tried include_blank with the same result being that "Please select" can be multi selected:
<%= collection_select(:vehicle_group ,:user_group_id, VehicleGroup.all, :id, :group_name, {:include_blank => "Please select"}, {:multiple => true})%>
Is there any way to avoid this?
I'm trying to create a select box that takes data from my db. I'm having trouble setting this up. I tried this code:
<%= f.fields_for :unit do |u| %>
<%= u.label :name %>
<%= u.select :name, :class => "ingredient_unit", :prompt => "Please Select" %>
<% end %>
but I'm missing the part of the choices, I don't know how to pull them out of the database. I tried using collection_select, which worked, but then the class option wasn't working... collection_select went like this:
<%= u.collection_select :unit, Unit.all, :id, :name, :class => "ingredient_unit", :prompt => "Please Select" %>
I also don't understand what the first symbol means (:unit), it seems to be setting the html id and name, so that can be anything I want it to be?
You should look at the documentation for collection_select and select. But to answer your question, for the select part, you forgot to pass the list of options to choose from. You also need to swap the order for prompt and class since prompt is an option for the helper and class is an html option
<%= u.select :unit_id, Unit.all.map { |u| [u.name, u.id] }, { :prompt => "Please Select" }, { :class => "ingredient_unit" } %>
For the collection select
<%= u.collection_select :unit_id, Unit.all, :id, :name, { :prompt => "Please Select" }, { :class => "ingredient_unit" } %>
The first parameter passed to both helper is the column name where you want the selected answer to be saved. The 2 codes above just shows 2 different ways to generate the same select tag.
The first symbol tells it which field to populate with the id returned from the user selection.
Also, you should wrap your class section in {}
:unit refers to the model attribute that you're using for the select element. Yes, it will setup the name/id of the element (and name is the most important for the params hash).
To set a class in the collection_select, specify it as a hash as that helper takes it as an html_option.
<%= u.collection_select :unit, Unit.all, :id, :name, { :prompt => "Please Select" }, { :class => "ingredient_unit" } %>
I am trying to have a multiple select box. select box will contain all the stores in the DB but the ones that the user belongs to will be selected.
I'm half way there. I got a select box which has all the stores in the database. I'm unable to select the ones that the user belongs to.
I have the following:
<%= select_tag 'stores[]', options_for_select(#stores.map {|s| [s.store_name, s.store_id]},
:selected => #user.stores.map {|j| [j.store_name, j.store_id]}), :multiple => true, :size =>
10 %>
I have a map with stores that a user belongs to. it is in:
#user.stores
after a fair amount of trial and error the following worked for me:
<%= select_tag 'stores[]', options_for_select(#stores.map { |s| [s.store_name, s.store_id] }, #user.stores.pluck(:id)), multiple: true, size: 10 %>
Another way of doing this would be to use the options_from_collection_for_select helper method. It will look something like this:
<%= select_tag 'stores[]', options_from_collection_for_select(#stores, :store_id, :store_name, [4,5,6]), multiple: true, size: '10%' %>