Represents an HTTP response generated by a controller action. One can use an ActionController::AbstractResponse object to retrieve the current state of the response, or customize the response. An AbstractResponse 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.

AbstractResponse 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 AbstractResponse#headers.

Nevertheless, integration tests may want to inspect controller responses in more detail, and that‘s when AbstractResponse 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 AbstractResponse).

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
Methods
Constants
DEFAULT_HEADERS = { "Cache-Control" => "no-cache" }
Attributes
[RW] assigns
[RW] body The body content (e.g. HTML) of the response, as a String.
[RW] cookies
[RW] headers The headers of the response, as a Hash. It maps header names to header values.
[RW] layout
[RW] redirected_to
[RW] redirected_to_method_params
[RW] request
[RW] session
[RW] template
Public Class methods
new()
    # File vendor/rails/actionpack/lib/action_controller/response.rb, line 45
45:     def initialize
46:       @body, @headers, @session, @assigns = "", DEFAULT_HEADERS.merge("cookie" => []), [], []
47:     end
Public Instance methods
assign_default_content_type_and_charset!()
     # File vendor/rails/actionpack/lib/action_controller/response.rb, line 131
131:     def assign_default_content_type_and_charset!
132:       self.content_type ||= Mime::HTML
133:       self.charset ||= default_charset unless sending_file?
134:     end
charset()
    # File vendor/rails/actionpack/lib/action_controller/response.rb, line 90
90:     def charset
91:       charset = String(headers["Content-Type"] || headers["type"]).split(";")[1]
92:       charset.blank? ? nil : charset.strip.split("=")[1]
93:     end
charset=(charset)

Set the charset of the Content-Type header. Set to nil to remove it. If no content type is set, it defaults to HTML.

    # File vendor/rails/actionpack/lib/action_controller/response.rb, line 81
81:     def charset=(charset)
82:       headers["Content-Type"] =
83:         if charset
84:           "#{content_type || Mime::HTML}; charset=#{charset}"
85:         else
86:           content_type || Mime::HTML.to_s
87:         end
88:     end
content_type()

Returns the response‘s content MIME type, or nil if content type has been set.

    # File vendor/rails/actionpack/lib/action_controller/response.rb, line 74
74:     def content_type
75:       content_type = String(headers["Content-Type"] || headers["type"]).split(";")[0]
76:       content_type.blank? ? nil : content_type
77:     end
content_type=(mime_type)

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.

    # File vendor/rails/actionpack/lib/action_controller/response.rb, line 64
64:     def content_type=(mime_type)
65:       self.headers["Content-Type"] =
66:         if mime_type =~ /charset/ || (c = charset).nil?
67:           mime_type.to_s
68:         else
69:           "#{mime_type}; charset=#{c}"
70:         end
71:     end
etag()
     # File vendor/rails/actionpack/lib/action_controller/response.rb, line 109
109:     def etag
110:       headers['ETag']
111:     end
etag=(etag)
     # File vendor/rails/actionpack/lib/action_controller/response.rb, line 117
117:     def etag=(etag)
118:       headers['ETag'] = %("#{Digest::MD5.hexdigest(ActiveSupport::Cache.expand_cache_key(etag))}")
119:     end
etag?()
     # File vendor/rails/actionpack/lib/action_controller/response.rb, line 113
113:     def etag?
114:       headers.include?('ETag')
115:     end
last_modified()
    # File vendor/rails/actionpack/lib/action_controller/response.rb, line 95
95:     def last_modified
96:       if last = headers['Last-Modified']
97:         Time.httpdate(last)
98:       end
99:     end
last_modified=(utc_time)
     # File vendor/rails/actionpack/lib/action_controller/response.rb, line 105
105:     def last_modified=(utc_time)
106:       headers['Last-Modified'] = utc_time.httpdate
107:     end
last_modified?()
     # File vendor/rails/actionpack/lib/action_controller/response.rb, line 101
101:     def last_modified?
102:       headers.include?('Last-Modified')
103:     end
location()
    # File vendor/rails/actionpack/lib/action_controller/response.rb, line 52
52:     def location; headers['Location'] end
location=(url)
    # File vendor/rails/actionpack/lib/action_controller/response.rb, line 53
53:     def location=(url) headers['Location'] = url end
prepare!()
     # File vendor/rails/actionpack/lib/action_controller/response.rb, line 136
136:     def prepare!
137:       assign_default_content_type_and_charset!
138:       handle_conditional_get!
139:       set_content_length!
140:       convert_content_type!
141:     end
redirect(url, status)
     # File vendor/rails/actionpack/lib/action_controller/response.rb, line 121
121:     def redirect(url, status)
122:       self.status = status
123:       self.location = url.gsub(/[\r\n]/, '')
124:       self.body = "<html><body>You are being <a href=\"#{CGI.escapeHTML(url)}\">redirected</a>.</body></html>"
125:     end
sending_file?()
     # File vendor/rails/actionpack/lib/action_controller/response.rb, line 127
127:     def sending_file?
128:       headers["Content-Transfer-Encoding"] == "binary"
129:     end
status()
    # File vendor/rails/actionpack/lib/action_controller/response.rb, line 49
49:     def status; headers['Status'] end
status=(status)
    # File vendor/rails/actionpack/lib/action_controller/response.rb, line 50
50:     def status=(status) headers['Status'] = status end