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

Ticket #10814: render_partial_builder.diff

File render_partial_builder.diff, 4.5 kB (added by djanowski, 6 months ago)
  • actionpack/test/controller/new_render_test.rb

    old new  
    1717  end 
    1818end 
    1919 
     20class LabellingFormBuilder < ActionView::Helpers::FormBuilder 
     21end 
     22 
    2023class NewRenderTestController < ActionController::Base 
    2124  layout :determine_layout 
    2225 
     
    136139  def partial_with_locals 
    137140    render :partial => "customer", :locals => { :customer => Customer.new("david") }  
    138141  end 
     142 
     143  def partial_with_form_builder 
     144    render :partial => ActionView::Helpers::FormBuilder.new(:post, nil, @template, nil, Proc.new {}) 
     145  end 
    139146   
     147  def partial_with_form_builder_subclass 
     148    render :partial => LabellingFormBuilder.new(:post, nil, @template, nil, Proc.new {}) 
     149  end 
     150   
    140151  def partial_collection 
    141152    render :partial => "customer", :collection => [ Customer.new("david"), Customer.new("mary") ] 
    142153  end 
     
    685696    assert_equal "Hello: david", @response.body 
    686697  end 
    687698 
     699  def test_partial_with_form_builder 
     700    get :partial_with_form_builder 
     701    assert_match(/<label/, @response.body) 
     702    assert_template('test/_form') 
     703  end 
     704   
     705  def test_partial_with_form_builder_subclass 
     706    get :partial_with_form_builder_subclass 
     707    assert_match(/<label/, @response.body) 
     708    assert_template('test/_labelling_form') 
     709  end 
     710 
    688711  def test_partial_collection 
    689712    get :partial_collection 
    690713    assert_equal "Hello: davidHello: mary", @response.body 
  • actionpack/test/fixtures/test/_labelling_form.erb

    old new  
  • actionpack/test/fixtures/test/_form.erb

    old new  
  • actionpack/lib/action_view/partials.rb

    old new  
    119119          else 
    120120            render("#{path}/_#{partial_name}", local_assigns) 
    121121          end 
     122        when ActionView::Helpers::FormBuilder 
     123          builder_partial_path = partial_path.class.to_s.demodulize.underscore.sub(/_builder$/, '') 
     124          render_partial(builder_partial_path, object_assigns, (local_assigns || {}).merge(builder_partial_path.to_sym => partial_path)) 
    122125        when Array, ActiveRecord::Associations::AssociationCollection, ActiveRecord::Associations::HasManyThroughAssociation 
    123126          if partial_path.any? 
    124127            path       = ActionController::RecordIdentifier.partial_path(partial_path.first) 
  • actionpack/lib/action_view/helpers/form_helper.rb

    old new  
    3232    #       <input name="commit" type="submit" value="Create" /> 
    3333    #     </form> 
    3434    # 
     35    # If you are using a partial for your form fields, you can use this shortcut: 
     36    # 
     37    #     <% form_for :person, @person, :url => { :action => "create" } do |f| %> 
     38    #       <%= render :partial => f %> 
     39    #       <%= submit_tag 'Create' %> 
     40    #     <% end %> 
     41    # 
     42    # This example will render the <tt>people/_form</tt> partial, setting a local variable called <tt>form</tt> which references the yielded FormBuilder. 
     43    # 
    3544    # The <tt>params</tt> object created when this form is submitted would look like: 
    3645    # 
    3746    #     {"action"=>"create", "controller"=>"persons", "person"=>{"first_name"=>"William", "last_name"=>"Smith"}} 
     
    153162      #     <%= check_box_tag "person[admin]", @person.company.admin? %> 
    154163      #   <% end %> 
    155164      # 
     165      # In this case, if you use this: 
     166      # 
     167      #   <%= render :partial => f %> 
     168      # 
     169      # The rendered template is <tt>people/_labelling_form</tt> and the local variable referencing the form builder is called <tt>labelling_form</tt>. 
     170      # 
    156171      # In many cases you will want to wrap the above in another helper, so you could do something like the following: 
    157172      # 
    158173      #   def labelled_form_for(record_or_name_or_array, *args, &proc)