Rendering in ApplicationController fails

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

On 6 April 2011 21:51, Radhesh K. [email protected] 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.

Colin

Colin L. wrote in post #991306:

On 6 April 2011 21:51, Radhesh K. [email protected] 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

On Apr 6, 9:51pm, Radhesh K. [email protected] 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

Frederick C. wrote in post #991310:

On Apr 6, 9:51pm, Radhesh K. [email protected] 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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs