【发布时间】:2012-07-14 13:01:16
【问题描述】:
为什么我无法通过以下方法拯救任何东西?
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
当使用有效 id 调用时,它可以工作:
$ 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
【问题讨论】:
标签: ruby-on-rails exception activerecord rescue