use*_*717 2 routes ruby-on-rails ruby-on-rails-3
我知道如何处理RecordNotFound的错误
但是我如何处理路由错误(没有路由匹配[GET])?
PS在这个主题我找不到答案 - Rails - 如何处理不存在的路由("没有路由匹配[GET]")?
处理路由(以及所有其他路由)的另一种方法是在ApplicationController中添加以下内容:
unless Rails.application.config.consider_all_requests_local
rescue_from Exception do |e|
render_500 e
end
rescue_from ActionController::RoutingError, with: :render_404
rescue_from ActionController::UnknownController, with: :render_404
rescue_from ActionController::UnknownAction, with: :render_404
rescue_from ActiveRecord::RecordNotFound, with: :render_404
end
Run Code Online (Sandbox Code Playgroud)
然后确保定义render_500
,render_404
以便它们实际呈现某些东西.在我的应用程序中,我做到了:
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
Run Code Online (Sandbox Code Playgroud)
然后在中创建了一个模板errors/error_404
.这样它就可以处理所有路由错误并仍然使用我的应用程序布局.
您可以使用@not_found_path向用户显示错误.
归档时间: |
|
查看次数: |
1008 次 |
最近记录: |