Rails collection_select with multiple and prompt options - ruby-on-rails

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?

Related

f.country_select options default country

In my rails app I want the f.country_select to be user's country, if the user has provided the country. Else I want it to have the Select one value.
<%= f.country_select :country, nil, {:selected => #user.country, :include_blank => 'Select one'}, {:class=>'form-control'} %>
Anything wrong with the above code?
Update:
Am using the gem 'country_select'.
It should be like this:
<%= f.select(:country, options_for_select(Country, :selected => #user.country), :prompt => 'Select one' , {:class=>'form-control'}) %>

Include blank and default for collection_select helper

I would like to have a "select an option" option in my dropdown on my rails application.
I am using the collection_select helper tag and it looks something like this:
<%= collection_select(:country, :id, Country.order('name ASC'), :id,:name, {},{:class => "input-xlarge"}) %>
I want the default option of the drop-down to be "Select a country".
Use the include_blank option:
<%= collection_select(:country, :id, Country.order('name ASC'),
:id, :name,
{ include_blank: 'Select a country' },
{ :class => "input-xlarge" }) %>
See the official documentation here

How to use a table column in my select box on rails?

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" } %>

How to pre select a state in Rails Carmen state_select

What are the options I can pass in a state_select. I am trying to do :selected => state and its not working.
<%= f.state_select :state, 'US', {:prompt => '--Select One--', :selected => state}, :tabindex => autotab, :id => 'state_select, :state => state'%>
I don't think you can use state_select to do it. You'll have to use select with the state_options_for_select function in order to enter your selected state:
<%= f.select :state, state_options_for_select(state_to_select, country_code) %>
That should get you there.

:prompt for select_tag does not appear in the box

Using this code :
= f.select :how_did_you_hear, HOWD_YOU_HEAR, {:prompt => "HQCard found by"}, :style => "width: 142px;"
The prompt is seen as blank, and the first selection is "HQCard found by". Then followed by a blank space, and the collection of items.
How can I make it appear as a default within the selection box as default?
Easy to use the :include_blank:
f.select :how_did_you_hear, HOWD_YOU_HEAR, :style => "width: 142px;", {:include_blank => '- Select HowD U Hear -' }
Documentation
Wow Rails.. what a hack.
So if you use unconventionally allow_blank and selected it works :
= f.select :how_did_you_hear, HOWD_YOU_HEAR, { :include_blank => "HQCard found by", :selected => "HQCard found by"}, :style => "width: 142px;"

Resources