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

Changeset 8646

Show
Ignore:
Timestamp:
01/16/08 02:01:57 (4 months ago)
Author:
nzkoz
Message:

Make render :partial recognise form builders and use the _form partial. Closes #10814 [djanowski]

Files:

Legend:

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

    r8637 r8646  
    11*SVN* 
     2 
     3* Make render :partial recognise form builders and use the _form partial.  #10814 [djanowski] 
    24 
    35* Allow users to declare other namespaces when using the atom feed helpers. #10304 [david.calavera] 
  • trunk/actionpack/lib/action_view/helpers/form_helper.rb

    r8597 r8646  
    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    # 
     
    153162      #     <%= check_box_tag "person[admin]", @person.company.admin? %> 
    154163      #   <% end %> 
     164      # 
     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>. 
    155170      # 
    156171      # In many cases you will want to wrap the above in another helper, so you could do something like the following: 
  • trunk/actionpack/lib/action_view/partials.rb

    r8136 r8646  
    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? 
  • trunk/actionpack/test/controller/new_render_test.rb

    r8564 r8646  
    1616    page.visual_effect :highlight 
    1717  end 
     18end 
     19 
     20class LabellingFormBuilder < ActionView::Helpers::FormBuilder 
    1821end 
    1922 
     
    137140    render :partial => "customer", :locals => { :customer => Customer.new("david") }  
    138141  end 
    139    
     142 
     143  def partial_with_form_builder 
     144    render :partial => ActionView::Helpers::FormBuilder.new(:post, nil, @template, nil, Proc.new {}) 
     145  end 
     146   
     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") ] 
     
    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