Changeset 8786
- Timestamp:
- 02/02/08 06:16:04 (5 months ago)
- Files:
-
- trunk/actionpack/CHANGELOG (modified) (1 diff)
- trunk/actionpack/lib/action_view/helpers/form_helper.rb (modified) (6 diffs)
- trunk/actionpack/test/controller/new_render_test.rb (modified) (1 diff)
- trunk/actionpack/test/template/form_helper_test.rb (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/actionpack/CHANGELOG
r8785 r8786 1 1 *SVN* 2 3 * Introduce the :index option for form_for and fields_for to simplify multi-model forms (see http://railscasts.com/episodes/75). #9883 [rmm5t] 2 4 3 5 * Introduce map.resources :cards, :as => 'tarjetas' to use a custom resource name in the URL: cards_path == '/tarjetas'. #10578 [blj] trunk/actionpack/lib/action_view/helpers/form_helper.rb
r8646 r8786 66 66 # 67 67 # <input type="text" id="person_1_name" name="person[1][name]" value="<%= @person.name %>" /> 68 # 69 # An <tt>index</tt> option may also be passed to <tt>form_for</tt> and <tt>fields_for</tt>. This automatically applies 70 # the <tt>index</tt> to all the nested fields. 68 71 # 69 72 # There are also methods for helping to build form tags in link:classes/ActionView/Helpers/FormOptionsHelper.html, … … 645 648 def initialize(object_name, object, template, options, proc) 646 649 @object_name, @object, @template, @options, @proc = object_name, object, template, options, proc 650 @default_options = @options ? @options.slice(:index) : {} 647 651 end 648 652 … … 650 654 src = <<-end_src 651 655 def #{selector}(method, options = {}) 652 @template.send(#{selector.inspect}, @object_name, method, o ptions.merge(:object => @object))656 @template.send(#{selector.inspect}, @object_name, method, objectify_options(options)) 653 657 end 654 658 end_src … … 669 673 args.unshift(object) 670 674 end 671 675 672 676 @template.fields_for(name, *args, &block) 673 677 end 674 678 675 679 def label(method, text = nil, options = {}) 676 @template.label(@object_name, method, text, o ptions.merge(:object => @object))680 @template.label(@object_name, method, text, objectify_options(options)) 677 681 end 678 682 679 683 def check_box(method, options = {}, checked_value = "1", unchecked_value = "0") 680 @template.check_box(@object_name, method, o ptions.merge(:object => @object), checked_value, unchecked_value)684 @template.check_box(@object_name, method, objectify_options(options), checked_value, unchecked_value) 681 685 end 682 686 683 687 def radio_button(method, tag_value, options = {}) 684 @template.radio_button(@object_name, method, tag_value, o ptions.merge(:object => @object))688 @template.radio_button(@object_name, method, tag_value, objectify_options(options)) 685 689 end 686 690 … … 690 694 691 695 def error_messages(options = {}) 692 @template.error_messages_for(@object_name, o ptions.merge(:object => @object))696 @template.error_messages_for(@object_name, objectify_options(options)) 693 697 end 694 698 … … 696 700 @template.submit_tag(value, options.reverse_merge(:id => "#{object_name}_submit")) 697 701 end 702 703 private 704 def objectify_options(options) 705 @default_options.merge(options.merge(:object => @object)) 706 end 698 707 end 699 708 end trunk/actionpack/test/controller/new_render_test.rb
r8683 r8786 137 137 138 138 def partial_with_form_builder 139 render :partial => ActionView::Helpers::FormBuilder.new(:post, nil, @template, nil, Proc.new {})140 end 141 139 render :partial => ActionView::Helpers::FormBuilder.new(:post, nil, @template, {}, Proc.new {}) 140 end 141 142 142 def partial_with_form_builder_subclass 143 render :partial => LabellingFormBuilder.new(:post, nil, @template, nil, Proc.new {})143 render :partial => LabellingFormBuilder.new(:post, nil, @template, {}, Proc.new {}) 144 144 end 145 145 trunk/actionpack/test/template/form_helper_test.rb
r8564 r8786 364 364 def test_form_for_with_index 365 365 _erbout = '' 366 366 367 367 form_for("post[]", @post) do |f| 368 368 _erbout.concat f.label(:title) … … 371 371 _erbout.concat f.check_box(:secret) 372 372 end 373 373 374 374 expected = 375 375 "<form action='http://www.example.com' method='post'>" + … … 384 384 end 385 385 386 def test_form_for_with_nil_index_option_override 387 _erbout = '' 388 389 form_for("post[]", @post, :index => nil) do |f| 390 _erbout.concat f.text_field(:title) 391 _erbout.concat f.text_area(:body) 392 _erbout.concat f.check_box(:secret) 393 end 394 395 expected = 396 "<form action='http://www.example.com' method='post'>" + 397 "<input name='post[][title]' size='30' type='text' id='post__title' value='Hello World' />" + 398 "<textarea name='post[][body]' id='post__body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" + 399 "<input name='post[][secret]' checked='checked' type='checkbox' id='post__secret' value='1' />" + 400 "<input name='post[][secret]' type='hidden' value='0' />" + 401 "</form>" 402 403 assert_dom_equal expected, _erbout 404 end 405 386 406 def test_nested_fields_for 387 407 _erbout = '' … … 413 433 "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" + 414 434 "<input name='post[secret]' type='hidden' value='0' />" 435 436 assert_dom_equal expected, _erbout 437 end 438 439 def test_fields_for_with_index 440 _erbout = '' 441 442 fields_for("post[]", @post) do |f| 443 _erbout.concat f.text_field(:title) 444 _erbout.concat f.text_area(:body) 445 _erbout.concat f.check_box(:secret) 446 end 447 448 expected = 449 "<input name='post[123][title]' size='30' type='text' id='post_123_title' value='Hello World' />" + 450 "<textarea name='post[123][body]' id='post_123_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" + 451 "<input name='post[123][secret]' checked='checked' type='checkbox' id='post_123_secret' value='1' />" + 452 "<input name='post[123][secret]' type='hidden' value='0' />" 453 454 assert_dom_equal expected, _erbout 455 end 456 457 def test_fields_for_with_nil_index_option_override 458 _erbout = '' 459 460 fields_for("post[]", @post, :index => nil) do |f| 461 _erbout.concat f.text_field(:title) 462 _erbout.concat f.text_area(:body) 463 _erbout.concat f.check_box(:secret) 464 end 465 466 expected = 467 "<input name='post[][title]' size='30' type='text' id='post__title' value='Hello World' />" + 468 "<textarea name='post[][body]' id='post__body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" + 469 "<input name='post[][secret]' checked='checked' type='checkbox' id='post__secret' value='1' />" + 470 "<input name='post[][secret]' type='hidden' value='0' />" 471 472 assert_dom_equal expected, _erbout 473 end 474 475 def test_fields_for_with_index_option_override 476 _erbout = '' 477 478 fields_for("post[]", @post, :index => "abc") do |f| 479 _erbout.concat f.text_field(:title) 480 _erbout.concat f.text_area(:body) 481 _erbout.concat f.check_box(:secret) 482 end 483 484 expected = 485 "<input name='post[abc][title]' size='30' type='text' id='post_abc_title' value='Hello World' />" + 486 "<textarea name='post[abc][body]' id='post_abc_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" + 487 "<input name='post[abc][secret]' checked='checked' type='checkbox' id='post_abc_secret' value='1' />" + 488 "<input name='post[abc][secret]' type='hidden' value='0' />" 415 489 416 490 assert_dom_equal expected, _erbout