Skip to content

Commit

Permalink
Make render :partial recognise form builders and use the _form partial.
Browse files Browse the repository at this point in the history
Closes #10814 [djanowski]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8646 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
NZKoz committed Jan 16, 2008
1 parent 8a71f87 commit b812b23
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 0 deletions.
2 changes: 2 additions & 0 deletions actionpack/CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
*SVN*

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

* Allow users to declare other namespaces when using the atom feed helpers. #10304 [david.calavera]

* Introduce send_file :x_sendfile => true to send an X-Sendfile response header. [Jeremy Kemper]
Expand Down
15 changes: 15 additions & 0 deletions actionpack/lib/action_view/helpers/form_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ module Helpers
# <input name="commit" type="submit" value="Create" />
# </form>
#
# If you are using a partial for your form fields, you can use this shortcut:
#
# <% form_for :person, @person, :url => { :action => "create" } do |f| %>
# <%= render :partial => f %>
# <%= submit_tag 'Create' %>
# <% end %>
#
# This example will render the <tt>people/_form</tt> partial, setting a local variable called <tt>form</tt> which references the yielded FormBuilder.
#
# The <tt>params</tt> object created when this form is submitted would look like:
#
# {"action"=>"create", "controller"=>"persons", "person"=>{"first_name"=>"William", "last_name"=>"Smith"}}
Expand Down Expand Up @@ -153,6 +162,12 @@ module FormHelper
# <%= check_box_tag "person[admin]", @person.company.admin? %>
# <% end %>
#
# In this case, if you use this:
#
# <%= render :partial => f %>
#
# The rendered template is <tt>people/_labelling_form</tt> and the local variable referencing the form builder is called <tt>labelling_form</tt>.
#
# In many cases you will want to wrap the above in another helper, so you could do something like the following:
#
# def labelled_form_for(record_or_name_or_array, *args, &proc)
Expand Down
3 changes: 3 additions & 0 deletions actionpack/lib/action_view/partials.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ def render_partial(partial_path, object_assigns = nil, local_assigns = nil) #:no
else
render("#{path}/_#{partial_name}", local_assigns)
end
when ActionView::Helpers::FormBuilder
builder_partial_path = partial_path.class.to_s.demodulize.underscore.sub(/_builder$/, '')
render_partial(builder_partial_path, object_assigns, (local_assigns || {}).merge(builder_partial_path.to_sym => partial_path))
when Array, ActiveRecord::Associations::AssociationCollection, ActiveRecord::Associations::HasManyThroughAssociation
if partial_path.any?
path = ActionController::RecordIdentifier.partial_path(partial_path.first)
Expand Down
23 changes: 23 additions & 0 deletions actionpack/test/controller/new_render_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ def rjs_helper_method_from_module
end
end

class LabellingFormBuilder < ActionView::Helpers::FormBuilder
end

class NewRenderTestController < ActionController::Base
layout :determine_layout

Expand Down Expand Up @@ -136,7 +139,15 @@ def partial_only_with_layout
def partial_with_locals
render :partial => "customer", :locals => { :customer => Customer.new("david") }
end

def partial_with_form_builder
render :partial => ActionView::Helpers::FormBuilder.new(:post, nil, @template, nil, Proc.new {})
end

def partial_with_form_builder_subclass
render :partial => LabellingFormBuilder.new(:post, nil, @template, nil, Proc.new {})
end

def partial_collection
render :partial => "customer", :collection => [ Customer.new("david"), Customer.new("mary") ]
end
Expand Down Expand Up @@ -685,6 +696,18 @@ def test_partial_with_locals
assert_equal "Hello: david", @response.body
end

def test_partial_with_form_builder
get :partial_with_form_builder
assert_match(/<label/, @response.body)
assert_template('test/_form')
end

def test_partial_with_form_builder_subclass
get :partial_with_form_builder_subclass
assert_match(/<label/, @response.body)
assert_template('test/_labelling_form')
end

def test_partial_collection
get :partial_collection
assert_equal "Hello: davidHello: mary", @response.body
Expand Down
1 change: 1 addition & 0 deletions actionpack/test/fixtures/test/_form.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= form.label :title %>
1 change: 1 addition & 0 deletions actionpack/test/fixtures/test/_labelling_form.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= labelling_form.label :title %>

0 comments on commit b812b23

Please sign in to comment.