狂欢钩的使用仅限于1特价?

时间:2011-05-07 17:57:42

标签: ruby-on-rails spree

例如

       insert_after :homepage_products do
       "
            <h1>Promotional Item</h1>
            <% products=Product.find_by_sql('select * from products where id in (select product_id from products_taxons where taxon_id in (select id from taxons where name='Promotion'))') %>
            <%= render 'shared/products', :products => products, :taxon => @taxon %>
    "
       end

会出现此错误

compile error
inline template:3: syntax error, unexpected tCONSTANT, expecting ')'
...m taxons where name='Promotion'))') 
                              ^
inline template:3: syntax error, unexpected ')', expecting kEND
...ons where name='Promotion'))') 
                              ^

这里的问题是这一行

select * from products where id in (select product_id from products_taxons where taxon_id in (select id from taxons where name='Promotion')

使用'给出语法错误 但如果我将其更改为"Promotion",它将看起来像这样

       insert_after :homepage_products do
       "
            <h1>Promotional Item</h1>
            <% products=Product.find_by_sql('select * from products where id in (select product_id from products_taxons where taxon_id in (select id from taxons where name="Promotion"))') %>
            <%= render 'shared/products', :products => products, :taxon => @taxon %>
    "
       end

注意促销词是如何变成不同颜色的? 因为它与之前的"

重叠

还有其他“特殊字符”可以在这里使用吗?

还是有其他选择吗?

2 个答案:

答案 0 :(得分:0)

找到了答案

    <% sql_string = "select * from products where id in (select product_id from products_taxons where taxon_id in (select id from taxons where name=\"Promotion\"))" %>
    <% products=Product.find_by_sql(sql_string) %>

只需输入\

答案 1 :(得分:0)

我在项目中使用的另一种方法是使用备用Ruby语法进行引用,例如:

insert_after :homepage_products do
  %(
    <h1>Promotional Item</h1>
    <% products=Product.find_by_sql('select * from products where id in (select product_id from products_taxons where taxon_id in (select id from taxons where name="Promotion"))') %>
    <%= render 'shared/products', :products => products, :taxon => @taxon %>
  )
end

当然,您也可以将代码放入局部并将部分提供给钩子

insert_after :homepage_products, 'shared/hooks/_after_homepage_products'

值得一提的是,在最新的狂欢版本中,这个钩子系统被弃用,而不是deface gem。