0

例如

       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

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

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

还是有其他选择?

4

2 回答 2

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

只是把\

于 2011-05-08T07:38:07.520 回答
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。

于 2011-06-19T19:38:31.767 回答