我试图覆盖products / _cart_form中的qty输入元素,以避免用户修改数量 应该在erb代码或html上执行Deface覆盖
我使用以下覆盖任何成功 似乎是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>
答案 0 :(得分:0)
您正试图破坏生成的元素。在erb文件上运行Deface(输入元素尚不存在)。
尝试使用这样的代码:
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'"
)