ruby-on-railsrubyexceptionfayeprivate-pub

Rails: How to avoid AbstractController::DoubleRenderError when using live update / Faye?


I'm using Private Pub which is a gem built on top of Faye for live updating through my controller. I'm having trouble to be able to make a fallback for the Live Update Render, in case Faye server failed to serve.

Is there anyway to go around this situation and render second time if my first render method failed?

Here is my create action:

  def create
    #assigning objects and such happening here

    begin
        LiveAjaxRender(@comment)
    rescue => exception
        ExceptionNotifier.notify_exception(exception)

        normalAjaxRender(@comment)
    end # end rescue
  end

Normally when the error occurs, it should go to the 2nd block and ignore the first one. While here it doesn't because it says that:

An AbstractController::DoubleRenderError occurred in user_comments#create:

Render and/or redirect were called multiple times in this action. Please note that you may only call render OR redirect, and at most once per action. Also note that neither redirect nor render terminate execution of the action, so if you want to exit an action after redirecting, you need to do something like "redirect_to(...) and return". app/controllers/my_controller.rb130:in `block (2 levels) in normalAjaxRender'


Solution

  • In order to avoid the redundancy of the Rendering this is what I did:

    Set normalAjaxRender() method as default and in the JS file I added rescue and liveupdate