我的HAML模板的这个帮助器做错了什么?
def display_event(event)
event = MultiJson.decode(event)
markup_class = get_markup_class(event)
haml_tag :li, :class => markup_class do
haml_tag :b, "Foo"
haml_tag :i, "Bar"
end
end这是错误:
haml_tag outputs directly to the Haml template.
Disregard its return value and use the - operator,
or use capture_haml to get the value as a String.模板调用display_event的方式如下:
- @events.each do |event|
= display_event(event)如果我使用的是常规标记,它将扩展为以下内容
%li.fooclass
%b Foo
%i Bar发布于 2012-05-08 04:59:26
线索在错误消息中:
Disregard its return value and use the - operator,
or use capture_haml to get the value as a String.来自haml_tag的文档
haml_tag直接输出到缓冲区;不应使用其返回值。如果需要以字符串形式获取结果,请使用#capture_haml。
要修复它,请将您的Haml更改为:
- @events.each do |event|
- display_event(event)(例如,使用-运算符而不是=),或者将方法更改为使用capture_haml
def display_event()
event = MultiJson.decode(event)
markup_class = get_markup_class(event)
capture_haml do
haml_tag :li, :class => markup_class do
haml_tag :b, "Foo"
haml_tag :i, "Bar"
end
end
end这将使该方法返回一个字符串,然后可以在您的Haml中使用=显示该字符串。
请注意,您只需要进行其中一项更改,如果您同时进行了这两项更改,它们将相互抵消,并且不会显示任何内容。
https://stackoverflow.com/questions/10488855
复制相似问题