Match an option in select form with boolean values - ruby-on-rails

I have the following piece of code
%br
= f.label :active, 'Status'
= f.select :active, ['Active','Inactive']
Symbol :active is a boolean type var. How can i match Active => 1/True, and Inactive => 0/False , for the database adding.
Sorry for the newbie question, but i can't figure it out.

You can provide a pair of values for each options: first will be used as label (inner text of <option> tag), second will be used as a value attribute:
= f.select :active, [['Active', true], ['Inactive', false]]
It'll render something like:
<select name="model[active]">
<option value="true">Active</option>
<option value="false">Inactive</option>
</select>
Have a look at the docs for select and options_for_select.

A small extension of the earlier answer, if you're using a dropdown.
I needed to use "options_for_select." Also, ":selected" stores the value for the next time you return to the form.
<%= f.select(:active, options_for_select([['Active', true], ['Inactive', false]], {:selected => #symbol.active}),:prompt => "Select") %>

Related

include blank : true not working in select rails

<%= select :used_car, :car_make_id, #all_makes.map { |m| [m.title, m.name] }, {:include_blank => true,:prompt=>"Select Make"}%>
<%= select :used_car, :car_model_id, [], {:include_blank => true,:prompt=>"Select Model")}, size: 10, :class=>"form-control select"%>
Car models list updating on basis on car make selection
Issue is that when I select car make, prompt of car model remove and first option come
how i resolve it?
Here is the documentation.
http://api.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html#method-i-select
http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-select
Try f.select :car_model_id, ... instead. Also try using nil for choices, or [[]] for choices.
But first, you have an extra stray parenthesis ')' in this line:
<%= select :used_car, :car_model_id, [], {:include_blank => true,:prompt=>"Select Model" ) }
You have to remove that parenthesis. It doesn't go with anything.

Rails Multiple Select boxes: Injecting default values from params

I currently have a multiple select box in a rails form that looks like this:
= select_tag :in_all_tags, options_from_collection_for_select(Tag.where(:project_id => #project.id), :id, :name, #in_all_tags_param), { :id => "tags", :tabindex => "3", "data-placeholder" => "Choose Tags", :multiple => "multiple" }
Where
#in_all_tags_param = params[:in_all_tags]
The problem is, #in_all_tags_param will only populate the select form with the last value from params[:in_all_tags]. So, if the url string reads in_all_tags=5&in_all_tags=8, the pre-selected value in the multiple select will only be 8.
From what I understand, the way around this is to append [] to the field name for multiple params, so that :in_all_tags becomes in_all_tags[]
BUT, when I try this, submitting the form returns:
Expected type :default in params[:in_all_tags], got Array
Any suggestions appreciated.
Cheers...
You need to add a :name element to the same hash with :multiple => true in it. So I use something similar for Genres on an app for mine and I do { :multiple => true, :name => "lesson[genre_ids][]" }. The name has to be model[attribute][].

Using select method in form_for in rails

I would like to use a method inside a form_for in rails to create a select tag with options where the value of the options come from one array and the options enclosed by the option tags come from another array.
For example, the first option would be:
<option value = Array1[0]> Array2[0] </option>
How do I do this? Can I use 'select' such as:
= form_for #activity do |f|
= f.select(Array1, Array2, {:selected => nil, :prompt => 'Select Stage'})
I couldn't get something like this working, even though this format seemed consistent with options_for_select as described in the rails API at api.rubyonrails.org.
Try this:
= f.select(:method, Array2.zip(Array1), { :selected => nil, :prompt => 'Select Stage' })
The zip method will combine two arrays to make one two-dimensional array.
So, for example, [1,2,3].zip([4,5,6]) will return [[1,4], [2,5], [3,6]].
select can then interpret this as a list of option texts and option values.
Given [['Male', 'm'], ['Female', 'f']], select will return
<option value="m">Male</option>
<option value="f">Female</option>

Set Selected Value on Dropdown Based on Model Property

i have a simple dropdown like :
<%= collection_select("user_cities", "city_id", current_user.cities, :id, :name ) %>
current_user.cities is an array of the user cities. Each city has a field named "is_primary" and only one city has it set as true.
My question is, how can i make the above collection_select(or transform it if needed), so that it picks the selected option, based on City.is_primary ?
From the docs:
By default, post.person_id [in your case user_cities.city_id] is the selected option. Specify :selected => value to use a different selection or :selected => nil to leave all options unselected.
Armed with that knowledge we can pass the appropriate option to collection_select:
<%= collection_select "user_cities", "city_id", current_user.cities, :id, :name,
:selected => current_user.cities.detect(&:is_primary).id
%>
collection_select("user_cities", "city_id", current_user.cities, :id, :name,{:selected => current_user.cities.where(:is_primary => 1)})
I would start by defining a method called primary_city in your User model.
def primary_city
cities.where(:is_primary => true).first
end
Then,
<%= collection_select("user_cities", "city_id", current_user.cities, :id, :name, { :selected=> current_user.primary_city.id } ) %>

collection_select set option value of :include_blank to zero

In my rails app, i have a drop down box where i retrieve all groups from the Group table and display them using the collection_select tag.
When the user selects 'None', I want to pass '0' as option value.
Currently, an empty string is passed.
Is there a way to include option value = 0 for 'None'?
<%= f.collection_select :SUB_GROUP, Group.all, :Group_ID, :Group_ID, :include_blank => 'None' %>
Many many thanks for any suggestion provided
If you use options_for_select in combination with select_tag you can achieve that using this:
options_for_select(
[['None', '0']].concat(
Group.all.collect { |g| [g.group_id.to_s, g.group_id.to_s] }
)
)
In order to keep your views uncluttered, you might want to generalize and move this into a helper method with a reasonable name.

Resources