In some Rails app, I saw this in the routes.rb
root :to => "home#index", :via => [:get]
root :to => "accounts#manage", :via => [:options]
I could not understand how these two root URLs can exist. Googling didn't help clear the :options argument either. Can anyone help?
Thanks
The :as option creates a named path. You can then call this path in your controllers and views (e.g. redirect_to things_path ). This isn't very useful for the root path (as it already named root ), but is very useful for new routes you add.
Rails RESTful Design which creates seven routes all mapping to the user controller. Rails also allows you to define multiple resources in one line.
Rails routing is a two-way piece of machinery – rather as if you could turn trees into paper, and then turn paper back into trees. Specifically, it both connects incoming HTTP requests to the code in your application's controllers, and helps you generate URLs without having to hard-code them as strings.
TIP: If you ever want to list all the routes of your application you can use rails routes on your terminal and if you want to list routes of a specific resource, you can use rails routes | grep hotel . This will list all the routes of Hotel.
As per the HTTP spec (and explained a bit more here), there is an OPTIONS verb - which routes can support.
The impetus for using OPTIONS is to request documentation for a web service API; results are meant to provide information regarding how the API may be used.
ActionDispatch::Routing::HTTP_METHODS
=> [:get, :head, :post, :put, :delete, :options]
To get back to your question, in a typical browser GET request, the first route will be used. When an OPTIONS request is made, the second route will be used.
You can use the :via option to constrain the request to one or more HTTP methods
See the rails guide on routing
:post
, :get
, :put
, :delete
, :options
, :head
, and :any
are allowed as a value to this option.
As explained in a blog post, OPTIONS is just another HTTP verb to support CORS requests (a way to make cross domain AJAX requests).
Update found a blog post explaining :options
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With