问题
I am trying to override the qty input element in the products/_cart_form , to avoid a user modifying the qty Should the Deface override be performed on the erb code or the html
I used the following override wo any success seems to be a mix of html selector and erb code .... messy and tricky , obviously no example like this on the Deface github readme
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'}
)
the spree/products/_cart_form.erb code is ( related to the input element)
<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 %>
and it generates the following 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>
回答1:
You are trying to deface a generated element. Deface is run on the erb file (where the input element doesn't exist yet).
Try using a code deface like this:
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'"
)
来源:https://stackoverflow.com/questions/15008983/rails-deface-on-input-erb-element-cannot-override-it-correctly