我的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
解决方法
错误消息中的线索:
Disregard its return value and use the - operator,or use capture_haml to get the value as a String.
来自haml_tag
的文档:
haml_tag
outputs directly to the buffer; its return value should not be used. If you need to get the results as a string,use#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,"Bar" end end end