[:get] root :to =...">

: via => [: options] in ruby ​​along rails

In some Rails applications, I saw this in route.rb

root :to => "home#index", :via => [:get] root :to => "accounts#manage", :via => [:options] 

I could not understand how these two root URLs could exist. Googling did not help clear the argument: options. Can anyone help?

thanks

+6
source share
2 answers

According to the HTTP specification (and explained a little more here ), there is the verb OPTIONS - which routes can support.

The impulse to use OPTIONS is to request documentation for the web service API; The results are intended to provide information on how to use the API.

 ActionDispatch::Routing::HTTP_METHODS => [:get, :head, :post, :put, :delete, :options] 

To get back to your question, the first route will be used in a typical browser GET request. When the OPTIONS request is executed, the second route will be used.

+6
source

You can use the: via option to restrict the request to one or more HTTP methods.

See the routing guide for routing.

:post :get :put :delete :options :head and :any allowed as the value for this option.

As explained in the blog post , OPTIONS is another HTTP verb to support CORS Requests (a way to make AJAX requests for cross domains).

Refresh found a blog post explaining :options

+1
source

Source: https://habr.com/ru/post/943641/


All Articles