简体   繁体   English

Haml和Spree的字符串插值问题

[英]String interpolation trouble with Haml and Spree

I'm trying to convert my Spree store to Haml, and I'm facing some trouble with some parts of the backend. 我正在尝试将Spree商店转换为Haml,而且后端的某些部分也遇到了麻烦。 For instance, I take the following Haml code: 例如,我采用以下Haml代码:

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

And here's the output HTML (simplified to a single iteration): 这是输出HTML(简化为单个迭代):

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

Can someone help me understand why the string interpolation is wrong in both cases? 有人可以帮助我理解为什么两种情况下字符串插值都是错误的吗? I have been looking at this for hours... 我已经看了好几个小时了...

Your HAML syntax looks fine, but it's possible cycle is causing trouble and failing silently. 您的HAML语法看起来不错,但可能是cycle导致麻烦和无声地失败。 Cycle is a RAILs method within ActionView::Helpers::TextHelper , so perhaps your code no longer is including that library now that you've dropped Spree . Cycle是ActionView::Helpers::TextHelper的RAILs方法,因此既然您删除了Spree ,也许您的代码不再包含该库。

If this is the source of the problem, you could either include the ActionView Helpers or rewrite your code without cycle with something like the following: 如果这是问题的根源,则可以包括ActionView帮助器,也可以不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 - I'm assuming the second tr in your sample code is supposed to be a td and that you're just providing a fragment of the table. PS-我假设您的示例代码中的第二个tr应该是td ,而您只是提供表的一部分。

I have just revisited this and came across the fact that Deface (used by Spree) is not compatible with Haml. 我刚刚重新审视了这一点,并发现Deface(由Spree使用)与Haml不兼容。

So either one uses ERB and lives with it, or completely disables Deface and rewrite all 'defaced' views. 因此,要么使用ERB并使用它,要么完全禁用Deface并重写所有“损坏的”视图。

PS: In order to disable Deface, I added config.deface.enabled = false to development.rb , production.rb and test.rb inside config/environments/ . PS:为了禁用Deface,我在config/environments/中将development.rbproduction.rbtest.rb添加了config.deface.enabled = false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM