如何将%p与Haml中的其他行一起使用

问题描述 投票:-4回答:1

我收到以下错误:

Illegal nesting: content can't be both given on the same line as %p and nested within it.

这是我的代码:

- @docs.each do |doc|
%h2= link_to doc.title, doc
%p= time_ago_in_words(doc.created_at)
%p= truncate(doc.content, length:50)

    = link_to "Create Doc", new_doc_path
ruby-on-rails ruby haml
1个回答
2
投票

[当您在Haml中的标签上提供内容时,您说的是“此标签的内容将仅是该行中的内容,并立即关闭标签”。

所以

%p hello

成为

<p>hello</p>

如果标签的行数超过一行,请使用换行符和缩进来指示该块的内容:

%p
  Hello
  World

将导致:

<p>Hello\nWorld</p>

您正在尝试在此处同时使用它们。您正在为简写标签提供内容,但也要缩进,好像您打算继续向p标签添加内容一样。

要解决此问题,您需要使用多行语法,或者从link_to中删除缩进:

- @docs.each do |doc|
  %h2= link_to doc.title, doc
  %p= time_ago_in_words(doc.created_at)
  %p
    = truncate(doc.content, length:50)
    = link_to "Create Doc", new_doc_path
© www.soinside.com 2019 - 2024. All rights reserved.