爲什麼我不能用以下方法拯救任何東西?爲什麼ActiveRecord :: StatementInvalid無法在此Rails方法中獲救?
def get_things
begin
things= @member.things.where("id>?",params[:id])
rescue ActiveRecord::StatementInvalid
render(inline: "RESCUED ActiveRecord::StatementInvalid")
return
rescue
render(inline: "RESCUED something")
return
end
render(inline: "#{things.first.title}")
end
時憑有效身份證叫,它的工作原理:
$ curl -vd "id=3" http://localhost:3000/get_things
,但如果我傳遞一個錯誤的,如:
$ curl -vd "id=3,0" http://localhost:3000/get_things
$ curl -vd "id='3'" http://localhost:3000/get_things
異常沒有被救出:
< HTTP/1.1 500 Internal Server Error
<h1>
ActiveRecord::StatementInvalid
in ApplicationController#get_things
</h1>
<pre>PG::Error: ERROR: invalid input syntax for integer: "'3'"
只有當渲染髮生在裏面開始/救援塊
def get_things
begin
things= @member.things.where("id>?",params[:id])
render(inline: "#{things.first.title}")
rescue ActiveRecord::StatementInvalid
render(inline: "RESCUED ActiveRecord::StatementInvalid")
return
end
end
它按預期工作:
$ curl -vd "id='3'" http://localhost:3000/get_things
< HTTP/1.1 200 OK
RESCUED ActiveRecord::StatementInvalid
謝謝你,這是相當有趣的。 – 2012-07-14 21:40:00