Ruby on rails 如何捕获rails中的错误?
我在heroku中有一个rails应用程序。这些值被传递到服务器,服务器工作正常,有时会显示错误,如Ruby on rails 如何捕获rails中的错误?,ruby-on-rails,ruby-on-rails-3,heroku,Ruby On Rails,Ruby On Rails 3,Heroku,我在heroku中有一个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
<!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>
很抱歉,出了点问题(500)
正文{背景色:#fff;颜色:#666;文本对齐:居中;字体系列:arial,无衬线;}
div.dialog{
宽度:25em;
填充:0.4em;
保证金:4em自动0自动;
边框:1px实心#ccc;
右边框颜色:#999;
边框底色:#999;
}
h1{字体大小:100%;颜色:#f00;行高:1.5em;}
很抱歉,出了点问题。
我们已收到有关此问题的通知,我们将很快查看。
所以我不想得到这些错误。相反,我希望获得这些错误,并在json中向应用程序显示类似于
意外错误的内容。我该怎么做?请帮助我。在application\u 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
不,我没有要求查看错误。我想捕获错误并编辑错误消息,然后将其作为json发送到应用程序。您提到了“ActiveRecord::RecordNotFound”,所以我必须指定每个错误,然后指定相应的json,或者是否有指定所有错误并为所有人显示相同json的内容。对不起。我试了下一个你给的,效果很好。它捕捉错误。非常感谢你。