Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails javascript关闭时自动更新值_Ruby On Rails - Fatal编程技术网

Ruby on rails javascript关闭时自动更新值

Ruby on rails javascript关闭时自动更新值,ruby-on-rails,Ruby On Rails,在我的应用程序中,我展示了所有这样的产品 store.html.erb <% @products.each do |product| %> <div class="entry" > <%= image_tag(product.image_url) %> <h3><%= product.title %></h3> <%= sanitize(product.description

在我的应用程序中,我展示了所有这样的产品 store.html.erb

<% @products.each do |product| %>
    <div class="entry" >
      <%= image_tag(product.image_url) %>
      <h3><%= product.title %></h3>
      <%= sanitize(product.description) %>
      <div class="price_line" >
        <span class="price" ><%= number_to_currency(product.price) %></span>
        <%= button_to "add to cart", line_items_path(:product_id => product),
        :remote => true %>
      </div>
    </div>
<% end %>
到目前为止,我所理解的是,由于没有format.js add to cart,所以不应该向cart添加任何产品,也不应该发生任何事情。但是当我在控制器中添加.js格式时

format.js { @current_item = @line_item }
并更新页面,我看到添加到购物车工作落后,但没有显示结果。更新页面后,我在购物车中看到15个左右的项目。这是怎么发生的?

在此处进行更改

<% @products.each do |product| %>
    <div class="entry" >
      <%= image_tag(product.image_url) %>
      <h3><%= product.title %></h3>
      <%= sanitize(product.description) %>
      <div class="price_line" >
        <span class="price" ><%= number_to_currency(product.price) %></span>
        <%= button_to "add to cart", line_items_path(:product_id => product.id),
        :remote => true %> #changes line
      </div>
    </div>
<% end %>

产品编号),,
:remote=>true%>#更改行
对于更新,您必须为此创建.js文件,并通过jquery或javascript更新html页面的元素和对象。 并且只使用一种格式,以便控制器方法转到一个响应区域

<% @products.each do |product| %>
    <div class="entry" >
      <%= image_tag(product.image_url) %>
      <h3><%= product.title %></h3>
      <%= sanitize(product.description) %>
      <div class="price_line" >
        <span class="price" ><%= number_to_currency(product.price) %></span>
        <%= button_to "add to cart", line_items_path(:product_id => product.id),
        :remote => true %> #changes line
      </div>
    </div>
<% end %>