Tuesday, May 3, 2011

Custom Mime types in Rails

To create a custom Mime type, you need to give it a name to be used in Rails under the format section in any controller.

You also need to provide a 'class/type' string to be recognized by the request/response protocol (i.e. in http headers).

To do this, open the folder "config/initializers" in your project, you'll find a file called "mime_types.rb".

Inside that file, add the following line:
Mime::Type.register "class/type", :name
For example, we use "text/ajax" and :ajax to provide a way of telling the controller that I'm requesting an ajax page, so no layout is rendered. More on this scenario later.

Please comment if you find any better solution or find a new use case for this tip.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.