[英]Rails - Append content to helper method's block without <%= %>
I'm trying to create a helper method that appends stuff and shows the block inside of it, like this: 我正在尝试创建一个辅助方法,该方法可以附加内容并显示其中的块,如下所示:
def test &block
%{<div>
<div> Example content </div>
<p> #{capture(&block)} </div>
</div>}.html_safe()
end
when I call it like this 当我这样称呼它时
<%= test do %>
**stuff
<% end %>
it works properly, but when I also need to call it from the controller using ERB.new(), and when I do so, I get an error: 它可以正常工作,但是当我还需要使用ERB.new()从控制器调用它时,出现错误:
the call: 电话:
@result = ERB.new(
%{<%= view_context.test do %>
<div>content to show inside</div>
<% end %>}
).result(binding)
the error: 错误:
SyntaxError ((erb):2: syntax error, unexpected ')'
ncat(( view_context.test do ).to_s); _erbout.concat "\n
so I need to call without the " = ", <% test do ... %>, but when I call it like this, the appended content isn't shown 因此,我需要不带“ = ”,<%test do ...%>进行调用,但是当我这样调用时,未显示附加内容
what am I missing? 我想念什么?
Maybe using content_tag
in your helper would be a better way to do this. 也许在助手中使用content_tag
可能是一种更好的方法。
For your error, it may be because you are doing the whole ERB.new(...)
in your view, should be in an helper. 对于您的错误,可能是因为您在视图中正在执行整个ERB.new(...)
,所以应该在帮助程序中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.