HAML 块在产量时返回“0”?

发布于 2024-09-16 12:49:01 字数 294 浏览 9 评论 0原文

我刚刚升级到 Rails3、Ruby 1.9.2 和最新的 HAML gem。 此代码曾经有效:


  = allowed? do
    = link_to('New', new_video_path)

现在 允许? 产生 0

如果我这样做,它就会起作用:


  = allowed?{ link_to('New', new_video_path) }

什么给出了?

I just upgraded to Rails3, Ruby 1.9.2 and the latest HAML gem.
This code used to work:


  = allowed? do
    = link_to('New', new_video_path)

Now allowed? yields 0.

It works if I do:


  = allowed?{ link_to('New', new_video_path) }

What gives?

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

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

发布评论

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

评论(3

酷炫老祖宗 2024-09-23 12:49:01

执行此收益概念以允许正确捕获您想要的任何内容的最简洁方法是:

= allowed? do
  - capture_haml do
    = link_to('New', new_video_path)

但是,在您的情况下,为什么不编写另一个辅助方法呢?

def allowed_link_to(*args, &block)
  opts = args.extract_options!
  if allowed? args.last
    link_to args.push(opts), &block
  else
    ''
  end
end

并像这样使用它:

= allowed_link_to('New', new_video_path)

The cleanest way to do this yield concept to allow whatever content you'd like to be properly captured is:

= allowed? do
  - capture_haml do
    = link_to('New', new_video_path)

In your case, though, why not just write another helper method?

def allowed_link_to(*args, &block)
  opts = args.extract_options!
  if allowed? args.last
    link_to args.push(opts), &block
  else
    ''
  end
end

And use it like this:

= allowed_link_to('New', new_video_path)
南冥有猫 2024-09-23 12:49:01

你为什么首先要回应它的输出?您应该这样做:

- allowed? do
  = link_to('New', new_video_path)

一般来说,您永远不想在块中使用输出运算符(=)。块中输出的内容不会返回到块中;它直接连接到缓冲区中。使用这样的块可能会产生内容无序的错误。

Why are you echoing the output of that in the first place? You should be doing:

- allowed? do
  = link_to('New', new_video_path)

In general, you never want to use the output operator (=) with a block. Stuff outputted in blocks doesn't get returned to the block; it's concat'd directly into the buffer. Using a block like that is likely to produce errors with content out of order.

梦与时光遇 2024-09-23 12:49:01

这花了我一段时间才找到,但这就是你如何做到的:

def wrap_in_div(&block)
  "<div>#{capture_haml(&block)}</div>"
end

问题是 haml 将所有内容输出到它自己的特殊缓冲区,然后再将其发送到机架或任何地方。所以你必须让haml先调用该块并对其进行缓冲。

This took me a while to find, but this is how you do it:

def wrap_in_div(&block)
  "<div>#{capture_haml(&block)}</div>"
end

The problem is that haml outputs everything to its own special buffer before sending it on to rack or wherever. So you have to let haml call the block first and buffer it.

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