public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Return nil instead of a space when passing an empty collection or nil to 'render 
:partial' [#791 state:resolved]

Signed-off-by: Joshua Peek <josh@joshpeek.com>
ryanb (author)
Tue Aug 19 17:09:04 -0700 2008
josh (committer)
Tue Aug 19 17:09:04 -0700 2008
commit  a8ece12fe2ac7838407954453e0d31af6186a5db
tree    5a6cb0a957bebc1da95c1918563db10447cacd26
parent  71c4ff07ab4313c1f4781d59ad2f4528f5875665
...
939
940
941
942
943
 
944
945
946
...
1154
1155
1156
1157
 
 
 
 
 
1158
1159
1160
...
939
940
941
 
 
942
943
944
945
...
1153
1154
1155
 
1156
1157
1158
1159
1160
1161
1162
1163
0
@@ -939,8 +939,7 @@ module ActionController #:nodoc:
0
             render_for_text(generator.to_s, options[:status])
0
 
0
           elsif options[:nothing]
0
-            # Safari doesn't pass the headers of the return if the response is zero length
0
-            render_for_text(" ", options[:status])
0
+            render_for_text(nil, options[:status])
0
 
0
           else
0
             render_for_file(default_template_name, options[:status], true)
0
@@ -1154,7 +1153,11 @@ module ActionController #:nodoc:
0
           response.body ||= ''
0
           response.body << text.to_s
0
         else
0
-          response.body = text.is_a?(Proc) ? text : text.to_s
0
+          response.body = case text
0
+            when Proc then text
0
+            when nil  then " " # Safari doesn't pass the headers of the return if the response is zero length
0
+            else           text.to_s
0
+          end
0
         end
0
       end
0
 
...
263
264
265
266
 
267
268
269
...
263
264
265
 
266
267
268
269
0
@@ -263,7 +263,7 @@ module ActionView #:nodoc:
0
           end
0
         elsif options[:file]
0
           render_file(options[:file], nil, options[:locals])
0
-        elsif options[:partial] && options[:collection]
0
+        elsif options[:partial] && options.has_key?(:collection)
0
           render_partial_collection(options[:partial], options[:collection], options[:spacer_template], options[:locals], options[:as])
0
         elsif options[:partial]
0
           render_partial(options[:partial], options[:object], options[:locals])
...
161
162
163
164
 
165
166
167
...
161
162
163
 
164
165
166
167
0
@@ -161,7 +161,7 @@ module ActionView
0
       end
0
 
0
       def render_partial_collection(partial_path, collection, partial_spacer_template = nil, local_assigns = {}, as = nil) #:nodoc:
0
-        return " " if collection.empty?
0
+        return nil if collection.blank?
0
 
0
         local_assigns = local_assigns ? local_assigns.clone : {}
0
         spacer = partial_spacer_template ? render(:partial => partial_spacer_template) : ''
...
329
330
331
332
 
333
334
335
...
329
330
331
 
332
333
334
335
0
@@ -329,7 +329,7 @@ class RenderTest < Test::Unit::TestCase
0
   def test_render_text_with_nil
0
     get :render_text_with_nil
0
     assert_response 200
0
-    assert_equal '', @response.body
0
+    assert_equal ' ', @response.body
0
   end
0
 
0
   def test_render_text_with_false
...
87
88
89
 
 
 
 
 
 
 
 
90
91
92
...
87
88
89
90
91
92
93
94
95
96
97
98
99
100
0
@@ -87,6 +87,14 @@ class ViewRenderTest < Test::Unit::TestCase
0
       @view.render(:partial => "test/local_inspector", :collection => [ Customer.new("mary") ])
0
   end
0
 
0
+  def test_render_partial_with_empty_collection_should_return_nil
0
+    assert_nil @view.render(:partial => "test/customer", :collection => [])
0
+  end
0
+
0
+  def test_render_partial_with_nil_collection_should_return_nil
0
+    assert_nil @view.render(:partial => "test/customer", :collection => nil)
0
+  end
0
+
0
   # TODO: The reason for this test is unclear, improve documentation
0
   def test_render_partial_and_fallback_to_layout
0
     assert_equal "Before (Josh)\n\nAfter", @view.render(:partial => "test/layout_for_partial", :locals => { :name => "Josh" })

Comments