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") %>
Related
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.
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.
I am writing a form in Rails. I have a collection of users which includes the following user details
name
last_name
id
employee_id
I would like to define the user names to appear as the dropdown display values while their ids are the dropdown values but at the same time I would like to add an additional custom HTML attribute which is data-employee-id and is set to be the same as the employeeId
My current code is as follows:
.user-dropdown
= f.label 'name'
= f.input :id, as: :select, collection: #users, include_blank: }
The html result would be as follows:
<option value='1' data-employee-id='1234'>Alex</option>
<option value='2' data-employee-id='2468'>Bradley</option>
<option value='3' data-employee-id='1357'>Cathy</option>
You can do this using options_for_select
= f.select :id, options_for_select(
#users.map { |u| [u.name, u.id, {'data-employee-id' => u.id}] })
simple form has it's perks so Mihai's answer is close
The simple_form way would be
= f.input :id, collection: #users.map { |u| [u.name, u.id] },
include_blank: true/false
and also you left a closing curly brace without opening it.
<%= form_for(:offer,:url=>{:controller=>'offers',:action=>'combo'}) do |f|%>
<%= f.select :catId_get, options_from_collection_for_select(#categories, "id", "name"), prompt: "Select Category" %>
I am new in rails.I have a dropdown where all categories are there.When i select a category from this dropdown i want to get its category id in my controller,so that i can use that id for it's child dropdown.
Select
Each select option in HTML has two values -- the value and the label:
<select>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
It's only the value which is passed to your controller. This means if you are able to create the select tag in your Rails app with the correct value / label setup, it will pass the correct data you require.
Rails
Here's how I'd handle it:
<%= form_for :offer, offers_combo_path do |f|%>
<%= f.collection_select :cat_id, #categories, :id, :name, prompt: "Select Category" %>
This will pass the following params to your categories_controller:
#app/controllers/categories_controller.rb
class CategoriesController < ApplicationController
def combo
params[:offer][:cat_id]
end
end
Recommendation
I'd actually recommend you use the form_tag helper for this, rather than form_for. Reason being that form_for is mainly for ActiveRecord objects, and although you can use :symbols in the helper, you will really need to use a much less elaborate system
I'd just replace your form_for with the following:
<%= form_tag offer_combo_path do %>
<%= collection_select :cat_id, #categories, :id, :name, prompt: "Select Category" %>
<% end %>
Your id should be accessible by
params[:offer][:catId_get]
in your controller.
I'm trying to select multiple events in the invtime form.
Invtime
has_many :events
This is my form code:
<%= f.select :event_id, Event.all.collect {|x| [x.title, x.id]}, {}, :multiple => true %>
The display looks good! But, when I select the last event and submit, I get:
undefined method `to_i' for ["", "62"]:Array
The page inspection shows:
<select id="invtime_event_id" multiple="multiple" name="invtime[event_id][]">
<option value="66">Meeting</option>
<option value="62">Auto fill some fields on new workorder</option>
</select>
Thanks for the help!
The select helper doesn't know what to do with a collection like that, it assumes the first value is an ID and the second a string, you might be able to just reverse your method calls but this isn't really the right way to create a select tag, instead either use collection_select as dax suggests or use the options_from_collection_for_select helper:
<%= f.select :event_id, options_from_collection_for_select(Event.all, :id, :title), {}, :multiple => true %>
undefined method `to_i' for ["", "62"]:Array
from the docs:
:include_blank - set to true or a prompt string if the first option element of the select element is a blank. Useful if there is not a default value required for the select element.
So try adding include_blank: true
<%= f.select(:event_id, Event.all.collect {|x| [x.title, x.id]}, {}, :multiple => true, include_blank: true ) %>
also, you might try collection_select, where #events = Event.all
<%= f.collection_select(:event_id, #events, :id, :title, include_blank: true ) %>