2012-12-06 58 views
0

我在heroku中有一个rails应用程序。该值被传递到服务器,它工作正常,有时会显示如如何捕捉rails中的错误?

<!DOCTYPE html> <html> <head> <title>We're sorry, but something went wrong (500)</title> <style type="text/css"> 
    body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; } 
    div.dialog { 
     width: 25em; 
     padding: 0 4em; 
     margin: 4em auto 0 auto; 
     border: 1px solid #ccc; 
     border-right-color: #999; 
     border-bottom-color: #999; 
    } 
    h1 { font-size: 100%; color: #f00; line-height: 1.5em; } </style> </head> 

<body> <!-- This file lives in public/500.html --> <div class="dialog"> 
    <h1>We're sorry, but something went wrong.</h1> 
    <p>We've been notified about this issue and we'll take a look at it shortly.</p> </div> </body> </html> 

一个错误,所以我不想让这些错误本身。相反,我想获取这些错误,并在应用程序中显示json中的unexpected error。我怎样才能做到这一点?请帮帮我。

回答

2

添加以下代码application_controller.rb

rescue_from "ActiveRecord::RecordNotFound" do |exception| 
    render :json => {:error => 'page not found' } 
end 

同样你可以添加你rescue_from方法

rescue_from "Exception" do |exception| 
render :json => {:error => 'Unexpected error occurred' } 
end 
+0

没有必要所有的异常,我没有要求观看这些错误。我想捕捉错误并编辑错误消息并将其作为json发送给应用程序。 – logesh

+0

我更新了答案 – shivashankar

+0

你已经提到“ActiveRecord :: RecordNotFound”,所以我必须指定每个错误,然后相应的json或者是否有指定所有错误并显示相同的json的东西。 – logesh