diff --git a/actionpack/lib/action_controller/caching/fragments.rb b/actionpack/lib/action_controller/caching/fragments.rb
index <HASH>..<HASH> 100644
--- a/actionpack/lib/action_controller/caching/fragments.rb
+++ b/actionpack/lib/action_controller/caching/fragments.rb
@@ -41,7 +41,9 @@ module ActionController #:nodoc:
           else
             pos = buffer.length
             block.call
-            write_fragment(name, buffer[pos..-1], options)
+            content = buffer[pos..-1]
+            content = content.as_str if content.respond_to?(:as_str)
+            write_fragment(name, content, options)
           end
         else
           block.call
diff --git a/activesupport/lib/active_support/core_ext/string/output_safety.rb b/activesupport/lib/active_support/core_ext/string/output_safety.rb
index <HASH>..<HASH> 100644
--- a/activesupport/lib/active_support/core_ext/string/output_safety.rb
+++ b/activesupport/lib/active_support/core_ext/string/output_safety.rb
@@ -89,8 +89,12 @@ module ActiveSupport #:nodoc:
       self
     end
 
+    def as_str
+      ''.replace(self)
+    end
+
     def to_yaml
-      "".replace(self).to_yaml
+      as_str.to_yaml
     end
   end
 end
Write strings to fragment cache, not outputbuffers