From 103e18c87d50a53cd0a33b4e03f2c8a8eff19ece Mon Sep 17 00:00:00 2001 From: Prem Sichanugrist Date: Fri, 31 Jan 2014 15:37:58 -0500 Subject: Introduce `render :body` for render raw content This is an option for sending a raw content back to browser. Note that this rendering option will unset the default content type and does not include "Content-Type" header back in the response. You should only use this option if you are expecting the "Content-Type" header to not be set. More information on "Content-Type" header can be found on RFC 2616, section 7.2.1. Please see #12374 for more detail. --- actionpack/lib/abstract_controller/rendering.rb | 4 ++-- actionpack/lib/action_controller/metal/rendering.rb | 17 +++++++++++++---- actionpack/lib/action_dispatch/http/response.rb | 2 +- 3 files changed, 16 insertions(+), 7 deletions(-) (limited to 'actionpack/lib') diff --git a/actionpack/lib/abstract_controller/rendering.rb b/actionpack/lib/abstract_controller/rendering.rb index f24b03ad16..349bbf4ee7 100644 --- a/actionpack/lib/abstract_controller/rendering.rb +++ b/actionpack/lib/abstract_controller/rendering.rb @@ -23,7 +23,7 @@ module AbstractController def render(*args, &block) options = _normalize_render(*args, &block) self.response_body = render_to_body(options) - _process_format(rendered_format) if rendered_format + _process_format(rendered_format, options) if rendered_format self.response_body end @@ -98,7 +98,7 @@ module AbstractController # Process the rendered format. # :api: private - def _process_format(format) + def _process_format(format, options = {}) end # Normalize args and options. diff --git a/actionpack/lib/action_controller/metal/rendering.rb b/actionpack/lib/action_controller/metal/rendering.rb index 5c48b4ab98..c9ae1ab388 100644 --- a/actionpack/lib/action_controller/metal/rendering.rb +++ b/actionpack/lib/action_controller/metal/rendering.rb @@ -27,14 +27,19 @@ module ActionController end def render_to_body(options = {}) - super || options[:text].presence || ' ' + super || options[:body].presence || options[:text].presence || ' ' end private - def _process_format(format) + def _process_format(format, options = {}) super self.content_type ||= format.to_s + + if options[:body].present? + self.content_type = "none" + self.headers.delete "Content-Type" + end end # Normalize arguments by catching blocks and setting them on :update. @@ -46,12 +51,16 @@ module ActionController # Normalize both text and status options. def _normalize_options(options) #:nodoc: + if options.key?(:body) && options[:body].respond_to?(:to_text) + options[:body] = options[:body].to_text + end + if options.key?(:text) && options[:text].respond_to?(:to_text) options[:text] = options[:text].to_text end - if options.delete(:nothing) || (options.key?(:text) && options[:text].nil?) - options[:text] = " " + if options.delete(:nothing) || (options.key?(:body) && options[:body].nil?) || (options.key?(:text) && options[:text].nil?) + options[:body] = " " end if options[:status] diff --git a/actionpack/lib/action_dispatch/http/response.rb b/actionpack/lib/action_dispatch/http/response.rb index 2c6bcf7b7b..15a177aaff 100644 --- a/actionpack/lib/action_dispatch/http/response.rb +++ b/actionpack/lib/action_dispatch/http/response.rb @@ -288,7 +288,7 @@ module ActionDispatch # :nodoc: end def assign_default_content_type_and_charset!(headers) - return if headers[CONTENT_TYPE].present? + return if headers[CONTENT_TYPE].present? || @content_type == "none" @content_type ||= Mime::HTML @charset ||= self.class.default_charset unless @charset == false -- cgit v1.2.3