8

在某些 Rails 应用程序中,我在 routes.rb 中看到了这一点

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

我无法理解这两个根 URL 是如何存在的。谷歌搜索也无助于清除 :options 参数。任何人都可以帮忙吗?

谢谢

4

3 回答 3

11

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.

于 2013-04-25T19:00:21.273 回答
1

您可以使用 :via 选项将请求限制为一种或多种 HTTP 方法

请参阅有关路由的 rails 指南

:post, :get, :put, :delete, :options, :head, 和:any允许作为此选项的值。

正如博客文章中所解释的,OPTIONS 只是另一个支持CORS 请求的 HTTP 动词(一种进行跨域 AJAX 请求的方法)。

更新发现一篇博文解释:options

于 2013-04-25T18:54:16.677 回答
1

对于 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
于 2019-08-02T08:48:24.040 回答