Rails 通过路由出现自定义错误 - 阅读请求的原文 URL

Rails with custom error through routes - read original requested URL

我正在使用自定义错误控制器。我希望在访问 500 页面时有一个 200 响应代码 URL(否则我拦截 500 响应代码的中间件会在我想炫耀我的 500 时向我发送异常电子邮件)

似乎使用 self.routes 作为 exceptions_app 会将 request.path 更改为始终等于错误编号

目标

问题

如何提取真正访问过的URL?

# config/application.rb
config.exceptions_app = self.routes

# routes
match '/500', to: 'errors#server_error', via: :all

# app/controllers/errors_controller.rb
def server_error
    @status = 500
    can_be_visited_with_ok_response_code
    show
  end

def can_be_visited_with_ok_response_code
    # We want to hide hide the faulty status if the user only wanted to see how our beautiful /xxx page looks like
    # BUT `action_dispatch/middleware/debug_exceptions` will change request.path to be equal to the error !!
    # @status = 200 if request.path == "/#{@status}" # BAD request.path will always return response code
  end

  def show
    respond_to do |format|
      format.html { render 'show', status: @status }
      format.all  { head @status }
    end
  end

我喜欢 Ruby 和 did_you_mean...我能够猜出正确的方法名称:应该使用 request.original_fullpath 代替输入原始 URL由用户。

@status = 200 if request.original_fullpath == "/#{@status}"

请注意,request.original_url 还可以为您提供包含主机名的完整路径