在 Rails 2 中,在帮助器中生成一个块时,方法会部分呈现?

发布于 09-26 16:06 字数 418 浏览 3 评论 0原文

Rails 2.2.2

_competition.html.erb

<p>This is packed</p>
<% pack do %>
// some javascript
<% end %>

racings_helper.rb

def pack(&block)
  yield + 'PACKED'
end

但是 'PACKED' 没有附加到我的 javascript 字符串中,就好像 pack do 没有效果。块的上下文和实际视图渲染似乎出现了一些奇怪的情况,因为如果我删除对 helper 中的yield 的调用并返回硬编码的字符串,我就不会从 pack 中得到任何输出。

Rails 2.2.2

_competition.html.erb

<p>This is packed</p>
<% pack do %>
// some javascript
<% end %>

competitions_helper.rb

def pack(&block)
  yield + 'PACKED'
end

However 'PACKED' does not get appended to my javascript string, its as if pack do has no effect. It seems like there is some oddness going off with context of the blocks and the actual view rendering because if I remove the call to yield in the helper and return a hard coded string I get no output from pack.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

自在安然2024-10-03 16:06:23
def pack(&block)
  concat(capture(&block) + 'PACKED')
end
def pack(&block)
  concat(capture(&block) + 'PACKED')
end
¢好甜2024-10-03 16:06:23

我没有测试它,但你可以尝试:

yield << 'PACKED'

这只是组合 2 个字符串的不同方式。

I didn't test it, but you could try:

yield << 'PACKED'

It is just a different way of combining 2 strings.

情域2024-10-03 16:06:23

作为一种解决方法,暂时将 JavaScript 放在另一个部分中,并使用 render_to_string 将其传递给辅助函数,即。无需调用yield即可工作。

<%= pack(render_to_string :partial => '_competition_javascript') %>

As a work around, for the time being, putting the javascript in another partial and passing it to the helper function using render_to_string, ie. without the need to call yield works.

<%= pack(render_to_string :partial => '_competition_javascript') %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文