From b59e3d1897fd8701995d9b2dc31b5a4e5c1ebe9d Mon Sep 17 00:00:00 2001 From: Michael Koziarski Date: Sun, 5 Aug 2007 01:13:44 +0000 Subject: Major improvement to the documentation for the options / select form helpers. Closes #9038 [kampers, jardeon, wesg] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7276 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- .../lib/action_view/helpers/form_options_helper.rb | 104 +++++++++++++++------ 1 file changed, 73 insertions(+), 31 deletions(-) (limited to 'actionpack/lib/action_view/helpers/form_options_helper.rb') diff --git a/actionpack/lib/action_view/helpers/form_options_helper.rb b/actionpack/lib/action_view/helpers/form_options_helper.rb index 22db5a918f..f0629ae81b 100644 --- a/actionpack/lib/action_view/helpers/form_options_helper.rb +++ b/actionpack/lib/action_view/helpers/form_options_helper.rb @@ -84,7 +84,36 @@ module ActionView InstanceTag.new(object, method, self, nil, options.delete(:object)).to_select_tag(choices, options, html_options) end - # Return select and option tags for the given object and method using options_from_collection_for_select to generate the list of option tags. + # Returns + # + # + # + # + # def collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {}) InstanceTag.new(object, method, self, nil, options.delete(:object)).to_collection_select_tag(collection, value_method, text_method, options, html_options) end @@ -154,41 +183,54 @@ module ActionView options_for_select(options, selected) end - # Returns a string of option tags, like options_from_collection_for_select, but surrounds them with tags. + # Returns a string of tags, like #options_from_collection_for_select, but + # groups them by tags based on the object relationships of the arguments. # - # An array of group objects are passed. Each group should return an array of options when calling group_method - # Each group should return its name when calling group_label_method. + # Parameters: + # +collection+:: An array of objects representing the tags + # +group_method+:: The name of a method which, when called on a member of +collection+, returns an + # array of child objects representing the tags + # +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 tag + # +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 + # 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 + # tag + # +selected_key+:: A value equal to the +value+ attribute for one of the tags, + # which will have the +selected+ attribute set. Corresponds to the return value + # of one of the calls to +option_key_method+. If +nil+, no selection is made. # - # html_option_groups_from_collection(@continents, "countries", "continent_name", "country_id", "country_name", @selected_country.id) + # Example object structure for use with this method: + # class Continent < ActiveRecord::Base + # has_many :countries + # # attribs: id, name + # end + # class Country < ActiveRecord::Base + # belongs_to :continent + # # attribs: id, name, continent_id + # end # - # Could become: - # - # - # - # ... - # - # - # - # - # - # ... - # + # Sample usage: + # option_groups_from_collection_for_select(@continents, :countries, :name, :id, :name, 3) # - # with objects of the following classes: - # class Continent - # def initialize(p_name, p_countries) @continent_name = p_name; @countries = p_countries; end - # def continent_name() @continent_name; end - # def countries() @countries; end - # end - # class Country - # def initialize(id, name) @id = id; @name = name end - # def country_id() @id; end - # def country_name() @name; end - # end + # Possible output: + # + # + # + # ... + # + # + # + # + # + # ... + # # - # NOTE: Only the option tags are returned, you have to wrap this call in a regular HTML select tag. - def option_groups_from_collection_for_select(collection, group_method, group_label_method, - option_key_method, option_value_method, selected_key = nil) + # Note: Only the and tags are returned, so you still have to + # wrap the output in an appropriate