破坏 _products 数据挂钩

Deface _products data-hook

我想从侧边栏添加分类法到主页,而不是产品列表,但我的污损不起作用。

Deface::Override.new(
:virtual_path => 'spree/shared/_products',
:name => 'change view',
:replace => "[data-hook='homepage_products']",
:text => "
            <% max_level = Spree::Config[:max_level_in_taxons_menu] || 1 %>
            <nav id='taxonomies2' class='sidebar-item' data-hook>
                <% @taxonomies.each do |taxonomy| %>
                    <% cache [I18n.locale, taxonomy, max_level] do %>
                        <h4 class='taxonomy-root'><%= Spree.t(:shop_by_taxonomy, :taxonomy => taxonomy.name) %></h4>
                        <%= taxons_tree(taxonomy.root, @taxon, max_level) %>
                    <% end %>
                <% end %>
            </nav> ")

我真的卡在这上面了。

您应该将虚拟路径替换为首页索引视图一:spree/home/index,您要替换的元素所在的位置。

此外,我不确定名称是否应包含 space,请尝试将其更改为 "change_view"。始终确保名称是唯一的,并在添加新的篡改覆盖文件时重新启动服务器。

案例-重启服务器: 无需每次更改覆盖时都重启服务器。

是的,你应该设置 config.deface.enabled = false;通常我们在 production.rb 中设置它,在这种情况下,您应该预编译覆盖。 See this for more info


调试技巧: 如果您不确定 partial/template 的路径是否正确,那么您可以使用此技术进行验证

$ rake deface:get_result['spree/shared/_link_to_account']
=> ActionView::MissingTemplate: Missing template

$ rake deface:get_result['spree/shared/_login_bar']
# ---------------- Before ----------------
# <% if spree_current_user %>
#   <li><%= link_to Spree.t(:my_account), spree.account_path %></li>
#   <li><%= link_to Spree.t(:logout), spree.logout_path %></li>
# <% else %>

您甚至可以验证选择器,例如

$ rake deface:test_selector['spree/shared/_login_bar , li']
# Querying 'spree/shared/_login_bar' for 'li'
# ---------------- Match 1 ----------------
# <li>
# <%= link_to 'Dashboard', '/admin' if spree_current_user.is_admin? %> </li>
# ---------------- Match 2 ----------------

此外,如果选择器验证后错误仍然存​​在,那么您必须确保要替换的 text 不包含任何 syntax/runtime错误。