Skip to content

Commit

Permalink
Return nil instead of a space when passing an empty collection or nil…
Browse files Browse the repository at this point in the history
… to 'render :partial' [#791 state:resolved]

Signed-off-by: Joshua Peek <josh@joshpeek.com>
  • Loading branch information
ryanb authored and josh committed Aug 20, 2008
1 parent 71c4ff0 commit a8ece12
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 6 deletions.
9 changes: 6 additions & 3 deletions actionpack/lib/action_controller/base.rb
Expand Up @@ -939,8 +939,7 @@ def render(options = nil, extra_options = {}, &block) #:doc:
render_for_text(generator.to_s, options[:status])

elsif options[:nothing]
# Safari doesn't pass the headers of the return if the response is zero length
render_for_text(" ", options[:status])
render_for_text(nil, options[:status])

else
render_for_file(default_template_name, options[:status], true)
Expand Down Expand Up @@ -1154,7 +1153,11 @@ def render_for_text(text = nil, status = nil, append_response = false) #:nodoc:
response.body ||= ''
response.body << text.to_s
else
response.body = text.is_a?(Proc) ? text : text.to_s
response.body = case text
when Proc then text
when nil then " " # Safari doesn't pass the headers of the return if the response is zero length
else text.to_s
end
end
end

Expand Down
2 changes: 1 addition & 1 deletion actionpack/lib/action_view/base.rb
Expand Up @@ -263,7 +263,7 @@ def render(options = {}, local_assigns = {}, &block) #:nodoc:
end
elsif options[:file]
render_file(options[:file], nil, options[:locals])
elsif options[:partial] && options[:collection]
elsif options[:partial] && options.has_key?(:collection)
render_partial_collection(options[:partial], options[:collection], options[:spacer_template], options[:locals], options[:as])
elsif options[:partial]
render_partial(options[:partial], options[:object], options[:locals])
Expand Down
2 changes: 1 addition & 1 deletion actionpack/lib/action_view/partials.rb
Expand Up @@ -161,7 +161,7 @@ def render_partial(partial_path, object_assigns = nil, local_assigns = {}) #:nod
end

def render_partial_collection(partial_path, collection, partial_spacer_template = nil, local_assigns = {}, as = nil) #:nodoc:
return " " if collection.empty?
return nil if collection.blank?

local_assigns = local_assigns ? local_assigns.clone : {}
spacer = partial_spacer_template ? render(:partial => partial_spacer_template) : ''
Expand Down
2 changes: 1 addition & 1 deletion actionpack/test/controller/render_test.rb
Expand Up @@ -329,7 +329,7 @@ def test_render_custom_code_rjs
def test_render_text_with_nil
get :render_text_with_nil
assert_response 200
assert_equal '', @response.body
assert_equal ' ', @response.body
end

def test_render_text_with_false
Expand Down
8 changes: 8 additions & 0 deletions actionpack/test/template/render_test.rb
Expand Up @@ -87,6 +87,14 @@ def test_render_partial_collection_without_as
@view.render(:partial => "test/local_inspector", :collection => [ Customer.new("mary") ])
end

def test_render_partial_with_empty_collection_should_return_nil
assert_nil @view.render(:partial => "test/customer", :collection => [])
end

def test_render_partial_with_nil_collection_should_return_nil
assert_nil @view.render(:partial => "test/customer", :collection => nil)
end

# TODO: The reason for this test is unclear, improve documentation
def test_render_partial_and_fallback_to_layout
assert_equal "Before (Josh)\n\nAfter", @view.render(:partial => "test/layout_for_partial", :locals => { :name => "Josh" })
Expand Down

0 comments on commit a8ece12

Please sign in to comment.