2017-01-06 49 views
0

我在我的Rails应用程序中设置了自定义错误页面。Rails 500错误显示PUT请求的空白页面

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 

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

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

我试图改变server_error方法

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

,但这似乎并没有帮助。

让我知道如果我可以提供更多的代码,我不知道如何解决这个问题。

+1

尝试改变'GET'到'match'。 – sahil

+1

在'get'路由中意味着GET请求,如'put','post','patch'和'delete'。正如@ sa77回答,尝试使用匹配'via::all'来代替。 –

+0

请接受答案,如果它帮助和关闭的问题,以便有类似问题的人可以很容易地找到它 – sa77

回答

4

使用matchvia您的routes.rb路由所有类型的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 
+0

感谢这工作。我认为'匹配'虽然被弃用。 https://github.com/rails/rails/issues/5964 – user4584963

+0

一些使用匹配的情况像使用'match:via =>:get'那样使用特定的http动词折旧,使用通用路由'match'/:controller /:action''等。您需要浏览rails gem的代码库来调查furthur'bundle open rails'。但是,您不会因使用'match:via =>:all'而得到DEPRECIATION警告。 – sa77