Setting the request content type in Rails

Journal entry
November 14, 2005

To set the content type and character set of your responses of your Rails applicaiton, add this to your Application Controller (/app/controllers/application.rb):

class ApplicationController < ActionController::Base
  before_filter :set_content_type
  def set_content_type
    @headers["Content-Type"] = "text/html; charset=utf-8" 
  end    
end

This can obviously be set in any other controller if you need special contenttypes, for example for a feeds controller:

class FeedsController < ApplicationController
  before_filter :set_content_type
  def set_content_type
    @headers["Content-Type"] = "text/xml; charset=utf-8"
  end
end
Categories
Selling out

Comments and Trackbacks

Commenting on this entry has been closed.

... but if you're a dirty spambot, feel free to post a comment here anyways.