为什么我不能用以下方法拯救任何东西?为什么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