* 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?
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.
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.