简体   繁体   English

Ruby on Rails的response_to导致奇怪的错误

[英]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 ,在传入param[:top]是一种特殊情况,因此在代码的前面还有另一个respond_to

      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: 但是上面的代码实际上由于缺少json模板而给出了一个奇怪的错误,并进一步调试导致:

      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 . 需要返回,以便不会出现“双重渲染错误”,因为该程序将流向另一个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? 但是我不知道respond_to的奇怪语法(看起来像一个case语句)是否会在顶部引起类似的错误?

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. 从块返回时,实际上是从函数yield(respond_to)返回,而不是从控制器操作返回。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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