Hi experts, Just trying to see if anyone on this list can answer my question: http://stackoverflow.com/questions/5545877/rendering-in-applicationcontroller-fails Basically, the problem is as follows: * I have several methods such as render_not_found, render_internal_error etc. in ApplicationController. Each of these goes something like this: rescue_from ActiveRecord::RecordInvalid do |err| render_bad_request err.message end def render_bad_request(message="bad request") respond_to do |wants| wants.json { errmsg = {:error => ''failure'', :message => message } render :status => 400, :json => errmsg and return } end end And in my actual controller def create @resource.transaction do # a lot of stuff... end render_created @resource, :location => build_link(@resource) end The problem is, whenever an ActiveRecord::RecordInvalid (or any other exception I am rescuing from) is raised, the render_* method in ApplicationController runs, but it then continues execution in the action as though no render happened (so it leads to DoubleRender) Any way I could fix this / rewrite the rendering methods? Best, Radhesh -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 6 April 2011 21:51, Radhesh Kamath <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hi experts, > > Just trying to see if anyone on this list can answer my question: > > http://stackoverflow.com/questions/5545877/rendering-in-applicationcontroller-fails > > Basically, the problem is as follows: > > * I have several methods such as render_not_found, render_internal_error > etc. in ApplicationController. Each of these goes something like this: > > rescue_from ActiveRecord::RecordInvalid do |err| > render_bad_request err.messageTry putting a return in here, though I am not sure whether it will work. I have not looked at how rescue_from is implemented. Worth a try though. Colin> end > > def render_bad_request(message="bad request") > respond_to do |wants| > wants.json { > errmsg = {:error => ''failure'', :message => message } > render :status => 400, :json => errmsg and return > } > end > end > > And in my actual controller > > def create > -YFak03Ir5nRqiUuK/vxupZlDKeVEtLwC@public.gmane.org do > # a lot of stuff... > end > > render_created @resource, :location => build_link(@resource) > end > > The problem is, whenever an ActiveRecord::RecordInvalid (or any other > exception I am rescuing from) is raised, the render_* method in > ApplicationController runs, but it then continues execution in the > action as though no render happened (so it leads to DoubleRender)-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Colin Law wrote in post #991306:> On 6 April 2011 21:51, Radhesh Kamath <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >> >> rescue_from ActiveRecord::RecordInvalid do |err| >> render_bad_request err.message > > Try putting a return in here, though I am not sure whether it will > work. I have not looked at how rescue_from is implemented. Worth a > try though.No dice. I have tried putting return, and return etc. It does not work.> > Colin-- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Apr 6, 9:51 pm, Radhesh Kamath <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hi experts, > > Just trying to see if anyone on this list can answer my question: > > http://stackoverflow.com/questions/5545877/rendering-in-applicationco... >The code over there looks very different to the code you''ve posted below. Which is the most current?> > The problem is, whenever an ActiveRecord::RecordInvalid (or any other > exception I am rescuing from) is raised, the render_* method in > ApplicationController runs, but it then continues execution in the > action as though no render happened (so it leads to DoubleRender)When are the exceptions raised? if they are raised after you have called render for the first time, then it''s too late for your rescue_from handler to try and render something else. Fred> > Any way I could fix this / rewrite the rendering methods? > > Best, > Radhesh > > -- > Posted viahttp://www.ruby-forum.com/.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung wrote in post #991310:> On Apr 6, 9:51pm, Radhesh Kamath <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >> Hi experts, >> >> Just trying to see if anyone on this list can answer my question: >> >> http://stackoverflow.com/questions/5545877/rendering-in-applicationco... >> > > The code over there looks very different to the code you''ve posted > below. Which is the most current? >The code I have posted to this list is the more recent.> >> >> The problem is, whenever an ActiveRecord::RecordInvalid (or any other >> exception I am rescuing from) is raised, the render_* method in >> ApplicationController runs, but it then continues execution in the >> action as though no render happened (so it leads to DoubleRender) > > When are the exceptions raised? if they are raised after you have > called render for the first time, then it''s too late for your > rescue_from handler to try and render something else.No render is called before the exceptions are raised. Is there a way I can check whether rendering has occurred in my render_* methods? I am sure that render has not occurred before the exception is raised, but I would be interested to know nevertheless.> > Fred-- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.