instance method
grouped_collection_select
Ruby on Rails 8.1.2
Since v2.3.18Signature
grouped_collection_select(object, method, collection, group_method, group_label_method, option_key_method, option_value_method, options = {}, html_options = {})
Returns <select>, <optgroup> and <option> 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.
Parameters:
-
object- The instance of the class to be used for the select tag -
method- The attribute ofobjectcorresponding to the select tag -
collection- An array of objects representing the<optgroup>tags. -
group_method- The name of a method which, when called on a member ofcollection, returns an array of child objects representing the<option>tags. It can also be any object that responds tocall, such as aproc, that will be called for each member of thecollectionto retrieve the value. -
group_label_method- The name of a method which, when called on a member ofcollection, returns a string to be used as thelabelattribute for its<optgroup>tag. It can also be any object that responds tocall, such as aproc, that will be called for each member of thecollectionto retrieve the label. -
option_key_method- The name of a method which, when called on a child object of a member ofcollection, returns a value to be used as thevalueattribute for its<option>tag. -
option_value_method- The name of a method which, when called on a child object of a member ofcollection, returns a value to be used as the contents of its<option>tag.
Example object structure for use with this method:
# attributes: id, name class Continent < ActiveRecord::Base has_many :countries end # attributes: id, name, continent_id class Country < ActiveRecord::Base belongs_to :continent end # attributes: id, name, country_id class City < ActiveRecord::Base belongs_to :country end
Sample usage:
grouped_collection_select(:city, :country_id, @continents, :countries, :name, :id, :name)
Possible output:
<select name="city[country_id]" id="city_country_id">
<optgroup label="Africa">
<option value="1">South Africa</option>
<option value="3">Somalia</option>
</optgroup>
<optgroup label="Europe">
<option value="7" selected="selected">Denmark</option>
<option value="2">Ireland</option>
</optgroup>
</select>
Parameters
-
objectreq -
methodreq -
collectionreq -
group_methodreq -
group_label_methodreq -
option_key_methodreq -
option_value_methodreq -
optionsopt = {} -
html_optionsopt = {}
Source
# File actionview/lib/action_view/helpers/form_options_helper.rb, line 257
def grouped_collection_select(object, method, collection, group_method, group_label_method, option_key_method, option_value_method, options = {}, html_options = {})
Tags::GroupedCollectionSelect.new(object, method, self, collection, group_method, group_label_method, option_key_method, option_value_method, options, html_options).render
end
Defined in actionview/lib/action_view/helpers/form_options_helper.rb line 257
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActionView::Helpers::FormOptionsHelper