ruby-on-rails – haml_tag直接输出到Haml模板

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – haml_tag直接输出到Haml模板前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的HAML模板的帮助器出了什么问题?
  1. def display_event(event)
  2. event = MultiJson.decode(event)
  3. markup_class = get_markup_class(event)
  4. haml_tag :li,:class => markup_class do
  5. haml_tag :b,"Foo"
  6. haml_tag :i,"Bar"
  7. end
  8. end

这是错误

  1. haml_tag outputs directly to the Haml template.
  2. Disregard its return value and use the - operator,or use capture_haml to get the value as a String.

模板调用display_event,如下所示:

  1. - @events.each do |event|
  2. = display_event(event)

如果我使用常规标记,它将扩展为以下内容

  1. %li.fooclass
  2. %b Foo
  3. %i Bar

解决方法

错误消息中的线索:
  1. 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更改为:

  1. - @events.each do |event|
  2. - display_event(event)

(即使用 – 运算符而不是=),或更改方法以使用capture_haml

  1. def display_event()
  2. event = MultiJson.decode(event)
  3. markup_class = get_markup_class(event)
  4. capture_haml do
  5. haml_tag :li,"Bar"
  6. end
  7. end
  8. end

这将使该方法返回一个字符串,然后您可以在Haml中显示=.

请注意,您只需要进行其中一项更改,如果您同时取消这两项更改,则不会显示任何内容.

猜你在找的Ruby相关文章