Ruby on Rails的response_to导致奇怪的错误 [英] Ruby on Rails' respond_to causing strange error

查看:165
本文介绍了Ruby on Rails的response_to导致奇怪的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在通常情况下还有另一个respond_to,在传入param[:top]时是一种特殊情况,因此在代码的前面还有另一个respond_to:

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

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

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

可修复该错误.需要返回,以便不会出现双重渲染错误",因为程序将流向另一个respond_to.但是我想知道respond_to的奇怪语法,看起来像一个case语句,可能会在顶部引起类似的错误吗?

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?

推荐答案

返回值不能到达那里,因为您正在传递一个块.该块不会在控制器动作的直接上下文中执行.当您从块中返回时,实际上是从函数yield(respond_to)中返回,而不是从控制器操作中返回.

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.

这篇关于Ruby on Rails的response_to导致奇怪的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆