为了清理和分解难看的视图,我想做以下几件事:
1)在视图中:
= document_left_container do
= document_information2)在我的助手中:
def document_left_container(&block)
render partial: "/document_left_container", locals: { custom_block: block }
end
def document_information
render partial: "document_information"
end3)部分:
对于document_left_container:
.foo
= custom_block.call对于document_information:
.bar4)预期结果:
<div class='foo'>
<div class='bar'>
</div>
</div>5)实际结果:
<div class='foo'>
</div>
<div class='bar'>
</div>有人知道我该怎么做才能让我的东西正常工作吗?
提前谢谢你,
本
发布于 2012-07-13 10:33:44
下面是我保持干燥的方法:
=content_for(:document_information) do
.bar
.foo
=yield(:document_information)这将产生
<div class='foo'>
<div class='bar'>
</div>
</div>发布于 2012-07-13 15:39:04
我目前的解决方案是:
帮助者:
def document_left_container(&block)
content_for :document_left_container do
block.call
end
render partial: "/document_left_container"
end部分:
.foo
= yield :document_left_containerRest保持不变,我真的想保持相同的结构。
我仍然很想知道为什么我的原始代码失败了。
发布于 2012-07-14 02:40:27
我认为你的原始代码失败了,因为你本质上是先调用render,然后再调用render。所以你把一些东西推到堆栈上,然后再把其他东西推到堆栈上。如果你这样做了,我想它会起作用的。(尽管我还没有测试过它。)
def document_left_container(&block)
capture_haml do
render partial: "/document_left_container", locals: { custom_block: block }
end
end
def document_information
render partial: "document_information"
endhttps://stackoverflow.com/questions/11461395
复制相似问题