【发布时间】:2019-03-22 09:41:19
【问题描述】:
我有一个相对较大的网站,每天都会收到与各种 IP 地址无关的请求。一个常见的是example.com/home.zip。
这会命中我的 Home 控制器,索引操作 {"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。我在正确的轨道上吗?
【问题讨论】:
-
为什么
format.any为什么不format.zip? -
@Pavan 因为只是一个例子,可能不仅仅是
.zip,应用程序可能正在被某人扫描 -
@Pavan 正如@Vasfed 所说,
format.zip会解决我提到的具体错误,但是需要各种格式(json、jpeg 等)。 -
好的,我明白了。在这种情况下,我会使用路由约束。检查guides.rubyonrails.org/routing.html#request-based-constraints
标签: ruby-on-rails ruby-on-rails-3 error-handling request response