Error handling Rails:捕获生成ActionController::RoutingError的URL

Error handling Rails:捕获生成ActionController::RoutingError的URL,error-handling,http-status-code-404,ruby-on-rails-3.2,Error Handling,Http Status Code 404,Ruby On Rails 3.2,我正在尝试捕获产生路由错误的URL。我的最终目标是更改URL上的域,以检查它是否存在于我们的旧站点(已更改的域),如果存在,则重定向到旧站点。这是我到目前为止所做的 unless Rails.application.config.consider_all_requests_local rescue_from Exception, with: :render_500 rescue_from ActionController::RoutingError, with: :check_old_s

我正在尝试捕获产生路由错误的URL。我的最终目标是更改URL上的域,以检查它是否存在于我们的旧站点(已更改的域),如果存在,则重定向到旧站点。这是我到目前为止所做的

unless Rails.application.config.consider_all_requests_local
  rescue_from Exception, with: :render_500
  rescue_from ActionController::RoutingError, with: :check_old_site
  rescue_from ActionController::UnknownController, with: :render_404
  rescue_from ActionController::UnknownAction, with: :render_404
  rescue_from ActiveRecord::RecordNotFound, with: :render_404
end

private

def check_old_site(exception)
  #Need to edit the URL but I need access to before I can write the code to modify
  captured_and_modified_url = ???
  case Net::HTTP.get_response(URI.parse(captured_and_modified_url)) 
    when Net::HTTPSuccess then redirect_to captured_and_modified_url
    else render_404(exception)
  end
end

def render_404(exception)
  @not_found_path = exception.message
  respond_to do |format|
    format.html { render template: 'errors/error_404', layout: 'layouts/application', status: 404 }
    format.all { render nothing: true, status: 404 }
  end
end

所以对于这个问题,我只想得到404错误的URL。谢谢你的帮助

请在不久前解决这个问题<代码>请求路径

def check_old_site(exception)
  exception_url = "http://old.richarddawkins.net#{request.path}"
  case Net::HTTP.get_response(URI.parse(exception_url))
    when Net::HTTPSuccess then redirect_to exception_url
    else render_404(exception)
  end 
end