From 130fe74d17404e5c06353526c7b20beb4019cb69 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Wed, 19 Nov 2008 14:00:16 +0100 Subject: Changed the default of ActionView#render to assume partials instead of files when not given an options hash [DHH] --- actionpack/lib/action_view/base.rb | 23 ++++++++++++++++------- actionpack/lib/action_view/partials.rb | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 7 deletions(-) (limited to 'actionpack/lib/action_view') diff --git a/actionpack/lib/action_view/base.rb b/actionpack/lib/action_view/base.rb index 511858dd9b..0d3752d875 100644 --- a/actionpack/lib/action_view/base.rb +++ b/actionpack/lib/action_view/base.rb @@ -234,16 +234,21 @@ module ActionView #:nodoc: @view_paths = self.class.process_view_paths(paths) end - # Renders the template present at template_path (relative to the view_paths array). - # The hash in local_assigns is made available as local variables. + # Returns the result of a render that's dictated by the options hash. The primary options are: + # + # * :partial - See ActionView::Partials. + # * :update - Calls update_page with the block given. + # * :file - Renders an explicit template file (this used to be the old default), add :locals to pass in those. + # * :inline - Renders an inline template similar to how it's done in the controller. + # * :text - Renders the text passed in out. + # + # If no options hash is passed or :update specified, the default is to render a partial and use the second parameter + # as the locals hash. def render(options = {}, local_assigns = {}, &block) #:nodoc: local_assigns ||= {} - if options.is_a?(String) - render(:file => options, :locals => local_assigns) - elsif options == :update - update_page(&block) - elsif options.is_a?(Hash) + case options + when Hash options = options.reverse_merge(:locals => {}) if options[:layout] _render_with_layout(options, local_assigns, &block) @@ -256,6 +261,10 @@ module ActionView #:nodoc: elsif options[:text] options[:text] end + when :update + update_page(&block) + else + render_partial(:partial => options, :locals => local_assigns) end end diff --git a/actionpack/lib/action_view/partials.rb b/actionpack/lib/action_view/partials.rb index 8841099900..bbc995a340 100644 --- a/actionpack/lib/action_view/partials.rb +++ b/actionpack/lib/action_view/partials.rb @@ -46,6 +46,38 @@ module ActionView # # This will render the partial "advertisement/_ad.erb" regardless of which controller this is being called from. # + # == Rendering objects with the RecordIdentifier + # + # Instead of explicitly naming the location of a partial, you can also let the RecordIdentifier do the work if + # you're following its conventions for RecordIdentifier#partial_path. Examples: + # + # # @account is an Account instance, so it uses the RecordIdentifier to replace + # # <%= render :partial => "accounts/account", :locals => { :account => @buyer } %> + # <%= render :partial => @account %> + # + # # @posts is an array of Post instances, so it uses the RecordIdentifier to replace + # # <%= render :partial => "posts/post", :collection => @posts %> + # <%= render :partial => @posts %> + # + # == Rendering the default case + # + # If you're not going to be using any of the options like collections or layouts, you can also use the short-hand + # defaults of render to render partials. Examples: + # + # # Instead of <%= render :partial => "account" %> + # <%= render "account" %> + # + # # Instead of <%= render :partial => "account", :locals => { :account => @buyer } %> + # <%= render "account", :account => @buyer %> + # + # # @account is an Account instance, so it uses the RecordIdentifier to replace + # # <%= render :partial => "accounts/account", :locals => { :account => @account } %> + # <%= render(@account) %> + # + # # @posts is an array of Post instances, so it uses the RecordIdentifier to replace + # # <%= render :partial => "posts/post", :collection => @posts %> + # <%= render(@posts) %> + # # == Rendering partials with layouts # # Partials can have their own layouts applied to them. These layouts are different than the ones that are -- cgit v1.2.3