在某些 Rails 应用程序中,我在 routes.rb 中看到了这一点
root :to => "home#index", :via => [:get]
root :to => "accounts#manage", :via => [:options]
我无法理解这两个根 URL 是如何存在的。谷歌搜索也无助于清除 :options 参数。任何人都可以帮忙吗?
谢谢
在某些 Rails 应用程序中,我在 routes.rb 中看到了这一点
root :to => "home#index", :via => [:get]
root :to => "accounts#manage", :via => [:options]
我无法理解这两个根 URL 是如何存在的。谷歌搜索也无助于清除 :options 参数。任何人都可以帮忙吗?
谢谢
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.
对于 Rails 5 及更高版本:在 routes.rb
match "/404", :to => "errors#not_found", via: :all
match "/500", :to => "errors#internal_server_error", via: :all
对于控制器:
class ErrorsController < ApplicationController
layout 'xyz'
def not_found
end
def internal_server_error
end
end
这将在生产中起作用,如果您也希望在开发中也一样,那么在 development.rb 中更改:
config.consider_all_requests_local = false