Rails 500错误显示PUT请求的空白页

时间:2017-01-06 14:32:27

标签: ruby-on-rails

我在rails应用程序中设置了自定义错误页面。

application.rb中

config.exceptions_app = self.routes

的routes.rb

  get '404', to: 'application#page_not_found'
  get '422', to: 'application#server_error'
  get '500', to: 'application#server_error'

application_controller.rb

  def page_not_found
    respond_to do |format|
      format.html { render template: 'errors/not_found_error', layout: 'layouts/application', status: 404 }
      format.all  { render nothing: true, status: 404 }
    end
  end

  def server_error
    respond_to do |format|
      format.html { render template: 'errors/internal_server_error', layout: 'layouts/error', status: 500 }
      format.all  { render nothing: true, status: 500}
    end
  end 

当我执行引发错误的GET请求时,我的自定义500错误页面显示正常,但是当我提交一个触发NoMethodError的表单时,所以PUT请求,我只是得到一个空白页。

为什么500错误页面正确显示GET请求而不是PUT请求的任何想法?

我尝试将server_error方法更改为

  def server_error
    render template: 'errors/internal_server_error', layout: 'layouts/error', status: 500 
  end 

但这似乎没有帮助。

如果我能提供更多代码,请告诉我,我不确定如何解决此问题。

1 个答案:

答案 0 :(得分:4)

在routes.rb上使用matchvia将所有类型的HTTP请求路由到自定义错误操作

  # error routes
  match '/404' => 'application#page_not_found', :via => :all
  match '/422' => 'application#unprocessable_entity', :via => :all
  match '/500' => 'application#server_error', :via => :all
相关问题