Haml 和 Spree 的字符串插值问题

String interpolation trouble with Haml and Spree

我正在尝试将我的 Spree 商店转换为 Haml,但我在后端的某些部分遇到了一些麻烦。 例如,我采用以下 Haml 代码:

- @orders.each do |order|   
  %tr{:class => "state-#{order.state.downcase} #{cycle('odd', 'even')}"}   
  %tr{:class => "state-#{order.state.downcase}"}

这是输出 HTML(简化为单次迭代):

<tr class="state-#order.state.downcase"></tr>
<tr></tr>

谁能帮我理解为什么在这两种情况下字符串插值都是错误的?我已经看了好几个小时了...

您的 HAML 语法看起来不错,但 cycle 可能会造成问题并悄无声息地失败。 Cycle 是 ActionView::Helpers::TextHelper 中的一个 RAILs 方法,所以您的代码可能不再包含该库,因为您已经删除了 Spree

如果这是问题的根源,您可以包括 ActionView Helpers 或在没有 cycle 的情况下重写您的代码,如下所示:

%table
  %tbody
    - @orders.each_with_index do |order,index|   
      %tr{:class => "state-#{order.state.downcase} #{%w(even odd)[index%2]}"}   
        %td{:class => "state-#{order.state.downcase}"}
          #{order.state}

PS - 我假设您的示例代码中的第二个 tr 应该是 td 而您只是提供了 table.

我刚刚重新审视了这个,发现 Deface(Spree 使用的)与 Haml 不兼容。

所以要么使用 ERB 并接受它,要么完全禁用污损并重写所有 'defaced' 视图。

P.S.: 为了禁用Deface,我在config/environments/中添加了config.deface.enabled = falsedevelopment.rbproduction.rbtest.rb