diff options
Diffstat (limited to 'actionpack')
-rw-r--r-- | actionpack/lib/action_dispatch/http/response.rb | 12 | ||||
-rw-r--r-- | actionpack/lib/action_view/helpers/date_helper.rb | 160 | ||||
-rw-r--r-- | actionpack/lib/action_view/helpers/form_options_helper.rb | 310 | ||||
-rw-r--r-- | actionpack/lib/action_view/template.rb | 2 |
4 files changed, 62 insertions, 422 deletions
diff --git a/actionpack/lib/action_dispatch/http/response.rb b/actionpack/lib/action_dispatch/http/response.rb index 845fa2ec1f..e7e8905d7e 100644 --- a/actionpack/lib/action_dispatch/http/response.rb +++ b/actionpack/lib/action_dispatch/http/response.rb @@ -137,7 +137,7 @@ module ActionDispatch # :nodoc: @committed end - # Allows you to set the HTTP status code + # Sets the HTTP status code. def status=(status) @status = Rack::Utils.status_code(status) end @@ -146,22 +146,24 @@ module ActionDispatch # :nodoc: @content_type = content_type.to_s end - # The response code of the request + # The response code of the request. def response_code @status end - # Returns a String to ensure compatibility with Net::HTTPResponse + # Returns a string to ensure compatibility with <tt>Net::HTTPResponse</tt>. def code @status.to_s end - # Returns the corresponding message for the current HTTP status code + # Returns the corresponding message for the current HTTP status code: + # # response.status = 200 # response.message # => "OK" # # response.status = 404 # response.message # => "Not Found" + # def message Rack::Utils::HTTP_STATUS_CODES[@status] end @@ -179,7 +181,7 @@ module ActionDispatch # :nodoc: stream.to_path end - # Returns the content of the response as a String. This contains the contents + # Returns the content of the response as a string. This contains the contents # of any calls to <tt>render</tt>. def body strings = [] diff --git a/actionpack/lib/action_view/helpers/date_helper.rb b/actionpack/lib/action_view/helpers/date_helper.rb index 6dd0abf89b..f4245ec9ba 100644 --- a/actionpack/lib/action_view/helpers/date_helper.rb +++ b/actionpack/lib/action_view/helpers/date_helper.rb @@ -1034,162 +1034,38 @@ module ActionView end class FormBuilder - - # Returns a set of select tags (one for year, month, and day) pre-selected for accessing a specified date-based - # attribute (identified by +method+) on an object assigned to the template (identified by +object+). - # - # ==== Options - # * <tt>:use_month_numbers</tt> - Set to true if you want to use month numbers rather than month names (e.g. - # "2" instead of "February"). - # * <tt>:use_two_digit_numbers</tt> - Set to true if you want to display two digit month and day numbers (e.g. - # "02" instead of "February" and "08" instead of "8"). - # * <tt>:use_short_month</tt> - Set to true if you want to use abbreviated month names instead of full - # month names (e.g. "Feb" instead of "February"). - # * <tt>:add_month_numbers</tt> - Set to true if you want to use both month numbers and month names (e.g. - # "2 - February" instead of "February"). - # * <tt>:use_month_names</tt> - Set to an array with 12 month names if you want to customize month names. - # Note: You can also use Rails' i18n functionality for this. - # * <tt>:date_separator</tt> - Specifies a string to separate the date fields. Default is "" (i.e. nothing). - # * <tt>:start_year</tt> - Set the start year for the year select. Default is <tt>Time.now.year - 5</tt>. - # * <tt>:end_year</tt> - Set the end year for the year select. Default is <tt>Time.now.year + 5</tt>. - # * <tt>:discard_day</tt> - Set to true if you don't want to show a day select. This includes the day - # as a hidden field instead of showing a select field. Also note that this implicitly sets the day to be the - # first of the given month in order to not create invalid dates like 31 February. - # * <tt>:discard_month</tt> - Set to true if you don't want to show a month select. This includes the month - # as a hidden field instead of showing a select field. Also note that this implicitly sets :discard_day to true. - # * <tt>:discard_year</tt> - Set to true if you don't want to show a year select. This includes the year - # as a hidden field instead of showing a select field. - # * <tt>:order</tt> - Set to an array containing <tt>:day</tt>, <tt>:month</tt> and <tt>:year</tt> to - # customize the order in which the select fields are shown. If you leave out any of the symbols, the respective - # select will not be shown (like when you set <tt>discard_xxx: true</tt>. Defaults to the order defined in - # the respective locale (e.g. [:year, :month, :day] in the en locale that ships with Rails). - # * <tt>:include_blank</tt> - Include a blank option in every select field so it's possible to set empty - # dates. - # * <tt>:default</tt> - Set a default date if the affected date isn't set or is nil. - # * <tt>:disabled</tt> - Set to true if you want show the select fields as disabled. - # * <tt>:prompt</tt> - Set to true (for a generic prompt), a prompt string or a hash of prompt strings - # for <tt>:year</tt>, <tt>:month</tt>, <tt>:day</tt>, <tt>:hour</tt>, <tt>:minute</tt> and <tt>:second</tt>. - # Setting this option prepends a select option with a generic prompt (Day, Month, Year, Hour, Minute, Seconds) - # or the given prompt string. - # * <tt>:with_css_classes</tt> - Set to true if you want assign different styles for 'select' tags. This option - # automatically set classes 'year', 'month', 'day', 'hour', 'minute' and 'second' for your 'select' tags. - # - # If anything is passed in the +html_options+ hash it will be applied to every select tag in the set. - # - # NOTE: Discarded selects will default to 1. So if no month select is available, January will be assumed. - # - # # Generates a date select that when POSTed is stored in the article variable, in the written_on attribute. - # date_select("article", "written_on") - # - # # Generates a date select that when POSTed is stored in the article variable, in the written_on attribute, - # # with the year in the year drop down box starting at 1995. - # date_select("article", "written_on", start_year: 1995) - # - # # Generates a date select that when POSTed is stored in the article variable, in the written_on attribute, - # # with the year in the year drop down box starting at 1995, numbers used for months instead of words, - # # and without a day select box. - # date_select("article", "written_on", start_year: 1995, use_month_numbers: true, - # discard_day: true, include_blank: true) - # - # # Generates a date select that when POSTed is stored in the article variable, in the written_on attribute, - # # with two digit numbers used for months and days. - # date_select("article", "written_on", use_two_digit_numbers: true) - # - # # Generates a date select that when POSTed is stored in the article variable, in the written_on attribute - # # with the fields ordered as day, month, year rather than month, day, year. - # date_select("article", "written_on", order: [:day, :month, :year]) - # - # # Generates a date select that when POSTed is stored in the user variable, in the birthday attribute - # # lacking a year field. - # date_select("user", "birthday", order: [:month, :day]) - # - # # Generates a date select that when POSTed is stored in the article variable, in the written_on attribute - # # which is initially set to the date 3 days from the current date - # date_select("article", "written_on", default: 3.days.from_now) - # - # # Generates a date select that when POSTed is stored in the credit_card variable, in the bill_due attribute - # # that will have a default day of 20. - # date_select("credit_card", "bill_due", default: { day: 20 }) + # Wraps ActionView::Helpers::DateHelper#date_select for form builders: # - # # Generates a date select with custom prompts. - # date_select("article", "written_on", prompt: { day: 'Select day', month: 'Select month', year: 'Select year' }) - # - # The selects are prepared for multi-parameter assignment to an Active Record object. + # <%= form_for @person do |f| %> + # <%= f.date_select :birth_date %> + # <%= f.submit %> + # <% end %> # - # Note: If the day is not included as an option but the month is, the day will be set to the 1st to ensure that - # all month choices are valid. + # Please refer to the documentation of the base helper for details. def date_select(method, options = {}, html_options = {}) @template.date_select(@object_name, method, objectify_options(options), html_options) end - # Returns a set of select tags (one for hour, minute and optionally second) pre-selected for accessing a - # specified time-based attribute (identified by +method+) on an object assigned to the template (identified by - # +object+). You can include the seconds with <tt>:include_seconds</tt>. You can get hours in the AM/PM format - # with <tt>:ampm</tt> option. - # - # This method will also generate 3 input hidden tags, for the actual year, month and day unless the option - # <tt>:ignore_date</tt> is set to +true+. If you set the <tt>:ignore_date</tt> to +true+, you must have a - # +date_select+ on the same method within the form otherwise an exception will be raised. - # - # If anything is passed in the html_options hash it will be applied to every select tag in the set. - # - # # Creates a time select tag that, when POSTed, will be stored in the article variable in the sunrise attribute. - # time_select("article", "sunrise") - # - # # Creates a time select tag with a seconds field that, when POSTed, will be stored in the article variables in - # # the sunrise attribute. - # time_select("article", "start_time", include_seconds: true) - # - # # You can set the <tt>:minute_step</tt> to 15 which will give you: 00, 15, 30 and 45. - # time_select 'game', 'game_time', {minute_step: 15} - # - # # Creates a time select tag with a custom prompt. Use <tt>prompt: true</tt> for generic prompts. - # time_select("article", "written_on", prompt: {hour: 'Choose hour', minute: 'Choose minute', second: 'Choose seconds'}) - # time_select("article", "written_on", prompt: {hour: true}) # generic prompt for hours - # time_select("article", "written_on", prompt: true) # generic prompts for all - # - # # You can set :ampm option to true which will show the hours as: 12 PM, 01 AM .. 11 PM. - # time_select 'game', 'game_time', {ampm: true} + # Wraps ActionView::Helpers::DateHelper#time_select for form builders: # - # The selects are prepared for multi-parameter assignment to an Active Record object. + # <%= form_for @race do |f| %> + # <%= f.time_select :average_lap %> + # <%= f.submit %> + # <% end %> # - # Note: If the day is not included as an option but the month is, the day will be set to the 1st to ensure that - # all month choices are valid. + # Please refer to the documentation of the base helper for details. def time_select(method, options = {}, html_options = {}) @template.time_select(@object_name, method, objectify_options(options), html_options) end - # Returns a set of select tags (one for year, month, day, hour, and minute) pre-selected for accessing a - # specified datetime-based attribute (identified by +method+) on an object assigned to the template (identified - # by +object+). - # - # If anything is passed in the html_options hash it will be applied to every select tag in the set. - # - # # Generates a datetime select that, when POSTed, will be stored in the article variable in the written_on - # # attribute. - # datetime_select("article", "written_on") - # - # # Generates a datetime select with a year select that starts at 1995 that, when POSTed, will be stored in the - # # article variable in the written_on attribute. - # datetime_select("article", "written_on", start_year: 1995) - # - # # Generates a datetime select with a default value of 3 days from the current time that, when POSTed, will - # # be stored in the trip variable in the departing attribute. - # datetime_select("trip", "departing", default: 3.days.from_now) - # - # # Generate a datetime select with hours in the AM/PM format - # datetime_select("article", "written_on", ampm: true) - # - # # Generates a datetime select that discards the type that, when POSTed, will be stored in the article variable - # # as the written_on attribute. - # datetime_select("article", "written_on", discard_type: true) + # Wraps ActionView::Helpers::DateHelper#datetime_select for form builders: # - # # Generates a datetime select with a custom prompt. Use <tt>prompt: true</tt> for generic prompts. - # datetime_select("article", "written_on", prompt: {day: 'Choose day', month: 'Choose month', year: 'Choose year'}) - # datetime_select("article", "written_on", prompt: {hour: true}) # generic prompt for hours - # datetime_select("article", "written_on", prompt: true) # generic prompts for all + # <%= form_for @person do |f| %> + # <%= f.time_select :last_request_at %> + # <%= f.submit %> + # <% end %> # - # The selects are prepared for multi-parameter assignment to an Active Record object. + # Please refer to the documentation of the base helper for details. def datetime_select(method, options = {}, html_options = {}) @template.datetime_select(@object_name, method, objectify_options(options), html_options) end diff --git a/actionpack/lib/action_view/helpers/form_options_helper.rb b/actionpack/lib/action_view/helpers/form_options_helper.rb index 9f0205ada0..8b9471e485 100644 --- a/actionpack/lib/action_view/helpers/form_options_helper.rb +++ b/actionpack/lib/action_view/helpers/form_options_helper.rb @@ -756,312 +756,74 @@ module ActionView end class FormBuilder - # Create a select tag and a series of contained option tags for the provided object and method. - # The option currently held by the object will be selected, provided that the object is available. - # - # There are two possible formats for the choices parameter, corresponding to other helpers' output: - # * A flat collection: see options_for_select - # * A nested collection: see grouped_options_for_select - # - # Example with @post.person_id => 1: - # select("post", "person_id", Person.all.collect {|p| [ p.name, p.id ] }, { include_blank: true }) - # - # could become: - # - # <select name="post[person_id]"> - # <option value=""></option> - # <option value="1" selected="selected">David</option> - # <option value="2">Sam</option> - # <option value="3">Tobias</option> - # </select> + # Wraps ActionView::Helpers::FormOptionsHelper#select for form builders: # - # This can be used to provide a default set of options in the standard way: before rendering the create form, a - # new model instance is assigned the default options and bound to @model_name. Usually this model is not saved - # to the database. Instead, a second model object is created when the create request is received. - # This allows the user to submit a form page more than once with the expected results of creating multiple records. - # In addition, this allows a single partial to be used to generate form inputs for both edit and create forms. - # - # By default, <tt>post.person_id</tt> is the selected option. Specify <tt>selected: value</tt> to use a different selection - # or <tt>selected: nil</tt> to leave all options unselected. Similarly, you can specify values to be disabled in the option - # tags by specifying the <tt>:disabled</tt> option. This can either be a single value or an array of values to be disabled. - # - # ==== Gotcha - # - # The HTML specification says when +multiple+ parameter passed to select and all options got deselected - # web browsers do not send any value to server. Unfortunately this introduces a gotcha: - # if an +User+ model has many +roles+ and have +role_ids+ accessor, and in the form that edits roles of the user - # the user deselects all roles from +role_ids+ multiple select box, no +role_ids+ parameter is sent. So, - # any mass-assignment idiom like - # - # @user.update(params[:user]) - # - # wouldn't update roles. - # - # To prevent this the helper generates an auxiliary hidden field before - # every multiple select. The hidden field has the same name as multiple select and blank value. - # - # This way, the client either sends only the hidden field (representing - # the deselected multiple select box), or both fields. Since the HTML specification - # says key/value pairs have to be sent in the same order they appear in the - # form, and parameters extraction gets the last occurrence of any repeated - # key in the query string, that works for ordinary forms. - # - # In case if you don't want the helper to generate this hidden field you can specify - # <tt>include_hidden: false</tt> option. + # <%= form_for @post do |f| %> + # <%= f.select :person_id, Person.all.collect {|p| [ p.name, p.id ] }, { include_blank: true }) %> + # <%= f.submit %> + # <% end %> # + # Please refer to the documentation of the base helper for details. def select(method, choices, options = {}, html_options = {}) @template.select(@object_name, method, choices, objectify_options(options), @default_options.merge(html_options)) end - # Returns <tt><select></tt> and <tt><option></tt> 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 <tt>:prompt</tt> - # or <tt>:include_blank</tt> in the +options+ hash. + # Wraps ActionView::Helpers::FormOptionsHelper#collection_select for form builders: # - # The <tt>:value_method</tt> and <tt>:text_method</tt> parameters are methods to be called on each member - # of +collection+. The return values are used as the +value+ attribute and contents of each - # <tt><option></tt> tag, respectively. They 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/text. + # <%= form_for @post do |f| %> + # <%= f.collection_select :person_id, Author.all, :id, :name_with_initial, prompt: true %> + # <%= f.submit %> + # <% end %> # - # Example object structure for use with this method: - # - # class Post < ActiveRecord::Base - # belongs_to :author - # end - # - # class Author < ActiveRecord::Base - # has_many :posts - # def name_with_initial - # "#{first_name.first}. #{last_name}" - # end - # end - # - # Sample usage (selecting the associated Author for an instance of Post, <tt>@post</tt>): - # - # collection_select(:post, :author_id, Author.all, :id, :name_with_initial, prompt: true) - # - # If <tt>@post.author_id</tt> is already <tt>1</tt>, this would return: - # <select name="post[author_id]"> - # <option value="">Please select</option> - # <option value="1" selected="selected">D. Heinemeier Hansson</option> - # <option value="2">D. Thomas</option> - # <option value="3">M. Clark</option> - # </select> + # Please refer to the documentation of the base helper for details. def collection_select(method, collection, value_method, text_method, options = {}, html_options = {}) @template.collection_select(@object_name, method, collection, value_method, text_method, objectify_options(options), @default_options.merge(html_options)) end - # Returns <tt><select></tt>, <tt><optgroup></tt> and <tt><option></tt> 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 <tt>:prompt</tt> - # or <tt>:include_blank</tt> 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 <tt><optgroup></tt> tags. - # * +group_method+ - The name of a method which, when called on a member of +collection+, returns an - # array of child objects representing the <tt><option></tt> 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 <tt><optgroup></tt> 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 <tt><option></tt> 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 <tt><option></tt> tag. - # - # Example object structure for use with this method: + # Wraps ActionView::Helpers::FormOptionsHelper#grouped_collection_select for form builders: # - # class Continent < ActiveRecord::Base - # has_many :countries - # # attribs: id, name - # end - # - # class Country < ActiveRecord::Base - # belongs_to :continent - # # attribs: id, name, continent_id - # end - # - # class City < ActiveRecord::Base - # belongs_to :country - # # attribs: id, name, country_id - # end - # - # Sample usage: - # - # grouped_collection_select(:city, :country_id, @continents, :countries, :name, :id, :name) - # - # Possible output: - # - # <select name="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> + # <%= form_for @city do |f| %> + # <%= f.grouped_collection_select :country_id, :country_id, @continents, :countries, :name, :id, :name %> + # <%= f.submit %> + # <% end %> # + # Please refer to the documentation of the base helper for details. def grouped_collection_select(method, collection, group_method, group_label_method, option_key_method, option_value_method, options = {}, html_options = {}) @template.grouped_collection_select(@object_name, method, collection, group_method, group_label_method, option_key_method, option_value_method, objectify_options(options), @default_options.merge(html_options)) end - # Return select and option tags for the given object and method, using - # #time_zone_options_for_select to generate the list of option tags. - # - # In addition to the <tt>:include_blank</tt> option documented above, - # this method also supports a <tt>:model</tt> option, which defaults - # to ActiveSupport::TimeZone. This may be used by users to specify a - # different time zone model object. (See +time_zone_options_for_select+ - # for more information.) - # - # You can also supply an array of ActiveSupport::TimeZone objects - # as +priority_zones+, so that they will be listed above the rest of the - # (long) list. (You can use ActiveSupport::TimeZone.us_zones as a convenience - # for obtaining a list of the US time zones, or a Regexp to select the zones - # of your choice) + # Wraps ActionView::Helpers::FormOptionsHelper#time_zone_select for form builders: # - # Finally, this method supports a <tt>:default</tt> option, which selects - # a default ActiveSupport::TimeZone if the object's time zone is +nil+. + # <%= form_for @user do |f| %> + # <%= f.time_zone_select :time_zone, nil, include_blank: true %> + # <%= f.submit %> + # <% end %> # - # time_zone_select( "user", "time_zone", nil, include_blank: true) - # - # time_zone_select( "user", "time_zone", nil, default: "Pacific Time (US & Canada)" ) - # - # time_zone_select( "user", 'time_zone', ActiveSupport::TimeZone.us_zones, default: "Pacific Time (US & Canada)") - # - # time_zone_select( "user", 'time_zone', [ ActiveSupport::TimeZone['Alaska'], ActiveSupport::TimeZone['Hawaii'] ]) - # - # time_zone_select( "user", 'time_zone', /Australia/) - # - # time_zone_select( "user", "time_zone", ActiveSupport::TimeZone.all.sort, model: ActiveSupport::TimeZone) + # Please refer to the documentation of the base helper for details. def time_zone_select(method, priority_zones = nil, options = {}, html_options = {}) @template.time_zone_select(@object_name, method, priority_zones, objectify_options(options), @default_options.merge(html_options)) end - # Returns check box 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. - # - # The <tt>:value_method</tt> and <tt>:text_method</tt> parameters are - # methods to be called on each member of +collection+. The return values - # are used as the +value+ attribute and contents of each check box tag, - # respectively. They 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/text. + # Wraps ActionView::Helpers::FormOptionsHelper#collection_check_boxes for form builders: # - # Example object structure for use with this method: - # class Post < ActiveRecord::Base - # has_and_belongs_to_many :author - # end - # class Author < ActiveRecord::Base - # has_and_belongs_to_many :posts - # def name_with_initial - # "#{first_name.first}. #{last_name}" - # end - # end - # - # Sample usage (selecting the associated Author for an instance of Post, <tt>@post</tt>): - # collection_check_boxes(:post, :author_ids, Author.all, :id, :name_with_initial) - # - # If <tt>@post.author_ids</tt> is already <tt>[1]</tt>, this would return: - # <input id="post_author_ids_1" name="post[author_ids][]" type="checkbox" value="1" checked="checked" /> - # <label for="post_author_ids_1">D. Heinemeier Hansson</label> - # <input id="post_author_ids_2" name="post[author_ids][]" type="checkbox" value="2" /> - # <label for="post_author_ids_2">D. Thomas</label> - # <input id="post_author_ids_3" name="post[author_ids][]" type="checkbox" value="3" /> - # <label for="post_author_ids_3">M. Clark</label> - # <input name="post[author_ids][]" type="hidden" value="" /> - # - # It is also possible to customize the way the elements will be shown by - # giving a block to the method: - # collection_check_boxes(:post, :author_ids, Author.all, :id, :name_with_initial) do |b| - # b.label { b.check_box } - # end - # - # The argument passed to the block is a special kind of builder for this - # collection, which has the ability to generate the label and check box - # for the current item in the collection, with proper text and value. - # Using it, you can change the label and check box display order or even - # use the label as wrapper, as in the example above. + # <%= form_for @post do |f| %> + # <%= f.collection_check_boxes :author_ids, Author.all, :id, :name_with_initial %> + # <%= f.submit %> + # <% end %> # - # The builder methods <tt>label</tt> and <tt>check_box</tt> also accept - # extra html options: - # collection_check_boxes(:post, :author_ids, Author.all, :id, :name_with_initial) do |b| - # b.label(class: "check_box") { b.check_box(class: "check_box") } - # end - # - # There are also three special methods available: <tt>object</tt>, <tt>text</tt> and - # <tt>value</tt>, which are the current item being rendered, its text and value methods, - # respectively. You can use them like this: - # collection_check_boxes(:post, :author_ids, Author.all, :id, :name_with_initial) do |b| - # b.label(:"data-value" => b.value) { b.check_box + b.text } - # end + # Please refer to the documentation of the base helper for details. def collection_check_boxes(method, collection, value_method, text_method, options = {}, html_options = {}) @template.collection_check_boxes(@object_name, method, collection, value_method, text_method, objectify_options(options), @default_options.merge(html_options)) end - # Returns radio button 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. - # - # The <tt>:value_method</tt> and <tt>:text_method</tt> parameters are - # methods to be called on each member of +collection+. The return values - # are used as the +value+ attribute and contents of each radio button tag, - # respectively. They 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/text. - # - # Example object structure for use with this method: - # class Post < ActiveRecord::Base - # belongs_to :author - # end - # class Author < ActiveRecord::Base - # has_many :posts - # def name_with_initial - # "#{first_name.first}. #{last_name}" - # end - # end + # Wraps ActionView::Helpers::FormOptionsHelper#collection_radio_buttons for form builders: # - # Sample usage (selecting the associated Author for an instance of Post, <tt>@post</tt>): - # collection_radio_buttons(:post, :author_id, Author.all, :id, :name_with_initial) - # - # If <tt>@post.author_id</tt> is already <tt>1</tt>, this would return: - # <input id="post_author_id_1" name="post[author_id]" type="radio" value="1" checked="checked" /> - # <label for="post_author_id_1">D. Heinemeier Hansson</label> - # <input id="post_author_id_2" name="post[author_id]" type="radio" value="2" /> - # <label for="post_author_id_2">D. Thomas</label> - # <input id="post_author_id_3" name="post[author_id]" type="radio" value="3" /> - # <label for="post_author_id_3">M. Clark</label> - # - # It is also possible to customize the way the elements will be shown by - # giving a block to the method: - # collection_radio_buttons(:post, :author_id, Author.all, :id, :name_with_initial) do |b| - # b.label { b.radio_button } - # end - # - # The argument passed to the block is a special kind of builder for this - # collection, which has the ability to generate the label and radio button - # for the current item in the collection, with proper text and value. - # Using it, you can change the label and radio button display order or - # even use the label as wrapper, as in the example above. + # <%= form_for @post do |f| %> + # <%= f.collection_radio_buttons :author_id, Author.all, :id, :name_with_initial %> + # <%= f.submit %> + # <% end %> # - # The builder methods <tt>label</tt> and <tt>radio_button</tt> also accept - # extra html options: - # collection_radio_buttons(:post, :author_id, Author.all, :id, :name_with_initial) do |b| - # b.label(class: "radio_button") { b.radio_button(class: "radio_button") } - # end - # - # There are also three special methods available: <tt>object</tt>, <tt>text</tt> and - # <tt>value</tt>, which are the current item being rendered, its text and value methods, - # respectively. You can use them like this: - # collection_radio_buttons(:post, :author_id, Author.all, :id, :name_with_initial) do |b| - # b.label(:"data-value" => b.value) { b.radio_button + b.text } - # end + # Please refer to the documentation of the base helper for details. def collection_radio_buttons(method, collection, value_method, text_method, options = {}, html_options = {}) @template.collection_radio_buttons(@object_name, method, collection, value_method, text_method, objectify_options(options), @default_options.merge(html_options)) end diff --git a/actionpack/lib/action_view/template.rb b/actionpack/lib/action_view/template.rb index 6ba1a7cf0f..f73d14c79b 100644 --- a/actionpack/lib/action_view/template.rb +++ b/actionpack/lib/action_view/template.rb @@ -80,7 +80,7 @@ module ActionView # problems with converting the user's data to # the <tt>default_internal</tt>. # - # To do so, simply raise the +WrongEncodingError+ as follows: + # To do so, simply raise +WrongEncodingError+ as follows: # # raise WrongEncodingError.new( # problematic_string, |