简体   繁体   中英

Ruby on Rails' respond_to causing strange error

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM