From b812b236876ab7a6bd1f3e58a1aa71847394f3a1 Mon Sep 17 00:00:00 2001 From: Michael Koziarski Date: Wed, 16 Jan 2008 02:01:57 +0000 Subject: Make render :partial recognise form builders and use the _form partial. Closes #10814 [djanowski] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8646 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- actionpack/lib/action_view/helpers/form_helper.rb | 15 +++++++++++++++ actionpack/lib/action_view/partials.rb | 3 +++ 2 files changed, 18 insertions(+) (limited to 'actionpack/lib/action_view') diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb index f67a040149..d2835a2438 100644 --- a/actionpack/lib/action_view/helpers/form_helper.rb +++ b/actionpack/lib/action_view/helpers/form_helper.rb @@ -32,6 +32,15 @@ module ActionView # # # + # 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 people/_form partial, setting a local variable called form which references the yielded FormBuilder. + # # The params object created when this form is submitted would look like: # # {"action"=>"create", "controller"=>"persons", "person"=>{"first_name"=>"William", "last_name"=>"Smith"}} @@ -153,6 +162,12 @@ module ActionView # <%= check_box_tag "person[admin]", @person.company.admin? %> # <% end %> # + # In this case, if you use this: + # + # <%= render :partial => f %> + # + # The rendered template is people/_labelling_form and the local variable referencing the form builder is called labelling_form. + # # 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) diff --git a/actionpack/lib/action_view/partials.rb b/actionpack/lib/action_view/partials.rb index 0795b8ec55..a1ba60d560 100644 --- a/actionpack/lib/action_view/partials.rb +++ b/actionpack/lib/action_view/partials.rb @@ -119,6 +119,9 @@ module ActionView 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) -- cgit v1.2.3