2

I am using a custom error controller. I would like to have a 200 response code when visiting the 500 page URL (otherwise my middleware that intercepts 500 response codes sends me an exception email whenever I want to show off my 500)

It would seem using the self.routes as exceptions_app will change the request.path to always be equal to the error number

Target

  • visit www.example.com/crashy_url # => Should show custom 500 error template with 500 response code
  • visit www.example.com/500 # => Should show custom 500 error template with 200 response code

Problem

  • visit www.example.com/crashy_url # => request.path is equal to `/500' so my controller sends a 200 response code

How can I extract the really visited URL with this ?

# 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
4

1 回答 1

2

我喜欢 Ruby 并且did_you_mean......我能够猜出正确的方法名称:request.original_fullpath应该用于获取用户输入的原始 URL。

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

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

于 2017-05-03T23:40:30.330 回答