1

我正在尝试覆盖 products/_cart_form 中的 qty 输入元素,以避免用户修改 qty 应该在 erb 代码或 html 上执行 Deface 覆盖

我使用了以下覆盖 wo 任何成功似乎都是 html 选择器和 erb 代码的混合......混乱和棘手,显然在 Deface github 自述文件上没有这样的例子

 Deface::Override.new(
     :virtual_path => %q{spree/products/_cart_form},
     :name => %q{read_only_qty_cart_form},
     :set_attributes => %q{#cart-form form div.add-to-cart input},
     :attributes => {:readonly => 'readonly'}
 )

spree/products/_cart_form.erb 代码是(与输入元素相关)

    <div class="add-to-cart">
      <% if @product.on_sale? %>      
        <%= number_field_tag (@product.has_variants? ? :quantity : "variants[#{@product.master.id}]"),
          1, :class => 'title', :in => 1..@product.on_hand, :min => 1 , :readonly => true %>
        <%= button_tag :class => 'large primary', :id => 'add-to-cart-button', :type => :submit do %>
          <%= t(:add_to_cart) %>
        <% end %>

它生成以下 html :

 <div data-hook="cart_form" id="cart-form">
        <form method="post" action="/fr/orders/populate" accept-charset="UTF-8">
            <div ...  name="authenticity_token"></div>
              <div...... data-hook="inside_product_cart_form" id="inside-product-cart-form">
                   <div class="columns five  alpha " data-hook="product_price">
                          <div id="product-price">....</div>
                          <div class="add-to-cart">
                                  <input type="number" value="1" name="variants[1001480408]" min="1" max="Infinity" id="variants_1001480408" class="title">
                                   <button type="submit" id="add-to-cart-button"..... >
                           </div>
                     </div>
               </div>
          </form> 
   </div>
4

1 回答 1

1

您正在尝试破坏生成的元素。Deface 在 erb 文件上运行(输入元素尚不存在)。

尝试使用这样的代码破坏:

Deface::Override.new(
     :virtual_path => "spree/products/_cart_form",
     :name => "read_only_qty_cart_form",
     :insert_bottom => "code[erb-loud]:contains('number_field_tag (@product.variants_and_option_values.any?')",
     :text => ", :readonly => 'readonly'"
)
于 2013-06-17T22:27:16.333 回答