gpt4 book ai didi

ruby-on-rails - 渲染 :partial returning an array when defined in ApplicationController but string when in ApplicationHelper

转载 作者:行者123 更新时间:2023-12-04 06:13:13 24 4
gpt4 key购买 nike

这种方法:

  def admin_buttons
render :partial => 'portable/admin/admin_buttons'
end

当在 ApplicationControler 中定义并使用 helper_method 创建一个助手时返回这个(不是 html_safe):

[“我的部分输出”]

但是在 ApplicationHelper 中定义时,它通常会返回预期的字符串。

我不明白。据我所知,这是在 rails 3.1 中的新行为

最佳答案

简单来说,就是不要调用 Controller 的render在 helper 。它只是不那样工作
render在 Controller 和 render in a helper 不能互换使用。这在 Rails 3.1 中并不新鲜。

当您调用 render在 Controller 中,它最终会调用 render在 View 上,其结果存储为其 response_body .响应正文最终以 Rack 期望的方式返回,作为字符串数组(您看到的输出)。

这些链接可能会更清楚地说明这是如何工作的:
- The controller's definition of render (metal)
- It's superclass method, where response_body is set (abstract_controller)

关于ruby-on-rails - 渲染 :partial returning an array when defined in ApplicationController but string when in ApplicationHelper,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7547492/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com