Select ActionView::FormHelper html_options not being set - ruby-on-rails

I currently have the following in my application and I am simply trying to apply a class to the select as shown below
<%= f.select :widget_id, options_from_collection_for_select(#widgets, "id", "name"), html_options: {class: 'form-control'} %>
According to the select helper on API dock it shows it should be set up as followed:
select(object, method, choices, options = {}, html_options = {}) public
My problem is that for some reason when I inspect the element I see:
<select id="user_widget_id" name="user[widget_id]">
I don't understand why the class is not being included.

Try this:
<%= f.select :widget_id, options_from_collection_for_select(#widgets, "id", "name"), {}, {class: 'form-control'} %>
The third parameters is for options, you want to pass your html_options in the 4th parameters, and no need to actually declare "html_options" too. Hope it helps !
Official doc: select(object, method, choices, options = {}, html_options = {})

Related

Rails form helper select when adding custom css class

This question comes up all the time, which probably points to a design flaw (#dhh)
WHy does this work:
<%= form.select :category_id, category_select_collection, {}, html_options = { class: "form-select"} %>
but this does not work:
<%= form.select :category_id, category_select_collection, html_options = { class: "form-select"} %>
I bet here's some subtlety about parameter ordering, named parameters, defaults and so on that I am tripping on. But I don't see where my understanding is incorrect.
You have a parameter ordering problem. form.select looks like this:
select(method, choices = nil, options = {}, html_options = {}, &block)
Wraps ActionView::Helpers::FormOptionsHelper#select for form builders:
The third argument are the options, the fourth are the HTML options so you want your { class: ... } in the fourth argument.
You might also be misinterpreting what html_options = { class: "form-select"} means. That's an assignment to the local variable html_options inside the arguments to the select method. An assignment is an expression that evaluates to the RHS so it still works in the first case but you could say:
<%= form.select :category_id, category_select_collection, {}, any_local_variable_name = { class: "form-select"} %>
and achieve the same result (assuming of course that you're not using the html_options variable later on).

Multiple attribute in collection_select in Rails

I am trying to allow for multiple value selection from a collection in a Rails form. The field is working but does not allow for multiple selections (once an alternative option is selected the previously selected is unselected). I am using Bootstrap CDN, which I don't presume is causing issues, but I may be wrong?
Can you see anything wrong with this code?
<div class="field form-group row">
<%= f.label :industry_ids, class:"col-sm-3"%>
<%= f.collection_select(:industry_ids, Industry.all, :id, :name, {:multiple => true}, size: Industry.all.length) %>
</div>
Thanks for your help.
I believe your problem is that you're putting {:multiple => true} in the wrong options hash. The method signature for collection_select looks like this:
collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {})
multiple is an html attribute of the select tag itself (here's a doc), so you want to pass that to html_options, not options. Your code looks like this:
f.collection_select(:industry_ids, Industry.all, :id, :name, {:multiple => true}, size: Industry.all.length)
Where Industry.all is the collection, :id is the value_method, and :name is the text_method, which means { :multiple => true } is getting passed to options, not html_options. Move it to the second hash and you should be fine.

ruby on rails - drop down list from model

I would like to create a drop down list with Ruby on rails from the model "Company" which has a item call "name". I would like to length of the dropdown list to be as long as Company.count (dynamic)
For example for 3 element in "Company":
<%= f.select :company_brand, [[Company.find(1).name, Company.find(1).id],[Company.find(2).name, Company.find(2).id],[Company.find(3).name, Company.find(3).id]]%>
collection_select (documentation) will provide what you need:
collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {})
Returns and tags for the collection of existing return values of method for object's class. The value returned from calling method on the instance object will be selected. If calling method returns nil, no selection is made without including :prompt or :include_blank in the options hash.
The :value_method and :text_method parameters are methods to be called on each member of collection. The return values are used as the value attribute and contents of each tag, respectively. They can also be any object that responds to call, such as a proc, that will be called for each member of the collection to retrieve the value/text.
For your use case, this would mean changing the code to:
<%= f.collection_select(:company_brand, Company.all, :id, :name) %>
You can do like this:
<%= select(:company_brand, Company.all.collect {|c| [ c.name, c.id ] }, { include_blank: true }) %>
#League - form.html.erb
<%= f.collection_select(:game_id, Game.order(:title), :id, :title, {prompt: true}, {class: 'form-control col-md-7 col-xs-12', required: "required"})%>
#.html_output
<select class="form-control col-md-7 col-xs-12" required="required" name="league[game_id]" id="league_game_id"><option value="">Please select</option>
<option value="2">csgo</option>
<option value="1">dota2</option>
</select>
You can try this i think this will help you.
<%= f.select :company_brand, options_from_collection_for_select(Company.all, "id", "name") %>

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.

Way to define id or class for options_for_select in rails?

how can we define id for this rails select statement , i have tried doing in this way like
<%= f.select :state, options_for_select(Contact::STATES), :id=>"state_job" %>
but it is not showing any id when i inspect it in the browser. Please help me out
<%= f.select :state, options_for_select(Contact::STATES) %>
The select tag helper looks for options, then html_options, you just need to make sure your id is in the right place (html_options) by passing something to the options parameter:
<%= f.select :state, options_for_select(Contact::STATES), {}, {:id=>"state_job"} %>

Resources