Class ActionController::Response
In: vendor/rails/actionpack/lib/action_controller/response.rb
Parent: Rack::Response

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

Methods

Constants

DEFAULT_HEADERS = { "Cache-Control" => "no-cache" }

Attributes

assigns  [RW] 
layout  [RW] 
redirected_to  [RW] 
redirected_to_method_params  [RW] 
request  [RW] 
session  [RW] 
template  [RW] 

Public Class methods

[Source]

    # File vendor/rails/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

Public Instance methods

[Source]

     # File vendor/rails/actionpack/lib/action_controller/response.rb, line 138
138:     def assign_default_content_type_and_charset!
139:       self.content_type ||= Mime::HTML
140:       self.charset ||= default_charset unless sending_file?
141:     end

[Source]

    # File vendor/rails/actionpack/lib/action_controller/response.rb, line 93
93:     def charset
94:       charset = String(headers["Content-Type"] || headers["type"]).split(";")[1]
95:       charset.blank? ? nil : charset.strip.split("=")[1]
96:     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.

[Source]

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

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

[Source]

    # File vendor/rails/actionpack/lib/action_controller/response.rb, line 77
77:     def content_type
78:       content_type = String(headers["Content-Type"] || headers["type"]).split(";")[0]
79:       content_type.blank? ? nil : content_type
80:     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.

[Source]

    # File vendor/rails/actionpack/lib/action_controller/response.rb, line 66
66:     def content_type=(mime_type)
67:       new_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:       self.headers["Content-Type"] = URI.escape(new_content_type, "\r\n")
74:     end

[Source]

     # File vendor/rails/actionpack/lib/action_controller/response.rb, line 152
152:     def each(&callback)
153:       if @body.respond_to?(:call)
154:         @writer = lambda { |x| callback.call(x) }
155:         @body.call(self, self)
156:       elsif @body.respond_to?(:to_str)
157:         yield @body
158:       else
159:         @body.each(&callback)
160:       end
161: 
162:       @writer = callback
163:       @block.call(self) if @block
164:     end

[Source]

     # File vendor/rails/actionpack/lib/action_controller/response.rb, line 112
112:     def etag
113:       headers['ETag']
114:     end

[Source]

     # File vendor/rails/actionpack/lib/action_controller/response.rb, line 120
120:     def etag=(etag)
121:       if etag.blank?
122:         headers.delete('ETag')
123:       else
124:         headers['ETag'] = %("#{Digest::MD5.hexdigest(ActiveSupport::Cache.expand_cache_key(etag))}")
125:       end
126:     end

[Source]

     # File vendor/rails/actionpack/lib/action_controller/response.rb, line 116
116:     def etag?
117:       headers.include?('ETag')
118:     end

[Source]

     # File vendor/rails/actionpack/lib/action_controller/response.rb, line 98
 98:     def last_modified
 99:       if last = headers['Last-Modified']
100:         Time.httpdate(last)
101:       end
102:     end

[Source]

     # File vendor/rails/actionpack/lib/action_controller/response.rb, line 108
108:     def last_modified=(utc_time)
109:       headers['Last-Modified'] = utc_time.httpdate
110:     end

[Source]

     # File vendor/rails/actionpack/lib/action_controller/response.rb, line 104
104:     def last_modified?
105:       headers.include?('Last-Modified')
106:     end

[Source]

    # File vendor/rails/actionpack/lib/action_controller/response.rb, line 54
54:     def location; headers['Location'] end

[Source]

    # File vendor/rails/actionpack/lib/action_controller/response.rb, line 55
55:     def location=(url) headers['Location'] = url end

[Source]

     # File vendor/rails/actionpack/lib/action_controller/response.rb, line 143
143:     def prepare!
144:       assign_default_content_type_and_charset!
145:       handle_conditional_get!
146:       set_content_length!
147:       convert_content_type!
148:       convert_language!
149:       convert_cookies!
150:     end

[Source]

     # File vendor/rails/actionpack/lib/action_controller/response.rb, line 128
128:     def redirect(url, status)
129:       self.status = status
130:       self.location = url.gsub(/[\r\n]/, '')
131:       self.body = "<html><body>You are being <a href=\"#{CGI.escapeHTML(url)}\">redirected</a>.</body></html>"
132:     end

[Source]

     # File vendor/rails/actionpack/lib/action_controller/response.rb, line 134
134:     def sending_file?
135:       headers["Content-Transfer-Encoding"] == "binary"
136:     end

[Source]

     # File vendor/rails/actionpack/lib/action_controller/response.rb, line 177
177:     def set_cookie(key, value)
178:       if value.has_key?(:http_only)
179:         ActiveSupport::Deprecation.warn(
180:           "The :http_only option in ActionController::Response#set_cookie " +
181:           "has been renamed. Please use :httponly instead.", caller)
182:         value[:httponly] ||= value.delete(:http_only)
183:       end
184: 
185:       super(key, value)
186:     end

[Source]

     # File vendor/rails/actionpack/lib/action_controller/response.rb, line 166
166:     def write(str)
167:       @writer.call str.to_s
168:       str
169:     end

[Validate]