ruby on rails - Handling an exception in an ajax action -


i have ajax action in rails:

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 

i wonder, sensible enough have action this? there better way of doing out there?

you can use rescue keyword this:

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 

method def can serve begin statement:

def ajax_action1   ... rescue   ... end 

Comments

Popular posts from this blog

c# - Unity IoC Lifetime per HttpRequest for UserStore -

Change the color of an oval at click in Java AWT -

I am trying to solve the error message 'incompatible ranks 0 and 1 in assignment' in a fortran 95 program. -