我正在学习ajax,但即使我编写的第一个应用程序仍然无效。点击链接后,没有任何变化。
cart_controller.rb
def cart_add_quan
id_cart = params[:id]
cart = session[:cart]
cart.each do |id, quantity|
if id_cart == id then
cart[id] += 1
end
end
respond_to do |format|
format.html {redirect_to cart_path}
format.js
end
end
视图/购物车/的 index.html.erb
<h1>Your cart:</h1>
<td><%= link_to 'Continue shopping', product_user_index_path %></td>
<% if @cart.empty? %>
<p>Your cart is empty</p>
<% else %>
<div id="carts">
<%= render 'carts' %>
<br>
<h2><%= link_to 'Check out', check_out_index_path %></h2>
<br>
<td><%= link_to 'Empty your cart', cart_clear_path %></td>
</div>
<% end %>
视图/购物车/的 _carts.html.erb
<br>
<table>
<thead>
<tr>
<th>Product name</th>
<th>Image</th>
<th>Description</th>
<th>SubPrice</th>
<th>Quantity</th>
<th></th>
</tr>
</thead>
<tbody>
<br>
<% @cart.each do |id, quantity| %>
<% product = Product.find_by_id(id) %>
<tr>
<td><%= link_to product.name, product %></td>
<td><%= image_tag(product.image.thumb('45x45#').url, class: "SlideItMoo_element") if product.image_stored? %></td>
<td><%= product.description.html_safe %></td>
<td><%= sub_price(product, quantity) %></td>
<td>
**#link to *cart_quan_path***
<%= link_to "+", cart_add_quan_path(id), remote: true %>
<%= quantity %>
<%= link_to "-", cart_sub_quan_path(id) %></td>
<td><%= link_to 'remove', cart_remove_product_path(id) %></td>
</tr>
<% end %>
</tbody>
</table>
<h3 id ="total">Totald: <%= totalCart(@cart) %></h3>
视图/购物车/的 cart_add_quan.js.erb
$('#carts').html('<%= escape_javascript(render("carts")) %>');
console.log("hihi");