当有人通过错误的路线访问我的网站时,rails 会使用 404
代码和错误进行响应
ActionController::RoutingError 没有路由匹配
我可以在 Rails 错误上使用 410
代码而不是 404
进行响应吗?
最佳答案
我不知道你为什么想成为 410 error (Gone) 是指曾经可用但现在不再可用的资源。
如果您按照 https://stackoverflow.com/a/5360684/219743 的答案进行操作然后它允许你这样做。
只需将 render_404 方法更改为
def render_404
if /(jpe?g|png|gif)/i === request.path
render :text => "404 Not Found", :status => 410 # Change the status here
else
render :template => "shared/404", :layout => 'application', :status => 410 #and here
end
end
如果您仍想指向公共(public)文件夹中的 404.html:
render :file => "#{Rails.root}/public/404.html"
关于ruby-on-rails - Rails 410 没有路线匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9363783/