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'] = '*' 

Be the first to leave a comment. Don’t be shy.

Join the Discussion

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>