Wednesday, April 6, 2011

[Rails] 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

--
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@googlegroups.com.
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.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate