2011-05-07 26 views
0

例如Spree挂钩使用限于只有1个特殊报价?

 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') 

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

 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 

注意促销字是如何变成不同颜色的? ,因为它与前面的重叠"

是否有任何其他“特殊字符”,可以在这里使用?

还是有其他选择吗?

回答

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宝石。

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) %> 

只是把\