在 Rails 2 中,在帮助器中生成一个块时,方法会部分呈现?
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我没有测试它,但你可以尝试:
这只是组合 2 个字符串的不同方式。
I didn't test it, but you could try:
It is just a different way of combining 2 strings.
作为一种解决方法,暂时将 JavaScript 放在另一个部分中,并使用
render_to_string
将其传递给辅助函数,即。无需调用yield
即可工作。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 callyield
works.