rails4 // ActionController::UnknownFormat

rails4 // ActionController::UnknownFormat

某些机器人程序或其他外部请求在重定向时会导致 ActionController::UnknownFormat 错误

是否可以在 application_controller 的某处添加一个包罗万象的方法来处理这些错误?

在请求中添加默认方法的配置规范(如果指定了 none)?

下面是跟踪示例:

An ActionController::UnknownFormat occurred in main#index:

  ActionController::UnknownFormat
  app/controllers/main_controller.rb:17:in `index'


-------------------------------
Request:
-------------------------------

  * URL        : http://ownedurl.com/
  * HTTP Method: GET
  * IP address : 82.xxx.xxx.xxx
  * Parameters : {"controller"=>"main", "action"=>"index"}
  * Timestamp  : 2015-06-07 20:54:32 +0200
  * Server : servername
  * Rails root : /var/www/ownedurl.com/releases/208
  * Process: 29726

get 请求显然可以;但它让它发生了。

有这方面的经验表示赞赏 非常感谢

您可以将 :format 提供给您的路线本身,以告诉它哪些格式有效,哪些格式无效。

阅读一些 discussions on this and this answer

这将处理没有格式的请求:

rescue_from ActionController::UnknownFormat, with: :raise_not_found
def raise_not_found
    render(text: 'Not Found', status: 404)
end