module FormOptionsHelper
Ruby on Rails 6.0.6
Since v2.2.3Provides a number of methods for turning different kinds of containers into a set of option tags.
The collection_select, select and time_zone_select methods take an options parameter, a hash:
-
: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.select("post", "category", Post::CATEGORIES, { include_blank: true })
could become:
<select name="post[category]" id="post_category"> <option value=""></option> <option value="joke">joke</option> <option value="poem">poem</option> </select>
Another common case is a select tag for a
belongs_to-associated object.Example with
@post.person_id => 2:select("post", "person_id", Person.all.collect { |p| [ p.name, p.id ] }, { include_blank: 'None' })
could become:
<select name="post[person_id]" id="post_person_id"> <option value="">None</option> <option value="1">David</option> <option value="2" selected="selected">Eileen</option> <option value="3">Rafael</option> </select>
-
:prompt- set to true or a prompt string. When the select element doesn’t have a value yet, this prepends an option with a generic prompt – “Please select” – or the given prompt string.select("post", "person_id", Person.all.collect { |p| [ p.name, p.id ] }, { prompt: 'Select Person' })
could become:
<select name="post[person_id]" id="post_person_id"> <option value="">Select Person</option> <option value="1">David</option> <option value="2">Eileen</option> <option value="3">Rafael</option> </select>
-
:index- like the other form helpers,selectcan accept an:indexoption to manually set the ID used in the resulting output. Unlike other helpers,selectexpects this option to be in thehtml_optionsparameter.select("album[]", "genre", %w[rap rock country], {}, { index: nil })
becomes:
<select name="album[][genre]" id="album__genre"> <option value="rap">rap</option> <option value="rock">rock</option> <option value="country">country</option> </select>
-
:disabled- can be a single value or an array of values that will be disabled options in the final output.select("post", "category", Post::CATEGORIES, { disabled: 'restricted' })
could become:
<select name="post[category]" id="post_category"> <option value=""></option> <option value="joke">joke</option> <option value="poem">poem</option> <option disabled="disabled" value="restricted">restricted</option> </select>
When used with the
collection_selecthelper,:disabledcan also be a Proc that identifies those options that should be disabled.collection_select(:post, :category_id, Category.all, :id, :name, { disabled: -> (category) { category.archived? } })
If the categories “2008 stuff” and “Christmas” return true when the method
archived?is called, this would return:<select name="post[category_id]" id="post_category_id"> <option value="1" disabled="disabled">2008 stuff</option> <option value="2" disabled="disabled">Christmas</option> <option value="3">Jokes</option> <option value="4">Poems</option> </select>
Includes
Methods (defined here)
Private methods
(8)
Implementation detail — not part of the public API.
Methods (inherited)
From ActionView::Helpers::TextHelper (11)
- # concat
- # current_cycle
- # cycle
- # excerpt
- # highlight
- # pluralize
- # reset_cycle
- # safe_concat
- # simple_format
- # truncate
- # word_wrap
From ActionView::Helpers::OutputSafetyHelper (3)
- # raw
- # safe_join
- # to_sentence
From ActionView::Helpers::SanitizeHelper (4)
- # sanitize
- # sanitize_css
- # strip_links
- # strip_tags
From ActionView::Helpers::TagHelper (4)
- # cdata_section
- # content_tag
- # escape_once
- # tag
From ActiveSupport::Concern (2)
From ActionView::Helpers::CaptureHelper (4)
- # capture
- # content_for
- # content_for?
- # provide