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

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

Related

Getting value of an object in collection select

I have created drop down list for employees.
What I want?
I want to select full Name for each one of them.
Type of form:
I use simple_form.
I actually have:
= f.input :person_id, label: "Employee", collection: #employee, prompt: "Select employee"
Result(I know, that is reference):
Before I use collection_select, but simple_form doesn't support validation for this type of collection.
Code for collection_select. This type of drop down list displays properly full name.
= f.collection_select :person_id, #employee, :id, :fullName, {prompt: "Wybierz pracownika"}, {class: "form-control"}
Update:
fullName is a method in a person.rb model.
def fullName
"#{first_name} #{last_name}"
end
Object employee.
#employee = Person.where.not(type: "Client")
The easiest way to do this is :
= f.select :person_id, options_for_select(#employees.map{|e| [e.fullName, e.id]}), {:prompt=>"Wybierz pracownika", :required => true}
It will show full names as select options and will dispatch ids as values with form.
You follow this code like below:
<%= f.collection_select(:person_id, Model.all, :person_id, :fullName,{:prompt=>"Wybierz pracownika"}, {:class => 'form-control'}) %>
You will replace your model_name.
Or
= f.input :person_id, label: "Employee", collection: #employee.fullName, prompt: "Select employee"
I think will help you

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

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

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