1

Edit: I re-added the options method in a pull request to Rails which should now be live. The answer below should no longer be necessary. Call process(:options, path, **args) in order to preform the options request.

See commit 1f979184efc27e73f42c5d86c7f19437c6719612 for more information if required.

I've read around the other answers and none of them seemed to work in Rails 5. It's surprising that Rails doesn't just ship with an options method, but here we are. Of course if you can use xdomain, you probably should (edit: I no longer hold this view, there are advantages to CORS) because it's both faster (no preflight check doubling latency!), easier (no need for silly headers / HTTP methods!), and more supported (works basically everywhere!) but sometimes you just need to support CORS and something about the CORS gem makes it not work for you.

zachaysan
  • 1,482
  • 14
  • 28

1 Answers1

1

At the top of your config/routes.rb file place the following:

match "/example/",
  controller: "example_controller",
  action: "options_request",
  via: [:options]

And in your controller write:

def options_request
  # Your code goes here.
end

If you are interested in writing an integration test there is some misinformation around the process method, which is not actually a public method. In order to support OPTIONS requests from your integration tests create an initializer (mine is at: config/initializers/integration_test_overrides.rb because I override a number of things) and add the following code:

class ActionDispatch::Integration::Session

  def http_options_request(path)
    process(:options, path)
  end

end

So that you can call http_options_request from your integration test.

zachaysan
  • 1,482
  • 14
  • 28