我有一个相对较大的网站,每天整天都有与各种IP地址无关的请求受到打击。常见的是example.com/home.zip
。
这将命中我的Home Controller index action {"controller"=>"home", "action"=>"index", "format"=>"zip"}
。当前,它导致500错误:Missing template home/index, application/index with {:locale=>[:en], :formats=>[:zip], :handlers=>[:erb, :builder, :arb]}
我认为处理它的优美方法是404。我所能找到的最接近的解决方案是:
respond_to do |format|
format.html
format.any { redirect_to :foo }
end
除了
redirect_to :foo
,我会做render file: "#{Rails.root}/public/404.html", status: 404
。我在正确的轨道上吗?
最佳答案
您没有理由自己渲染404,您可以
raise ActionController::RoutingError.new('Not Found')
对于这种情况,您还可以使路由约束更加困难,以使此类请求不会到达 Controller 。
关于ruby-on-rails - 在Rails中,如何响应无关的非HTTP请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52859465/