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

SRa*_*ack 1 ruby configuration routing ruby-on-rails ruby-on-rails-4

我正在将动态错误页面实现到应用程序中,并且感觉它正在查找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"
Run Code Online (Sandbox Code Playgroud)

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

class ErrorsController < ApplicationController
  layout 'errors'
  def not_found
    render :status => 404
  end

  def unacceptable
    render :status => 422
  end

  def internal_error
    render :status => 500
  end
end
Run Code Online (Sandbox Code Playgroud)

/not-found尽管访问任何不存在的URL(即/ i-dont-exist),但是要显示模板应该是一个空页面.

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

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

Puc*_*uce 5

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

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

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

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

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