Rails Deface on input erb element, cannot override it correctly

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-13 06:01:13

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!