【问题标题】:Render and redirect_to in rails在rails中渲染和redirect_to
【发布时间】:2015-01-31 15:23:35
【问题描述】:

我刚刚在一次测验中回答了一个问题,问题如下:

以下哪项是关于render和redirect_to的无效陈述?

  • render 不会发送新请求,但 redirect_to 会发送
  • redirect_to 不会发送新请求,但 render 会发送
  • render 不会导致当前动作停止执行
  • redirect_to 不会导致当前动作停止执行

我选择了选项 1 作为我的答案,但结果是错误的。我在网上查了一下,发现了这个link,上面明确指出redirect_to向浏览器发送了一个新请求:

render 和 redirect_to 之间有一个重要区别:render 会告诉 Rails 它应该使用哪个视图(使用您可能已经发送的相同参数)但 redirect_to 会向浏览器发送一个新请求。

【问题讨论】:

    标签: ruby-on-rails ruby render


    【解决方案1】:

    我猜你错过了问题中的invalid 字,正确答案是选项 4

    redirect_to will not cause the current action to stop executing
    

    选项1render doesn't send a new request but redirect_to does是有效的,因为redirect_to向get类型的action发送get请求,但是render只是渲染view,与其他action无关

    【讨论】:

    • redirect_to 不会导致操作停止执行。这不像在 Ruby 方法中调用 return
    【解决方案2】:

    选项二将是正确答案,因为问题写得很清楚:以下哪一项是无效陈述

    正如您所了解和知道的 redirect_to 发送一个新请求,但在选项中它说不发送所以它变成 无效声明,您的答案应该是

    redirect_to doesn't send a new request but render does
    

    注意:

    Kindly read question properly and understand it first before give the answer..

    【讨论】:

    • 哦,没注意到题主问的是invalid statement o_O ,我现在觉得很傻:/
    • 有人能解释一下为什么第四个选项不正确吗?
    • @RSB:我没有否决你的回答。但是对于您的信息,请read this希望它让您清楚
    • 感谢您的澄清
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-30
    • 2017-02-21
    • 2012-05-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多