f.country_select options default country - ruby-on-rails

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'}) %>

Related

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

Rails collection_select with multiple and prompt options

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?

How to use order in collection method in rails

Here is my code i am not able to sort the name by first_name
<%= f.collection_select :donation_id, #donation_array,
:id, :dropdown_display,:order => ("first_name DESC"),
:prompt => "Select Donation Detail", :include_blank => true %>
You cannot order it like this, it is done when you are pulling donation_array like this
#donation_array = Donation.order('first_name DESC').all
and create your collection like this
<%= f.collection_select :donation_id, #donation_array,
:id, :dropdown_display,
:prompt => "Select Donation Detail", :include_blank => true %>

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 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.

Resources