1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Added the same record identification guessing rules to fields_for as form_for has [DHH]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8252 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
David Heinemeier Hansson 2007-12-02 00:46:43 +00:00
parent 67442cb40d
commit d0ce7cd4c7
3 changed files with 47 additions and 4 deletions

View file

@ -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]

View file

@ -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!
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)

View file

@ -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|