0

私は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; 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>

だから私はこれらのエラーをそのまま取得したくありません。unexpected error代わりに、これらのエラーを取得して、アプリにjsonのようなものを表示したいと思います。これどうやってするの?私を助けてください。

4

1 に答える 1

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
于 2012-12-06T13:50:20.960 に答える