Spree hook 的使用仅限于 1 个特殊报价?

发布于 2024-11-05 16:17:19 字数 1581 浏览 5 评论 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

注意“Promotion”字样如何变成不同的颜色? 因为它与前面的 " 重叠,

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

或者有其他选择吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

掩饰不了的爱 2024-11-12 16:17:19

我在项目中使用的另一种方法是使用备用 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'

还值得一提的是,在最新的 spree 版本中,这hook 系统已被弃用,取而代之的是 deface gem。

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.

二智少女 2024-11-12 16:17:19

找到答案

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

只需输入\

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 \

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文