【问题标题】:Haml not rendering element from rails modelHaml 不从 rails 模型渲染元素
【发布时间】:2018-12-06 15:01:03
【问题描述】:

我正在尝试从我的“轨道”模型中呈现适当的元素,该模型是一个名为“流派”的列中的字符串

但是,不是渲染流派;页面改为呈现=track.genre 而不是实际的流派。这是语法错误吗?

index.html.haml(用于视图>曲目)

- content_for :header do
  %section.hero.is-warning
    .hero-body
      .container
        %h1.title
          Browse the newest Tracks
.instrument-index-grid.pt4
  - @tracks.each do |track|
    .track.border-light
      .track-thumb
        =link_to image_tag(track.image_url(:thumb)), track
        - if track.genre
          .condition
            %span.tag.is-dark = track.genre // this renders incorrectly
      .pa3
        %h3.fw7.f4.title= link_to track.name, track
        %p.has-text-gray.fg.pt1
          Sold by #{track.user.name}
        %p.f3.fw6.has-text-right.pt2.price= number_to_currency(track.price)
        - if track_artist(track)
          = link_to 'Edit', edit_track_path(track), class: "button is-small"
          = link_to 'Delete', track, method: :delete, data: { confirm: "Are you sure ?" }, class: "button is-small"

【问题讨论】:

    标签: ruby-on-rails ruby haml


    【解决方案1】:

    这只是一个错字。而不是

    %span.tag.is-dark = track.genre
    

    应该是

    %span.tag.is-dark= track.genre
    

    参见reference 的“Ruby 评估”部分:

    = 也可以用在标签的末尾以在该标签中插入 Ruby 代码

    标签后有多余的空格,= 不再位于标签的末尾,因此它与该行的其余部分一起被解释为纯文本。

    【讨论】:

      【解决方案2】:

      将其移至单独的行并删除空格

      .condition
         %span.tag.is-dark 
           =track.genre 
      

      【讨论】:

      • 为什么之前渲染不正确? (顺便说一句,这有效)
      • 很可能是因为你把它放在同一行,并用空格包围了=
      猜你喜欢
      • 1970-01-01
      • 2011-05-05
      • 1970-01-01
      • 2020-06-12
      • 2010-11-18
      • 1970-01-01
      • 2011-12-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多