开发者

Spree hook usage limited to only 1 special quote?

for example

       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

will give this error

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

the problem here is this line

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

the usage of ' giving the syntax error but if i change it to "Promotion", it will look something like this

       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

notice how the Promotion words become different colour? because its overlap with the previous "

is there any other "special character" that can be used here?

or is there any alternative?


An alternate approach that I use in my projects is to use alternate Ruby syntax for quoting, such as:

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

Of course you can also put your code into a partial and supply the partial to the hook

insert_after :homepage_products, 'shared/hooks/_after_homepage_products'

It's also worth mentioning that in the latest spree versions this hook system is being deprecated in favor of the deface gem.


found the answer

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

just put the \

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜