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

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -