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
Did you know?
Jakob is an independent web application developer who builds awesome stuff for the web. You can hire him to build awesome stuff for you.

Comments and Trackbacks

Commenting on this entry has been closed.