Represents an HTTP response generated by a controller action. One can use an ActionController::Response object to retrieve the current state of the response, or customize the response. An Response object can either represent a "real" HTTP response (i.e. one that is meant to be sent back to the web browser) or a test response (i.e. one that is generated from integration tests). See CgiResponse and TestResponse, respectively.
Response is mostly a Ruby on Rails framework implement detail, and should never be used directly in controllers. Controllers should use the methods defined in ActionController::Base instead. For example, if you want to set the HTTP response‘s content MIME type, then use ActionControllerBase#headers instead of Response#headers.
Nevertheless, integration tests may want to inspect controller responses in more detail, and that‘s when Response can be useful for application developers. Integration test methods such as ActionController::Integration::Session#get and ActionController::Integration::Session#post return objects of type TestResponse (which are of course also of type Response).
For example, the following demo integration "test" prints the body of the controller response to the console:
class DemoControllerTest < ActionController::IntegrationTest def test_print_root_path_to_console get('/') puts @response.body end end
- assign_default_content_type_and_charset!
- charset
- charset=
- content_type
- content_type=
- each
- etag
- etag=
- etag?
- last_modified
- last_modified=
- last_modified?
- location
- location=
- new
- prepare!
- redirect
- sending_file?
- set_cookie
- write
DEFAULT_HEADERS | = | { "Cache-Control" => "no-cache" } |
[RW] | assigns | |
[RW] | layout | |
[RW] | redirected_to | |
[RW] | redirected_to_method_params | |
[RW] | request | |
[RW] | session | |
[RW] | template |
[ show source ]
# File actionpack/lib/action_controller/response.rb, line 42 42: def initialize 43: @status = 200 44: @header = Rack::Utils::HeaderHash.new(DEFAULT_HEADERS) 45: 46: @writer = lambda { |x| @body << x } 47: @block = nil 48: 49: @body = "", 50: @session = [] 51: @assigns = [] 52: end
[ show source ]
# File actionpack/lib/action_controller/response.rb, line 133 133: def assign_default_content_type_and_charset! 134: self.content_type ||= Mime::HTML 135: self.charset ||= default_charset unless sending_file? 136: end
[ show source ]
# File actionpack/lib/action_controller/response.rb, line 92 92: def charset 93: charset = String(headers["Content-Type"] || headers["type"]).split(";")[1] 94: charset.blank? ? nil : charset.strip.split("=")[1] 95: end
Set the charset of the Content-Type header. Set to nil to remove it. If no content type is set, it defaults to HTML.
[ show source ]
# File actionpack/lib/action_controller/response.rb, line 83 83: def charset=(charset) 84: headers["Content-Type"] = 85: if charset 86: "#{content_type || Mime::HTML}; charset=#{charset}" 87: else 88: content_type || Mime::HTML.to_s 89: end 90: end
Returns the response‘s content MIME type, or nil if content type has been set.
[ show source ]
# File actionpack/lib/action_controller/response.rb, line 76 76: def content_type 77: content_type = String(headers["Content-Type"] || headers["type"]).split(";")[0] 78: content_type.blank? ? nil : content_type 79: end
Sets the HTTP response‘s content MIME type. For example, in the controller you could write this:
response.content_type = "text/plain"
If a character set has been defined for this response (see charset=) then the character set information will also be included in the content type information.
[ show source ]
# File actionpack/lib/action_controller/response.rb, line 66 66: def content_type=(mime_type) 67: self.headers["Content-Type"] = 68: if mime_type =~ /charset/ || (c = charset).nil? 69: mime_type.to_s 70: else 71: "#{mime_type}; charset=#{c}" 72: end 73: end
[ show source ]
# File actionpack/lib/action_controller/response.rb, line 147 147: def each(&callback) 148: if @body.respond_to?(:call) 149: @writer = lambda { |x| callback.call(x) } 150: @body.call(self, self) 151: elsif @body.respond_to?(:to_str) 152: yield @body 153: else 154: @body.each(&callback) 155: end 156: 157: @writer = callback 158: @block.call(self) if @block 159: end
[ show source ]
# File actionpack/lib/action_controller/response.rb, line 111 111: def etag 112: headers['ETag'] 113: end
[ show source ]
# File actionpack/lib/action_controller/response.rb, line 119 119: def etag=(etag) 120: headers['ETag'] = %("#{Digest::MD5.hexdigest(ActiveSupport::Cache.expand_cache_key(etag))}") 121: end
[ show source ]
# File actionpack/lib/action_controller/response.rb, line 115 115: def etag? 116: headers.include?('ETag') 117: end
[ show source ]
# File actionpack/lib/action_controller/response.rb, line 97 97: def last_modified 98: if last = headers['Last-Modified'] 99: Time.httpdate(last) 100: end 101: end
[ show source ]
# File actionpack/lib/action_controller/response.rb, line 107 107: def last_modified=(utc_time) 108: headers['Last-Modified'] = utc_time.httpdate 109: end
[ show source ]
# File actionpack/lib/action_controller/response.rb, line 103 103: def last_modified? 104: headers.include?('Last-Modified') 105: end
[ show source ]
# File actionpack/lib/action_controller/response.rb, line 54 54: def location; headers['Location'] end
[ show source ]
# File actionpack/lib/action_controller/response.rb, line 55 55: def location=(url) headers['Location'] = url end
[ show source ]
# File actionpack/lib/action_controller/response.rb, line 138 138: def prepare! 139: assign_default_content_type_and_charset! 140: handle_conditional_get! 141: set_content_length! 142: convert_content_type! 143: convert_language! 144: convert_cookies! 145: end
[ show source ]
# File actionpack/lib/action_controller/response.rb, line 123 123: def redirect(url, status) 124: self.status = status 125: self.location = url.gsub(/[\r\n]/, '') 126: self.body = "<html><body>You are being <a href=\"#{CGI.escapeHTML(url)}\">redirected</a>.</body></html>" 127: end
[ show source ]
# File actionpack/lib/action_controller/response.rb, line 129 129: def sending_file? 130: headers["Content-Transfer-Encoding"] == "binary" 131: end
[ show source ]
# File actionpack/lib/action_controller/response.rb, line 172 172: def set_cookie(key, value) 173: if value.has_key?(:http_only) 174: ActiveSupport::Deprecation.warn( 175: "The :http_only option in ActionController::Response#set_cookie " + 176: "has been renamed. Please use :httponly instead.", caller) 177: value[:httponly] ||= value.delete(:http_only) 178: end 179: 180: super(key, value) 181: end
[ show source ]
# File actionpack/lib/action_controller/response.rb, line 161 161: def write(str) 162: @writer.call str.to_s 163: str 164: end