How do I set the HTML options for collection_select in Rails?
Many Rails helpers take multiple hash arguments. The first is usually the options to control the helper itself, and the second is the html_options where you specifiy custom ids, classes etc.
The method definition looks like this:
collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {})
You'll notice the multiple '= {}' in the params list. To use this, the first set of options that you would specify must actually be enclosed in braces:
collection_select(:user, :title, UserTitle.all, :id, :name, {:prompt=>true}, {:class=>'my-custom-class'})
If you don't have any options to specify besides the html class, then just put an empty hash placeholder:
collection_select(:user, :title, UserTitle.all, :id, :name, {}, {:class=>'my-custom-class'})
Additional API documentation is available at:
http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/collection_select
Add class to collection_select on Ruby On Rails formulary
You're doing it right, but you need to separate the hashes:
<%= f.collection_select :id, TipusProducte.order(:nom),:id ,:nom, { include_blank: false }, { class: "btn btn-secondary dropdown-toggle" } %>
Otherwise, it gets interpreted as one hash. The function spec has two object hashes as arguments; the first is for collection options, the second for HTML options.
Rails' collection_select helper method and the Create item option at the end
You should probably use select
instead.
Like so:
f.select(:category_id, @categories.collect {|p| [ p.name, p.id ] } + [ [ 'Or create a new one', 'new' ] ], {:include_blank => 'Please select a category'})
Good luck!
Change the option selected on collection_select
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' } %>
Rails, collection select style
...what I want is to have a whole div inside the option tag
As far as I know, it is invalid to have any other html tags inside of an option tag.
Also, I would like the options to be listed, rather than shown in a
dropdown. Any ideas on how to accomplish that
Select tag in html has a size attribute, eg which shows the first n options in the select tag. You could write some code in rails to make the size attribute equal to the count of the collection you are displaying in the drop down list.
For example, you could add { size: @meal.meal_sizes.count }
to the html options hash of the select tag.
<%= f.collection_select(:meal_size_id, @meal.meal_sizes, :id, :name, {:prompt => false}, { class: "form-control", size: @meal.meal_sizes.count }) %>
Rails: How to add custom data-attributes in collection_select
Is this what you want?
= f.select :parallax_id, options_for_select(@parallax.map {|p| [p.title, p.id, {'data-icon' => p.image }]})
Rails 4: collection_select not inserting 'class' attribute?
Try using :
<%= f.collection_select :category, Category.all, :id, :name, {prompt: "Select a category"}, {class: "form-control"} %>
According to the rails documentation, first comes options and then html options. Remember that html options need to be in braces: {prompt: "Select a category"}
or {class: "form-control"}
.
Adding extra html attributes to a Rails collection_select
Not sure if it's possible using collection_select, but I think using select does what you want:
<%= f.select :country_id, Country.all.map {|c| [c.name, c.id, {:'prov-val' => c.prov_val, :'code-val' => c.code_val}]} %>
This assumes that your country object has the prov_val and code_val fields.
Related Topics
Making a Paragraph in HTML Contain a Text from a File
How to Detect HTML 5 Compatibility in Browser
How to Make Responsive Website for All Devices
How to Write Equations in HTML
In HTML5, Can the <Header> and <Footer> Tags Appear Outside of the <Body> Tag
If an HTML Form Has Two <Input Type="Submit"> Buttons, How to Know Which Got Clicked
Rounded Corners in Outlook Without Images
Setting the Granularity of the HTML5 Audio Event 'Timeupdate'
Why Is "" Being Injected into My HTML
Can Div with Contenteditable=True Be Passed Through Form
CSS Display None and Opacity Animation with Keyframes Not Working
How to Use the Same Meta Tag for Opengraph and Schema.Org
How to Put a <Style>...</Style> Tag Within the Body of a HTML File to Send in Email
How to Add Div Inside Table Above Every <Tr>
Any Way of Using Frames in HTML5
How to Change Content on Hover