rb. Each mapping has a set of MIME types, as well as the Rails
symbol that is used to denote those types (e.g., :html or :xml). At the time of this
writing, the following types are recognized.
Shortcut MIME types
all */*
text text/plain
html text/html, application/xhtml+xml
js text/javascript, application/javascript, application/x-javascript
css text/css
ics text/calendar
csv text/csv
RESTful Rails | 219
New MIME types can be registered with Mime::Type.register. This method takes
four arguments: the primary MIME type, the Rails shortcut, a set of synonym MIME
types (such as text/x-json for JSON text), and a set of synonym file extensions, used
to force a format where the client does not send an Accept header or sends an
improper one.
The Rails shortcut symbol, such as rss, is also taken to be a file extension;
a request URI ending in .rss will trigger a format.rss block. The
list of synonym extensions adds to this default extension.
For example, suppose we want to add JPEG format support to an application. We
would like to write format.jpg in a respond_to block to render a JPEG response. This
requires mapping the jpg format type to the image/jpeg type, as well as the jpg and
jpeg extensions. We can do this by simply putting the following in our config/
initializers/mime_types.rb:
Mime::Type.register "image/jpeg", :jpg, [], %w(jpeg)
HTTP Caching
Earlier in the chapter, we discussed HTTP??™s use of conditional GET for client-side
caching.
Pages:
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344