如何在 Ruby on Rails 中编写助手来捕获 Haml 块?
我正在编写一个 Rails 辅助方法,它将添加包装器 html 到捕获的内容块并替换 content_for 方法,例如
- content_for :header do
//haml code
.. 将成为
- content :header do
//haml code
为了做到这一点,我使用 Haml 和 Ruby 块。这就是它的样子
def content(name,&block)
content_for name do
capture_haml do
haml_tag "div",{:id=>name.to_s} do
haml_tag "div",{:id=>"#{name.to_s}_group"} do
block
end
end
end
end
end
,但我无法让它发挥作用。没有错误消息。它只是根本不显示块!我不确定我做错了什么。我会很感激第二个意见。
I am writing a Rails helper method that will add wrapper html to captured content blocks and replace content_for method, such as
- content_for :header do
//haml code
..would become
- content :header do
//haml code
In order to do this I am using Haml and Ruby blocks. This is what it looks like
def content(name,&block)
content_for name do
capture_haml do
haml_tag "div",{:id=>name.to_s} do
haml_tag "div",{:id=>"#{name.to_s}_group"} do
block
end
end
end
end
end
But I can't get this to work. There is no error message. It just doesn't show the block at all! I'm not sure what I am doing wrong. I'd appreciate a second opinion.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在做大致正确的事情,但您实际上并没有调用块
#content
已传递。为此,请使用block.call
或 Ruby 的内置yield
语句。You're doing roughly the right thing, but you aren't actually calling the block
#content
is passed. To do so, either useblock.call
, or Ruby's built-inyield
statement.只需将其更改
为
just change this
to