error-handling - Rails:捕获产生 ActionController::RoutingError 的 URL

标签 error-handling http-status-code-404 ruby-on-rails-3.2

我正在 try catch 产生路由错误的 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。感谢您提供任何帮助。

最佳答案

前一阵子想通了。 request.path

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

关于error-handling - Rails:捕获产生 ActionController::RoutingError 的 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12135210/

相关文章:

c# - 简单的 try/catch 不使用任何异常

java - 何时记录链式异常?

spring - org.springframework.web.servlet.DispatcherServlet noHandlerFound for login.jsp

javascript - Rails 3.2 .js.erb 呈现但从不触发

javascript - 在componentDidCatch之后重定向用户的方法

scala - Scala试试,其他,除 block

ssl - 使用 SSL 的某些页面出现 404 错误

jsp - Spring Boot JSP 404.Whitelabel 错误页面

ruby-on-rails - 如何在重定向时显示 Rails flash 通知?

ruby-on-rails - Heroku ruby​​ buildpack 错误 - 未定义的局部变量或方法 `install_language_pack_gems'