Ruby on rails 在ajax操作中处理异常
我在Rails中有一个ajax操作:Ruby on rails 在ajax操作中处理异常,ruby-on-rails,ruby,Ruby On Rails,Ruby,我在Rails中有一个ajax操作: def ajax_action1 begin item = Model1.where(id: params[:id]).first if item #.... else render json: { errors: ['Not found'] }, status: 404 end rescue render json: { errors: ['Unexp
def ajax_action1
begin
item = Model1.where(id: params[:id]).first
if item
#....
else
render json: { errors: ['Not found'] }, status: 404
end
rescue
render json: { errors: ['Unexpected exception, try again later.'] }, status: 503
end
end
我想知道,采取这样的行动是否明智?有没有更好的方法呢?你可以使用
rescue
这样的关键词:
def ajax_action1
item = Model1.where(id: params[:id]).first
if item
#....
else
render json: { errors: ['Not found'] }, status: 404
end
rescue
render json: { errors: ['Unexpected exception, try again later.'] }, status: 503
end
方法def
可以用作begin
语句:
def ajax_action1
...
rescue
...
end