Include blank and default for collection_select helper - ruby-on-rails

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

Related

How to add html class to rails f.select field?

I have found a few similar articles on stack but none of these examples seem to do the trick.
I'm calling map on an object, to create options in an f.select field, and I'm also using Include_blank option, then trying the class and the class will not work.
I've tried the following:
<%= f.select(:city_race_id, #city_races.map {|n| [n.office, n.id]}, include_blank: true, html: { class: "form-control" }) %>
and
<%= f.select(:city_race_id, #city_races.map {|n| [n.office, n.id]}, include_blank: true, :class => 'form-control' ) %>
both render form and doesn't break rails, but neither show styling.
Try this way :
<%= f.select(:city_race_id, #city_races.map {|n| [n.office, n.id]}, {include_blank: "Select something"}, { :class => 'form-control' }) %>
select helper takes two options hashes, one for select, and the second for html options. you can also add prompt option.

Cannot add styling to Select field in rails

I am using twitter-bootstrap in my rails app.
<%= f.select :rating, options_for_select([["Good","1"],["Average","2"],["Poor","3"]]), :class => "form-control" %>
But the form-control class is not working in select field
please help me to get the style working.
Thanks.
If you look at the docs, the class key is part of the last argument, which is a hash. Before that there's an options hash:
f.select :rating, options_for_select([["Good","1"],["Average","2"],["Poor","3"]]), {}, class: "form-control"
Here's an example using the options hash:
f.select :rating, options_for_select(...), { include_blank: "Choose" }, class: "foo"

Rails collection_select and Foreign_key

Following is my code,
<%= f.collection_select :event_member_id, EventMember.all, :id, :company_member_id, :prompt => "Please select" %>
Here :company_member_id is foreign_key in EventMember class, i wanted to display text_method as company_member_email instead of company_member_id,
So my collection_select should be like below,
<%= f.collection_select :event_member_id, EventMember.all, :id, :'company_member.email', :prompt => "Please select" %>
How to achieve above...!!!
In EventMember modal :-
def company_member_email
return self.company_member.email unless self.company_member.nil?
""
end
And the collection as:-
<%= f.collection_select :event_member_id, EventMember.all, :id, :company_member_email, :prompt => "Please select" %>

Rails - how to show two attributes in a collection select form?

I have a collection_select form with the following code:
<%= f.collection_select(:city_id, City.order('name ASC'), :id, :name, {:prompt => "Select a City"}, {:id => 'cities_select'}) %>
Right now it just lists all of the cities by name in my database, but there are duplicate city names. For example, there are two "Peoria" cities.
So, I want to show the state attribute separate by a comma like: "Peoria, IL" to help distinguish the cities. How do I write this code within the form code?
You can also use collection_select for that
f.collection_select :city_id, City.order('name ASC'), :id, :name_with_state, { prompt: 'Select a City' }, { id: 'cities_select' }
name_with_state should be in your city model
def name_with_state
"#{name}, #{state}"
end
f.select :city_id, City.all.map{|c| ["#{c.name}, #{c.state}", c.id] }, { :prompt => "Select City" }
You can use select:
<%= f.select(:city_id, City.order('name ASC').map{ |city| [city.your_method, city.id]},
{:prompt => "Select a City"}, {:id => 'cities_select'}) %>

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

Resources