输入erb元素上的Rails Deface,无法正确覆盖它 [英] Rails Deface on input erb element, cannot override it correctly
问题描述
我正在尝试覆盖products/_cart_form中的qty输入元素,以避免用户修改qty 应该在erb代码或html
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
我成功使用了以下替代方法 似乎是html选择器和erb代码的混合....杂乱无章,显然在Deface github自述文件
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'}
)
spree/products/_cart_form.erb代码是(与输入元素有关)
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 %>
并生成以下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>
推荐答案
您正试图污损所生成的元素. Deface在erb文件(输入元素尚不存在)上运行.
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'"
)
这篇关于输入erb元素上的Rails Deface,无法正确覆盖它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!