options_from_collection_for_select wont allow selection by variable - ruby-on-rails

The two snippets of code below, the options_from_collection_for_select works for setting the :selected when i use a set value, but when I use #posts.user_id it fails.
Why is this working?
select_tag "user-dropdown", options_from_collection_for_select(#users, 'id', 'fname', **11**), :class =>'form-control'
But this is not?
select_tag "user-dropdown", options_from_collection_for_select(#users, 'id', 'fname', **#posts.user_id**), :class =>'form-control'

select_tag "user-dropdown", options_from_collection_for_select(#users, 'id', 'fname', **#posts.user_id**), :class =>'form-control'
What is this #posts.user_id you are trying to display in your options ?
select_tag with options_for_select example
An example of using options_for_select with select_tag
select_tag 'user_id', options_for_select(#users.collect{ |u| [u.name, u.id] })
This would generate something like:
<select id="user_id" name="user_id">
<option value="1">Brad</option>
<option value="2">Angie</option>
<option value="3">Jenny</option>
</select>

Related

Change the option selected on collection_select

I'm trying to change the option that is selected on collection_select on my form in rails.
My code look like this:
<%= f.collection_select :course_type_id, CourseType.where(:deleted => false), :id, :name, {}, {class: 'form-control m-b', :selected => #course_template.course_type.name } %>
However the option selected always shows the first one and never changes unless the user selects a different option.
The resulting html looks like this:
<select class="form-control m-b" selected="selected" name="course[course_type_id]" id="course_course_type_id">
<option value="1">Driving</option>
<option value="2">Pratical</option>
</select>
Any ideas on what I'm doing wrong?
It looks like you're putting the :selected key in the html_options argument attributes.
Here is the method definition for collection_select:
collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {})
Try this:
<%= f.collection_select :course_type_id, CourseType.where(:deleted => false), :id, :name, {:selected => #course_template.course_type.name}, {class: 'form-control m-b' } %>
<%= f.collection_select :course_type_id, CourseType.where(:deleted => false), :id, :name, { :selected => #course_template.course_type.id }, {class: 'form-control m-b' } %>
The selected parameter takes the value, and not the name of the option.
From collection_select definition, selected option and html_options are different params.
For further understanding, refer here.

Rails form_for multiple collection_select, values not selected on form failure

I'm not sure why, but my form is not showing the options selected on submit, even though the params hash shows that the information is being returned to the page.
Collection select code:
<%= f.collection_select :post_topic_ids, PostTopic.all, :id, :name, {}, { multiple: true, class: 'form-control' } %>
Which renders:
<select multiple="multiple" class="form-control" name="post[post_topic_ids][]" id="post_post_topic_ids">
<option value="1">Psychology</option>
<option value="2">Engineering</option>
<option value="3">Nanotechnology</option>
</select>
Params returned after form validation error
params = {"post"=>{"post_topic_ids"=>["", "1"]}}
Update
I have also tried:
<%= select_tag 'post_topic_ids', options_for_select(PostTopic.all.collect{ |p| [p.name, p.id] }), multiple: true %>
and:
<%= select_tag 'post_topic_ids', options_from_collection_for_select(PostTopic.all, "id", "name"), multiple: true %>
Which renders:
<select name="post_topic_ids[]" id="post_topic_ids" multiple="multiple"><option value="1">Psychology</option>
<option value="2">Engineering</option>
<option value="3">Nanotechnology</option></select>
you need to specify which element is selected a third parameter
<%= select_tag 'post_topic_ids', options_for_select(PostTopic.all.collect{ |p| [p.name, p.id] }, --->selected_element<---), multiple: true %>
look at http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/options_for_select for some examples.

How can we set selected option in select_tag with option_for_select in ruby

I have a select_tag in ruby on rails. The syntax for this is,
<%= select_tag "iso_region", options_for_select(#all_regions.collect {|p| [ "#{p['cc']}-#{p['lr']}", p['cc'] ] }), class: "form-control selectpicker reg_name", :data => {:'live-search' => 'true'} %>
Sample Options generated is like this,
<option value="ET">ET-Africa</option>
<option value="NG">NG-Africa</option>
<option value="PG">PG-Pacific</option>
<option value="IT">IT-Europe</option>
And I want IT-Europe to be selected in the dropdown.
How can I do that with my select_tag?
Try this:
options_for_select(#all_regions.collect {|p| [ "#{p['cc']}-#{p['lr']}", p['cc'] ] }, "IT"),
The last arg you pass to options_for_select is the value to be marked selected when it's rendered.

How to add selected attribute in f.select helper in rails?

i have written the f.select helper for this code
<select>
<option>abc</option>
<option>def</option>
<option selected>ghi</option>
</select>
<%= f.select(:xFields, ['abc', 'def', 'ghi]) %>
How do i make option 'ghi' selected as default?
edit
try this:
<%= f.select options_for_select(:xFields, ['abc', 'def', 'ghi'], 'ghi') %>
the following should work
<%= f.select(:xFields, [['ghi'], 'abc', 'def']) %>
based on Rails 3: f.select - options_for_select
This is working
<%= f.select(:xFields, options_for_select( ['abc', 'def', 'ghi'], 'ghi')) %>

Add value to select in rails

i have a select that take values from database.
<%= select_tag :location, options_from_collection_for_select(Country.all, :id, :country_name), { :class => 'selectpicker' } %>
So i get all countries from database.
How i can do for add a custom value(for example Any, with value 0), to this select list taken from database ?
For example now i have:
<select>
<option value="UK">UK</option>
</select>
and i want get this:
<select>
<option value="0">Any</option>
<option value="UK">UK</option>
</select>
Thanks.
You can use the either prompt or include_blank options (FormOptionsHelper) as follows:
<%= select_tag :location, options_from_collection_for_select(Country.all, :id, :country_name), :prompt => 'Any', :class => 'selectpicker' %>

Resources