【发布时间】:2011-03-24 16:35:34
【问题描述】:
我有一个带块的助手:
def container(&block)
render(:partial => 'layouts/container', :locals => {:content => capture(&block)})
end
当我尝试在 ERB 的 <%= ... %> 标记内使用它时:
<%= container do %>
Test
<% end %>
我收到compile error:
compile error
test2.html.erb:1: syntax error, unexpected ')'
old_output_buffer = output_buffer;;@output_buffer = ''; __in_erb_template=true ; @output_buffer.concat(( container do ).to_s); @output_buffer.concat "\r\n"
^
test2.html.erb:4: syntax error, unexpected kENSURE, expecting ')'
test2.html.erb:6: syntax error, unexpected kEND, expecting ')'
但是,如果我捕获助手的输出然后输出它:
<% output = container do %>
Test
<% end %>
<%= output %>
它工作正常,但很丑。
有什么方法可以做我想做的事吗? (注意:通常我使用 HAML,但我试图让我的助手为团队中目前仍在 ERB 工作的其他人工作;切换到 HAML 不是解决方案)。
【问题讨论】:
标签: ruby-on-rails erb block