Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/60.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 按钮“;加上「;行为人很奇怪_Ruby On Rails_Ruby On Rails 3 - Fatal编程技术网

Ruby on rails 按钮“;加上「;行为人很奇怪

Ruby on rails 按钮“;加上「;行为人很奇怪,ruby-on-rails,ruby-on-rails-3,Ruby On Rails,Ruby On Rails 3,应用程序有添加到购物车按钮,该按钮创建新的@line\u项目(代码基于Rails的敏捷开发)。 我添加了一个Ajax功能,因此添加到购物车不会重新加载页面,而是将@line\u项目添加到@Cart 但是Rails每按一次按钮就执行三次 我按下“添加到购物车”,它会添加3个项目 而且,当我推着购物车时,它会问我三次“你确定吗?” 不知道是什么导致这些,有什么想法吗 def create @cart = current_cart product = Product.find(params[:prod

应用程序有
添加到购物车
按钮,该按钮创建新的
@line\u项目
(代码基于Rails的敏捷开发)。 我添加了一个Ajax功能,因此
添加到购物车
不会重新加载页面,而是将
@line\u项目添加到@Cart

但是Rails每按一次按钮就执行三次

我按下“添加到购物车”,它会添加3个项目

而且,当我推着购物车时,它会问我三次“你确定吗?” 不知道是什么导致这些,有什么想法吗

def create
@cart = current_cart
product = Product.find(params[:product_id]) 
@line_item = @cart.add_product(product.id)
respond_to do |format|
if @line_item.save
format.html { redirect_to(store_url) }
format.js
format.xml { render :xml => @line_item,
:status => :created, :location => @line_item }
else
format.html { render :action => "new" }
format.xml { render :xml => @line_item.errors,
:status => :unprocessable_entity }
end
end
end
按钮:

<%= button_to 'Add to Cart' , line_items_path(:product_id => product),:remote => true %>
product]:remote=>true%>
create.js.erb:

$('#cart').html("<%= escape_javascript(render(@cart)) %>");
$('#cart').html(“”);
_cart.html.erb模板:

<div class="cart_title" >Your Cart</div>
<table>
<%= render(cart.line_items) %>
<tr class="total_line" >
<td colspan="2" >Total</td>
<td class="total_cell" ><%= number_to_currency(cart.total_price) %></td>
</tr>
</table>
<%= button_to 'Empty cart' , cart, :method => :delete,
:confirm => 'Are you sure?' %>
<tr>
<td><%= line_item.quantity %>&times;</td>
<td><%= line_item.product.title %></td>
<td class="item_price" ><%= number_to_currency(line_item.total_price) %></td>
</tr>
您的购物车
全部的
:删除,
:confirm=>'你确定吗?'>
_行_item.html.erb模板:

<div class="cart_title" >Your Cart</div>
<table>
<%= render(cart.line_items) %>
<tr class="total_line" >
<td colspan="2" >Total</td>
<td class="total_cell" ><%= number_to_currency(cart.total_price) %></td>
</tr>
</table>
<%= button_to 'Empty cart' , cart, :method => :delete,
:confirm => 'Are you sure?' %>
<tr>
<td><%= line_item.quantity %>&times;</td>
<td><%= line_item.product.title %></td>
<td class="item_price" ><%= number_to_currency(line_item.total_price) %></td>
</tr>

&时代;
问题已解决

在我的视图应用程序布局中,我认为这样做是正确的

<!DOCTYPE html>
<html>
<head>
  <title>Store</title>
  <%= stylesheet_link_tag "store" %>
  <%= javascript_include_tag 'application' %>
  <%= csrf_meta_tags %>
  <title>Cart</title>
  <%= stylesheet_link_tag "carts" %>
  <%= javascript_include_tag 'cart' %> #this
  <%= csrf_meta_tags %>

商场
运货马车
#这个

事实上,我想一旦你在主页上使用了“应用程序”,这将简化JS代码的执行

请向我们展示这个按钮的代码,以及该页面的完整源代码。编辑mu的第一篇文章和其他信息它发生了一次,所有事件都被触发了两次。你是这样吗?如果是,那么可能是您的一些gems/javasript文件多次添加jquery。删除的js文件,同样的东西,所以可能不是,但是如果是jquery或gem bug,如何解决这个问题呢?