1

How can i apply this filter for all application routes, when i put this only in application_controller file i still get CORS error.

  before_filter :cors_preflight_check
  after_filter :cors_set_access_control_headers


  def cors_set_access_control_headers
    headers['Access-Control-Allow-Origin'] = '*'
    headers['Access-Control-Allow-Methods'] = 'POST, GET, OPTIONS'
    headers['Access-Control-Max-Age'] = "1728000"
  end

  def cors_preflight_check
    headers['Access-Control-Allow-Origin'] = '*'
    headers['Access-Control-Allow-Methods'] = 'POST, GET, OPTIONS'
    headers['Access-Control-Allow-Headers'] = 'X-Requested-With, X-Prototype-Version'
    headers['Access-Control-Max-Age'] = '1728000'
  end

1 Answers1

0

Do you have OPTIONS method enabled for the paths you want to support CORS on. CORS requests (i.e. POST/PUT) use OPTIONS request on same path. I don't think your controller will get executed if there is no route match.

https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS#Preflighted_requests

Like suggested by @pavel-tkackenko you could use https://github.com/cyu/rack-cors or create your own middleware.

Relevant: Rails Responds with 404 on CORS Preflight Options Request

Community
  • 1
  • 1
amitamb
  • 1,047
  • 12
  • 22