Handling Content Types
When a request comes in Mack will attempt to determine it's content type based on the extension of the request coming in. For example '/foo.xml' will automatically look up and set the 'Content-Type' header based on the Mack::Utils::MimeTypes registry. By default Mack will assume the type to be 'html'.
It's easy to override the content type for a request, there are a couple ways of doing so:
class UserController
include Mack::Controller
def download
response.content_type = Mack::Utils::MimeTypes[:rtf]
end
end
In the above example the content type will be set to 'application/rtf; application/x-rtf; text/richtext'. You obviously don't have to use the Mack::Utilss::MimeTypes registry, but it's a very convenient way of managing your different mime types, and it comes pre-loaded with 450 of them!
You can also set the content type when you call the render method, as such:
class UserController
include Mack::Controller
def download
render(:text, File.read('/path/to/file'), :content_type => 'application/pdf')
end
end
Sometimes when when developing you want to be able to handle different content types differently in your actions, that's where the wants method comes in handy.
class UserController
include Mack::Controller
def download
# do common things...
wants(:xml) do
# do something specific for xml
end
wants(:js) do
# do something specific for javascript
end
# do common things...
end
end
Anything wrapped in a wants block will only be called if that content type has been requested by the end user.