【问题标题】:rails 410 on no route matches没有路线匹配的rails 410
【发布时间】:2012-03-10 22:48:21
【问题描述】:

当有人以错误的路线访问我的网站时,rails 会以 404 代码和错误响应

ActionController::RoutingError No route matches

我可以用410 的rails 错误代码而不是404 来响应吗?

【问题讨论】:

  • 404 表示 URL 不正确。 410 将用于删除的内容。

标签: ruby-on-rails ruby


【解决方案1】:

我不知道你为什么想要 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

如果还想指向公用文件夹中的404.html:

render :file => "#{Rails.root}/public/404.html"

【讨论】:

    猜你喜欢
    • 2012-10-11
    • 2017-07-13
    • 2013-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-24
    相关资源
    最近更新 更多