如何直接输出在ERB中占用块的助手的结果?

时间:2011-03-24 16:35:34

标签: ruby-on-rails erb block

我有一个帮助,需要一个块:

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不是解决方案。)

1 个答案:

答案 0 :(得分:0)

我只会指出你here.

,而不是为了别人的工作