HAML if elsif 中的语法错误

Syntax error in HAML if elsif

当我尝试渲染 HAML 部分时,我不断收到此错误:

app/views/shared/_notification.html.haml:9: syntax error, unexpected keyword_elsif, expecting keyword_end
app/views/shared/_notification.html.haml:17: syntax error, unexpected keyword_elsif, expecting keyword_end
app/views/shared/_notification.html.haml:25: syntax error, unexpected keyword_elsif, expecting end-of-input
app/controllers/application_controller.rb:20:in `notify_in_realtime'
app/controllers/account/users_controller.rb:38:in `show'

当我尝试呈现此视图时:

- if object.class.name == 'Lab'
  = link_to account_lab_path(object), "data-id" => notification.id.to_s, :class => "notification item"
    = image_tag object.picture.url(:medium), :class => "img-circle pull-left avatar"
    .media-body
      %strong.name
        = object.title
      = content

- elsif object.class.name == 'Post'
  = link_to account_lab_path(object.lab, :post => object.id), "data-id" => notification.id.to_s, :class => "notification item"
    = image_tag object.lab.picture.url(:medium), :class => "img-circle pull-left avatar"
    .media-body
      %strong.name
        = object.lab.title
      = content

- elsif object.class.name == 'Comment'
  = link_to account_lab_path(object.post.lab, :comment => object.id), "data-id" => notification.id.to_s, :class => "notification item"
    = image_tag object.post.lab.picture.url(:medium), :class => "img-circle pull-left avatar"
    .media-body
      %strong.name
        = object.post.lab.title
      = content

- elsif object.class.name == 'User'
  = link_to account_user_path(object), "data-id" => notification.id.to_s, :class => "notification item"
    = image_tag avatar_url(user, :thumb), :class => "img-circle pull-left avatar"
    .media-body
      %strong.name
        = object.full_name
      = content

- elsif object.class.name == Mailboxer::Conversation
  = link_to account_conversation_path(object), "data-id" => notification.id.to_s, :class => "notification item"
    = image_tag avatar_url(user, :thumb), :class => "img-circle pull-left avatar"
    .media-body
      %strong.name
        = user.full_name
      = content
.clearfix

在我的应用程序控制器中,我试图将它存储在一个变量中以便稍后发送:

html = render_to_string :partial => 'shared/notification', :locals => {content: content, user: user, object: object, notification: notification}

感谢任何帮助,谢谢。 :)

- if object.class.name == 'Lab'
  = link_to account_lab_path(object), "data-id" => notification.id.to_s, :class => "notification item"
  = image_tag object.picture.url(:medium), :class => "img-circle pull-left avatar"
  .media-body
    %strong.name
      = object.title
    = content
- elsif object.class.name == 'Comment'
  = link_to account_lab_path(object.post.lab, :comment => object.id), "data-id" => notification.id.to_s, :class => "notification item"
  = image_tag object.post.lab.picture.url(:medium), :class => "img-circle pull-left avatar"
  .media-body
    %strong.name
      = object.post.lab.title
    = content
- elsif object.class.name == 'User'
  = link_to account_user_path(object), "data-id" => notification.id.to_s, :class => "notification item"
  = image_tag avatar_url(user, :thumb), :class => "img-circle pull-left avatar"
  .media-body
    %strong.name
      = object.full_name
    = content
- elsif object.class.name == Mailboxer::Conversation
  = link_to account_conversation_path(object), "data-id" => notification.id.to_s, :class => "notification item"
  = image_tag avatar_url(user, :thumb), :class => "img-circle pull-left avatar"
  .media-body
    %strong.name
      = user.full_name
    = content

尝试将您的 haml 代码转换为 erb/html。您可以注意到结构如下:

<% if something %>
<% end %>
<% elsif somthing %>
<% end %>
<% elsif something %>

应该是:

`<% if something %>
<% elsif somthing %>
<% elsif something %>
<% end %>`

haml 语法不正确

如果你想要 link 中的图像和 .media-body,你应该在 link_to 之后有 do,否则它们应该与 link_to 具有相同的级别

- if object.class.name == 'Lab'
  = link_to account_lab_path(object), "data-id" => notification.id.to_s, :class => "notification item" do
    = image_tag object.picture.url(:medium), :class => "img-circle pull-left avatar"
    .media-body
      %strong.name
        = object.title
      = content
...

您的代码还有另一个潜在问题:

- elsif object.class.name == Mailboxer::Conversation

应该是

- elsif object.class.name == 'Mailboxer::Conversation'

- elsif object.class == Mailboxer::Conversation

HTH