mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
Introduce the :index option for form_for and fields_for to simplify multi-model forms (see http://railscasts.com/episodes/75). Closes #9883.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8786 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
*SVN*
|
||||
|
||||
* Introduce the :index option for form_for and fields_for to simplify multi-model forms (see http://railscasts.com/episodes/75). #9883 [rmm5t]
|
||||
|
||||
* Introduce map.resources :cards, :as => 'tarjetas' to use a custom resource name in the URL: cards_path == '/tarjetas'. #10578 [blj]
|
||||
|
||||
* TestSession supports indifferent access. #7372 [tamc, Arsen7, mhackett, julik, jean.helou]
|
||||
|
||||
@@ -66,6 +66,9 @@ module ActionView
|
||||
#
|
||||
# <input type="text" id="person_1_name" name="person[1][name]" value="<%= @person.name %>" />
|
||||
#
|
||||
# An <tt>index</tt> option may also be passed to <tt>form_for</tt> and <tt>fields_for</tt>. This automatically applies
|
||||
# the <tt>index</tt> to all the nested fields.
|
||||
#
|
||||
# There are also methods for helping to build form tags in link:classes/ActionView/Helpers/FormOptionsHelper.html,
|
||||
# link:classes/ActionView/Helpers/DateHelper.html, and link:classes/ActionView/Helpers/ActiveRecordHelper.html
|
||||
module FormHelper
|
||||
@@ -644,12 +647,13 @@ module ActionView
|
||||
|
||||
def initialize(object_name, object, template, options, proc)
|
||||
@object_name, @object, @template, @options, @proc = object_name, object, template, options, proc
|
||||
@default_options = @options ? @options.slice(:index) : {}
|
||||
end
|
||||
|
||||
(field_helpers - %w(label check_box radio_button fields_for)).each do |selector|
|
||||
src = <<-end_src
|
||||
def #{selector}(method, options = {})
|
||||
@template.send(#{selector.inspect}, @object_name, method, options.merge(:object => @object))
|
||||
@template.send(#{selector.inspect}, @object_name, method, objectify_options(options))
|
||||
end
|
||||
end_src
|
||||
class_eval src, __FILE__, __LINE__
|
||||
@@ -668,20 +672,20 @@ module ActionView
|
||||
name = "#{object_name}[#{ActionController::RecordIdentifier.singular_class_name(object)}]"
|
||||
args.unshift(object)
|
||||
end
|
||||
|
||||
|
||||
@template.fields_for(name, *args, &block)
|
||||
end
|
||||
|
||||
def label(method, text = nil, options = {})
|
||||
@template.label(@object_name, method, text, options.merge(:object => @object))
|
||||
@template.label(@object_name, method, text, objectify_options(options))
|
||||
end
|
||||
|
||||
def check_box(method, options = {}, checked_value = "1", unchecked_value = "0")
|
||||
@template.check_box(@object_name, method, options.merge(:object => @object), checked_value, unchecked_value)
|
||||
@template.check_box(@object_name, method, objectify_options(options), checked_value, unchecked_value)
|
||||
end
|
||||
|
||||
def radio_button(method, tag_value, options = {})
|
||||
@template.radio_button(@object_name, method, tag_value, options.merge(:object => @object))
|
||||
@template.radio_button(@object_name, method, tag_value, objectify_options(options))
|
||||
end
|
||||
|
||||
def error_message_on(method, prepend_text = "", append_text = "", css_class = "formError")
|
||||
@@ -689,12 +693,17 @@ module ActionView
|
||||
end
|
||||
|
||||
def error_messages(options = {})
|
||||
@template.error_messages_for(@object_name, options.merge(:object => @object))
|
||||
@template.error_messages_for(@object_name, objectify_options(options))
|
||||
end
|
||||
|
||||
def submit(value = "Save changes", options = {})
|
||||
@template.submit_tag(value, options.reverse_merge(:id => "#{object_name}_submit"))
|
||||
end
|
||||
|
||||
private
|
||||
def objectify_options(options)
|
||||
@default_options.merge(options.merge(:object => @object))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -136,11 +136,11 @@ class NewRenderTestController < ActionController::Base
|
||||
end
|
||||
|
||||
def partial_with_form_builder
|
||||
render :partial => ActionView::Helpers::FormBuilder.new(:post, nil, @template, nil, Proc.new {})
|
||||
render :partial => ActionView::Helpers::FormBuilder.new(:post, nil, @template, {}, Proc.new {})
|
||||
end
|
||||
|
||||
|
||||
def partial_with_form_builder_subclass
|
||||
render :partial => LabellingFormBuilder.new(:post, nil, @template, nil, Proc.new {})
|
||||
render :partial => LabellingFormBuilder.new(:post, nil, @template, {}, Proc.new {})
|
||||
end
|
||||
|
||||
def partial_collection
|
||||
|
||||
@@ -363,14 +363,14 @@ class FormHelperTest < Test::Unit::TestCase
|
||||
|
||||
def test_form_for_with_index
|
||||
_erbout = ''
|
||||
|
||||
|
||||
form_for("post[]", @post) do |f|
|
||||
_erbout.concat f.label(:title)
|
||||
_erbout.concat f.text_field(:title)
|
||||
_erbout.concat f.text_area(:body)
|
||||
_erbout.concat f.check_box(:secret)
|
||||
end
|
||||
|
||||
|
||||
expected =
|
||||
"<form action='http://www.example.com' method='post'>" +
|
||||
"<label for=\"post_123_title\">Title</label>" +
|
||||
@@ -383,6 +383,26 @@ class FormHelperTest < Test::Unit::TestCase
|
||||
assert_dom_equal expected, _erbout
|
||||
end
|
||||
|
||||
def test_form_for_with_nil_index_option_override
|
||||
_erbout = ''
|
||||
|
||||
form_for("post[]", @post, :index => nil) do |f|
|
||||
_erbout.concat f.text_field(:title)
|
||||
_erbout.concat f.text_area(:body)
|
||||
_erbout.concat f.check_box(:secret)
|
||||
end
|
||||
|
||||
expected =
|
||||
"<form action='http://www.example.com' method='post'>" +
|
||||
"<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' />" +
|
||||
"</form>"
|
||||
|
||||
assert_dom_equal expected, _erbout
|
||||
end
|
||||
|
||||
def test_nested_fields_for
|
||||
_erbout = ''
|
||||
form_for(:post, @post) do |f|
|
||||
@@ -416,6 +436,60 @@ class FormHelperTest < Test::Unit::TestCase
|
||||
assert_dom_equal expected, _erbout
|
||||
end
|
||||
|
||||
def test_fields_for_with_index
|
||||
_erbout = ''
|
||||
|
||||
fields_for("post[]", @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[123][title]' size='30' type='text' id='post_123_title' value='Hello World' />" +
|
||||
"<textarea name='post[123][body]' id='post_123_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
|
||||
"<input name='post[123][secret]' checked='checked' type='checkbox' id='post_123_secret' value='1' />" +
|
||||
"<input name='post[123][secret]' type='hidden' value='0' />"
|
||||
|
||||
assert_dom_equal expected, _erbout
|
||||
end
|
||||
|
||||
def test_fields_for_with_nil_index_option_override
|
||||
_erbout = ''
|
||||
|
||||
fields_for("post[]", @post, :index => nil) 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_with_index_option_override
|
||||
_erbout = ''
|
||||
|
||||
fields_for("post[]", @post, :index => "abc") 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[abc][title]' size='30' type='text' id='post_abc_title' value='Hello World' />" +
|
||||
"<textarea name='post[abc][body]' id='post_abc_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
|
||||
"<input name='post[abc][secret]' checked='checked' type='checkbox' id='post_abc_secret' value='1' />" +
|
||||
"<input name='post[abc][secret]' type='hidden' value='0' />"
|
||||
|
||||
assert_dom_equal expected, _erbout
|
||||
end
|
||||
|
||||
def test_fields_for_without_object
|
||||
_erbout = ''
|
||||
fields_for(:post) do |f|
|
||||
|
||||
Reference in New Issue
Block a user