Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 8786

Show
Ignore:
Timestamp:
02/02/08 06:16:04 (2 years ago)
Author:
bitsweat
Message:

Introduce the :index option for form_for and fields_for to simplify multi-model forms (see http://railscasts.com/episodes/75). Closes #9883.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/actionpack/CHANGELOG

    r8785 r8786  
    11*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] 
    24 
    35* 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  
    6666    # 
    6767    #   <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. 
    6871    # 
    6972    # There are also methods for helping to build form tags in link:classes/ActionView/Helpers/FormOptionsHelper.html, 
     
    645648      def initialize(object_name, object, template, options, proc) 
    646649        @object_name, @object, @template, @options, @proc = object_name, object, template, options, proc 
     650        @default_options = @options ? @options.slice(:index) : {} 
    647651      end 
    648652 
     
    650654        src = <<-end_src 
    651655          def #{selector}(method, options = {}) 
    652             @template.send(#{selector.inspect}, @object_name, method, options.merge(:object => @object)) 
     656            @template.send(#{selector.inspect}, @object_name, method, objectify_options(options)) 
    653657          end 
    654658        end_src 
     
    669673          args.unshift(object) 
    670674        end 
    671          
     675 
    672676        @template.fields_for(name, *args, &block) 
    673677      end 
    674678 
    675679      def label(method, text = nil, options = {}) 
    676         @template.label(@object_name, method, text, options.merge(:object => @object)) 
     680        @template.label(@object_name, method, text, objectify_options(options)) 
    677681      end 
    678682 
    679683      def check_box(method, options = {}, checked_value = "1", unchecked_value = "0") 
    680         @template.check_box(@object_name, method, options.merge(:object => @object), checked_value, unchecked_value) 
     684        @template.check_box(@object_name, method, objectify_options(options), checked_value, unchecked_value) 
    681685      end 
    682686 
    683687      def radio_button(method, tag_value, options = {}) 
    684         @template.radio_button(@object_name, method, tag_value, options.merge(:object => @object)) 
     688        @template.radio_button(@object_name, method, tag_value, objectify_options(options)) 
    685689      end 
    686690 
     
    690694 
    691695      def error_messages(options = {}) 
    692         @template.error_messages_for(@object_name, options.merge(:object => @object)) 
     696        @template.error_messages_for(@object_name, objectify_options(options)) 
    693697      end 
    694698 
     
    696700        @template.submit_tag(value, options.reverse_merge(:id => "#{object_name}_submit")) 
    697701      end 
     702 
     703      private 
     704        def objectify_options(options) 
     705          @default_options.merge(options.merge(:object => @object)) 
     706        end 
    698707    end 
    699708  end 
  • trunk/actionpack/test/controller/new_render_test.rb

    r8683 r8786  
    137137 
    138138  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 
    142142  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 {}) 
    144144  end 
    145145 
  • trunk/actionpack/test/template/form_helper_test.rb

    r8564 r8786  
    364364  def test_form_for_with_index 
    365365    _erbout = '' 
    366      
     366 
    367367    form_for("post[]", @post) do |f| 
    368368      _erbout.concat f.label(:title) 
     
    371371      _erbout.concat f.check_box(:secret) 
    372372    end 
    373      
     373 
    374374    expected =  
    375375      "<form action='http://www.example.com' method='post'>" + 
     
    384384  end 
    385385 
     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 
    386406  def test_nested_fields_for 
    387407    _erbout = '' 
     
    413433      "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" + 
    414434      "<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' />" 
    415489 
    416490    assert_dom_equal expected, _erbout