views:

25

answers:

1

There is another respond_to for the usual case, and a special case when a param[:top] is passed in, so there is another respond_to earlier in the code:

      respond_to do |format|
        format.html { render :top_page_analytics }
        format.json { render :json => @analytics }
        format.xml { render :xml => @analytics }
        return
      end

but the above code actually gave a strange error for missing template for json, and further debug leading to:

      respond_to do |format|
        format.html { render :top_page_analytics }
        format.json { render :json => @analytics }
        format.xml { render :xml => @analytics }
      end
      return

which fixes the bug. The return is needed so that there will be no "double render error" because the program will flow to the other respond_to. But I wonder the strange syntax of respond_to, looking somewhat like a case statement, may cause error like that at the top?

+1  A: 

The return can't go there because you're passing a block. The block isn't executed in the immediate context of the controller action. When you return from the block, you're actually returning from the function yielding (respond_to), not the controller action.

Jamie Wong