diff options
-rw-r--r-- | actionpack/CHANGELOG | 2 | ||||
-rw-r--r-- | actionpack/lib/action_view/helpers/form_helper.rb | 32 | ||||
-rw-r--r-- | actionpack/test/template/form_helper_test.rb | 17 |
3 files changed, 47 insertions, 4 deletions
diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index 04e212f7dc..7641c24ead 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Added the same record identification guessing rules to fields_for as form_for has [DHH] + * Fixed that verification violations with no specified action didn't halt the chain (now they do with a 400 Bad Request) [DHH] diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb index 6da55e4479..4c050cae00 100644 --- a/actionpack/lib/action_view/helpers/form_helper.rb +++ b/actionpack/lib/action_view/helpers/form_helper.rb @@ -204,21 +204,45 @@ module ActionView # fields_for suitable for specifying additional model objects in the same form: # # ==== Examples - # <% form_for :person, @person, :url => { :action => "update" } do |person_form| %> + # <% form_for @person, :url => { :action => "update" } do |person_form| %> # First name: <%= person_form.text_field :first_name %> # Last name : <%= person_form.text_field :last_name %> # - # <% fields_for :permission, @person.permission do |permission_fields| %> + # <% fields_for @person.permission do |permission_fields| %> # Admin? : <%= permission_fields.check_box :admin %> # <% end %> # <% end %> # + # ...or if you have an object that needs to be represented as a different parameter, like a Client that acts as a Person: + # + # <% fields_for :person, @client do |permission_fields| %> + # Admin?: <%= permission_fields.check_box :admin %> + # <% end %> + # + # ...or if you don't have an object, just a name of the parameter + # + # <% fields_for :person do |permission_fields| %> + # Admin?: <%= permission_fields.check_box :admin %> + # <% end %> + # # Note: This also works for the methods in FormOptionHelper and DateHelper that are designed to work with an object as base, # like FormOptionHelper#collection_select and DateHelper#datetime_select. - def fields_for(object_name, *args, &block) + def fields_for(record_or_name_or_array, *args, &block) raise ArgumentError, "Missing block" unless block_given? options = args.extract_options! - object = args.first + + case record_or_name_or_array + when String, Symbol + object_name = record_or_name_or_array + object = args.first + when Array + object = record_or_name_or_array.last + object_name = ActionController::RecordIdentifier.singular_class_name(object) + apply_form_for_options!(record_or_name_or_array, options) + else + object = record_or_name_or_array + object_name = ActionController::RecordIdentifier.singular_class_name(object) + end builder = options[:builder] || ActionView::Base.default_form_builder yield builder.new(object_name, object, self, options, block) diff --git a/actionpack/test/template/form_helper_test.rb b/actionpack/test/template/form_helper_test.rb index fa53f6ae1a..136e6f1e83 100644 --- a/actionpack/test/template/form_helper_test.rb +++ b/actionpack/test/template/form_helper_test.rb @@ -433,6 +433,23 @@ class FormHelperTest < Test::Unit::TestCase assert_dom_equal expected, _erbout end + def test_fields_for_with_only_object + _erbout = '' + fields_for(@post) do |f| + _erbout.concat f.text_field(:title) + _erbout.concat f.text_area(:body) + _erbout.concat f.check_box(:secret) + end + + expected = + "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" + + "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" + + "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" + + "<input name='post[secret]' type='hidden' value='0' />" + + assert_dom_equal expected, _erbout + end + def test_fields_for_object_with_bracketed_name _erbout = '' fields_for("author[post]", @post) do |f| |