【问题标题】:Instance Variable If Else Condition to Add Tag in HAML实例变量 If Else 条件在 HAML 中添加标签
【发布时间】:2018-07-21 05:11:37
【问题描述】:

我正在尝试在我的 HAML 视图文件中编写条件语句。如果引用对象具有链接,我想做的是在块引用的其余部分顶部添加一个 link_to 标记。如果它为 nil,则格式化 blockqoute,顶部不带 link_to 标记。这是我目前的代码:

.carousel-inner
          - @quotes.each_with_index do |quote, index|
            .item{ class: ("active" if index == 0)}
              - if quote.link.present?
                = link_to quote.link
                  %blockquote
                    .row
                      .col-sm-3.text-center
                        %img.img-circle{:src => quote.avatar, :style => "width: 100px;height:100px;"}
                      .col-sm-9
                        %p= quote.quote
                        %small= quote.author
              - else
                %blockquote
                  .row
                    .col-sm-3.text-center
                      %img.img-circle{:src => quote.avatar, :style => "width: 100px;height:100px;"}
                    .col-sm-9
                      %p= quote.quote
                      %small= quote.author

我得到的当前堆栈错误是:

_quotes.html.haml:26:语法错误,意外关键字_else,预期关键字_end _quotes.html.haml:39:语法错误,意外关键字_确保,预期输入结束

以前有人遇到过这样的事情吗?谢谢!

【问题讨论】:

    标签: ruby-on-rails ruby haml


    【解决方案1】:

    您在= link_to quote.link 行中缺少do,这会导致解析结束块。您只需将其更改为:

    = link_to quote.link do
      %blockquote
        -# ...
    

    【讨论】:

    • 做到了!非常感谢!
    猜你喜欢
    • 2013-12-14
    • 2010-12-17
    • 1970-01-01
    • 1970-01-01
    • 2017-09-01
    • 1970-01-01
    • 2021-12-08
    • 2017-01-21
    • 2019-09-06
    相关资源
    最近更新 更多