Setting Access-Control-Allow-Origin for a rails application

I was experimenting with AngularJS and using Rails as a backend and needed to enable Access-Control-Allow-Origin for $http. Here is a simple snippet I created that does the trick. If you were doing this in production, I would assume Apache or Nginx would be the appropriate place for this code. However, this is just a POC.

# application_controller.rb
after_filter :local_access_control_headers

def local_access_control_headers
  headers['Access-Control-Allow-Origin'] = 'http://localhost:8000'
  headers['Access-Control-Request-Method'] = '*'
end

Leave a Reply

Your email address will not be published. Required fields are marked *