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