Rails动态错误页面(404,422,500)显示为空白

我正在将动态错误页面实现到应用程序中,并且感觉它正在public文件夹中查找(现在不存在的)模板,而不是遵循我设置的路由。

config/application.rb我添加了一行config.exceptions_app = self.routes来解释这一点。

我已经将以下内容添加到我的路线中:

 get "/not-found", :to => "errors#not_found" get "/unacceptable", :to => "errors#unacceptable" get "/internal-error", :to => "errors#internal_error" 

而错误控制器看起来像这样:

 class ErrorsController  404 end def unacceptable render :status => 422 end def internal_error render :status => 500 end end 

转到/not-found显示模板应该是,但访问任何不存在的URL(即/ i-dont-exist)呈现空页面。

我能看到的唯一原因是exception处理需要路由,例如, get "/404", :to => "errors#not_found" ,但具有讽刺意味的是,它没有找到/的路由404(不,这不仅仅是它的工作:))。

任何建议,非常感谢。 谢谢,史蒂夫。

似乎有些设置是错误的。 在你的路线中尝试这个:

match '/404', to: 'errors#not_found', via: :all (匹配而不是get)

你在application.rb提到你有config.exceptions_app = self.routes ,这很好。 但在测试更改之前,请确保重新启动服务器。

并确保您的错误视图文件与ErrorsController的操作具有相同的名称。

如果您在控制台中遇到任何类型的(哈哈)错误,可以发布吗?

改为:

的routes.rb

 %w(404 422 500).each do |code| get code, :to => "errors#show", :code => code end 

errors_controller.rb

 class ErrorsController < ApplicationController def show render status_code.to_s, :status => status_code end protected def status_code params[:code] || 500 end end 

在你的config / application.rb中确保你有:

 module YourWebsite class Application < Rails::Application config.exceptions_app = self.routes # .. more code .. end end 

那么你将需要视图,显然;)不要忘记删除公共目录中的错误页面。