File tree Expand file tree Collapse file tree 7 files changed +45
-12
lines changed Expand file tree Collapse file tree 7 files changed +45
-12
lines changed Original file line number Diff line number Diff line change
1
+ * Allow setting content type with a symbol of the Mime type.
2
+
3
+ ``` ruby
4
+ # Before
5
+ response.content_type = " text/html"
6
+
7
+ # After
8
+ response.content_type = :html
9
+ ```
10
+
11
+ * Petrik de Heus *
1
12
2
13
Please check [8 - 0 - stable](https: // github.com/ rails/ rails/ blob/ 8 - 0 - stable/ actionpack/ CHANGELOG .md) for previous changes.
Original file line number Diff line number Diff line change @@ -132,9 +132,7 @@ def send_file_headers!(options)
132
132
raise ArgumentError , ":type option required" if content_type . nil?
133
133
134
134
if content_type . is_a? ( Symbol )
135
- extension = Mime [ content_type ]
136
- raise ArgumentError , "Unknown MIME type #{ options [ :type ] } " unless extension
137
- self . content_type = extension
135
+ self . content_type = content_type
138
136
else
139
137
if !type_provided && options [ :filename ]
140
138
# If type wasn't provided, try guessing from file extension.
Original file line number Diff line number Diff line change @@ -41,7 +41,7 @@ def head(status, options = nil)
41
41
42
42
if include_content? ( response_code )
43
43
unless self . media_type
44
- self . content_type = content_type || ( ( f = formats ) && Mime [ f . first ] ) || Mime [ :html ]
44
+ self . content_type = content_type || ( ( f = formats ) && Mime [ f . first ] ) || :html
45
45
end
46
46
47
47
response . charset = false
Original file line number Diff line number Diff line change @@ -157,23 +157,23 @@ def _render_to_body_with_renderer(options)
157
157
158
158
if options [ :callback ] . present?
159
159
if media_type . nil? || media_type == Mime [ :json ]
160
- self . content_type = Mime [ :js ]
160
+ self . content_type = :js
161
161
end
162
162
163
163
"/**/#{ options [ :callback ] } (#{ json } )"
164
164
else
165
- self . content_type = Mime [ :json ] if media_type . nil?
165
+ self . content_type = :json if media_type . nil?
166
166
json
167
167
end
168
168
end
169
169
170
170
add :js do |js , options |
171
- self . content_type = Mime [ :js ] if media_type . nil?
171
+ self . content_type = :js if media_type . nil?
172
172
js . respond_to? ( :to_js ) ? js . to_js ( options ) : js
173
173
end
174
174
175
175
add :xml do |xml , options |
176
- self . content_type = Mime [ :xml ] if media_type . nil?
176
+ self . content_type = :xml if media_type . nil?
177
177
xml . respond_to? ( :to_xml ) ? xml . to_xml ( options ) : xml
178
178
end
179
179
end
Original file line number Diff line number Diff line change @@ -208,7 +208,7 @@ def _render_in_priorities(options)
208
208
end
209
209
210
210
def _set_html_content_type
211
- self . content_type = Mime [ :html ] . to_s
211
+ self . content_type = :html
212
212
end
213
213
214
214
def _set_rendered_content_type ( format )
Original file line number Diff line number Diff line change @@ -263,14 +263,27 @@ def status=(status)
263
263
# Sets the HTTP response's content MIME type. For example, in the controller you
264
264
# could write this:
265
265
#
266
- # response.content_type = "text/plain"
266
+ # response.content_type = "text/html"
267
+ #
268
+ # This method also accepts a symbol with the extension of the MIME type:
269
+ #
270
+ # response.content_type = :html
267
271
#
268
272
# If a character set has been defined for this response (see #charset=) then the
269
273
# character set information will also be included in the content type
270
274
# information.
271
275
def content_type = ( content_type )
272
- return unless content_type
273
- new_header_info = parse_content_type ( content_type . to_s )
276
+ case content_type
277
+ when NilClass
278
+ return
279
+ when Symbol
280
+ mime_type = Mime [ content_type ]
281
+ raise ArgumentError , "Unknown MIME type #{ content_type } " unless mime_type
282
+ new_header_info = ContentTypeHeader . new ( mime_type . to_s )
283
+ else
284
+ new_header_info = parse_content_type ( content_type . to_s )
285
+ end
286
+
274
287
prev_header_info = parsed_content_type_header
275
288
charset = new_header_info . charset || prev_header_info . charset
276
289
charset ||= self . class . default_charset unless prev_header_info . mime_type
Original file line number Diff line number Diff line change @@ -55,6 +55,11 @@ def render_content_type_with_charset
55
55
response . content_type = "text/html; fragment; charset=utf-16"
56
56
render body : "hello world!"
57
57
end
58
+
59
+ def render_content_type_with_symbol
60
+ response . content_type = :rss
61
+ render body : "hello world!"
62
+ end
58
63
end
59
64
60
65
class ContentTypeTest < ActionController ::TestCase
@@ -142,6 +147,12 @@ def test_content_type_with_charset
142
147
assert_equal "utf-16" , @response . charset
143
148
end
144
149
150
+ def test_content_type_with_symbol
151
+ get :render_content_type_with_symbol
152
+ assert_equal Mime [ :rss ] , @response . media_type
153
+ assert_equal "utf-8" , @response . charset
154
+ end
155
+
145
156
private
146
157
def with_default_charset ( charset )
147
158
old_default_charset = ActionDispatch ::Response . default_charset
You can’t perform that action at this time.
0 commit comments