grouped_collection_select
- 1.0.0
- 1.1.6
- 1.2.6
- 2.0.3
- 2.1.0
- 2.2.1
- 2.3.8 (0)
- 3.0.0 (0)
- 3.0.9 (-11)
- 3.1.0 (0)
- 3.2.1 (0)
- 3.2.8 (0)
- 3.2.13 (0)
- 4.0.2 (1)
- 4.1.8 (0)
- 4.2.1 (0)
- 4.2.7 (0)
- 4.2.9 (0)
- 5.0.0.1 (3)
- 5.1.7 (0)
- 5.2.3 (38)
- 6.0.0 (0)
- 6.1.3.1 (0)
- 6.1.7.7 (0)
- 7.0.0 (0)
- 7.1.3.2 (0)
- 7.1.3.4 (0)
- What's this?
grouped_collection_select(object, method, collection, group_method, group_label_method, option_key_method, option_value_method, options = {}, html_options = {})
public
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 of object corresponding 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 of collection, returns an array of child objects representing the <option> tags. It 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.
-
group_label_method - The name of a method which, when called on a member of collection, returns a string to be used as the label attribute for its <optgroup> tag. It 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 label.
-
option_key_method - The name of a method which, when called on a child object of a member of collection, returns a value to be used as the value attribute for its <option> tag.
-
option_value_method - The name of a method which, when called on a child object of a member of collection, 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>
Group method chain
The group_method parameter can be a string representing a method chain:
grouped_collection_select(:city, :country_id, @continents, 'countries.sort.reverse', :name, :id, :name)
If we were to modify the Country model so we can sort by name:
class Country include Comparable def <=>(other) self.name <=> other.name end end
The above example would have given us the countries sorted by name in descending sequence.
Preselecting options
To preselect options, pass in the selected options in the options hash:
{ :selected => [ selected_option_1, selected__option_2, ... ] }
Code example
grouped_collection_select(:city, :country_id, @continents, :countries, :name, :id, :name, { :selected => [1, 5, 6 ] } )