0

ルーティングエラーが発生した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を取得したいと思います。前もって助けてくれてありがとう。

4

1 に答える 1

0

少し前にこれを理解してください。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
于 2012-10-04T05:40:45.637 に答える