Javascript 如何在rails中的购物车中添加多个项目?
我在rails的简易商店遇到了一些问题。我想在购物车中添加一组选定的产品,但我不知道如何添加 我在我的3组产品上使用单选按钮,我希望用户选择所有3个项目,然后按“添加到购物车”按钮 在购物车中,必须按如下数组:username mix(1-id、2-id、3-id)。但我还有一个问题。我无法获得我的产品的id,也不知道为什么 所有产品都来自Javascript 如何在rails中的购物车中添加多个项目?,javascript,ruby-on-rails,shopping-cart,Javascript,Ruby On Rails,Shopping Cart,我在rails的简易商店遇到了一些问题。我想在购物车中添加一组选定的产品,但我不知道如何添加 我在我的3组产品上使用单选按钮,我希望用户选择所有3个项目,然后按“添加到购物车”按钮 在购物车中,必须按如下数组:username mix(1-id、2-id、3-id)。但我还有一个问题。我无法获得我的产品的id,也不知道为什么 所有产品都来自seed.db,带有以下字段::类别id、:菜单id、:标题、:价格、:描述和:路径到图像 这是我的密码: class ProductController
seed.db
,带有以下字段::类别id、:菜单id、:标题、:价格、:描述和:路径到图像
这是我的密码:
class ProductController < ApplicationController
before_action :admin_user, only: :edit
def show
@products = Product.where("category_id = 1").limit(3)
@products2 = Product.where("category_id = 2").limit(3)
@products3 = Product.where("category_id = 3").limit(3)
end
private
def product_params
params.permit(
:category_id,
:menu_id,
:title,
:discribe,
:price,
:path_to_image
)
end
end
product/show/html.erb
<div class="col-md-4 about-left">
<table class="tfood">
<td><p><b>Main</b></p></td>
<% @products.each do |i| %>
<tr>
<td>
<%= i.title %><%= image_tag i.path_to_image %>
</td>
<td>
<%= i.discribe %>
<p><b>Price: <%= i.price %></b></p>
<input name="main" type="radio" value="">
</td>
</tr>
<% end %>
</table>
</div>
<div class="col-md-4 about-left">
<table class="tfood">
<td>
<p><b>Topping</b></p>
</td>
<% @products2.each do |i| %>
<tr>
<td>
<%= i.title %>
<%= image_tag i.path_to_image %>
</td>
<td>
<%= i.discribe %>
<p><b>Price: <%= i.price %></b></p>
<input name="topping" type="radio" value="">
</td>
</tr>
<% end %>
</table>
</div>
<div class="col-md-4 about-left">
<table class="tfood">
<td>
<p><b>Drink</b></p>
</td>
<% @products3.each do |i| %>
<tr>
<td>
<%= i.title %>
<%= image_tag i.path_to_image %>
</td>
<td>
<%= i.discribe %>
<p><b>Price: <%= i.price %></b></p>
<input name="drink" type="radio" value="">
</td>
</tr>
<% end %>
</table>
</div>
<script>
function add_to_cart(id){
var x = window.localStorage.getItem(id);
x = x * 1 + 1;
window.localStorage.setItem(id, x);
}
</script>
主要
价格:
浇头
价格:
喝
价格:
功能添加到购物车(id){
var x=window.localStorage.getItem(id);
x=x*1+1;
window.localStorage.setItem(id,x);
}
看起来您没有在单选按钮内设置值。也许最好写点像这样的东西
<input name="drink" type="radio" value=<%= i.id %>>
注意。我没有在IDE中检查我的代码,这只是想法。事实上,我大约一年前就与ERB合作过,我对语法不太清楚。嗨,Andrew,当单击
添加到购物车
按钮时,只需检查单选按钮的值字段是否为空。如果要切换启用和禁用添加到购物车按钮,请编写一个函数,在该函数中必须执行上述操作。此脚本看起来如何?我的JSI技能很差,无论是否选中,我都会看到我的单选按钮发送未定义。这是一个问题,但现在如何将这些ID放入函数add_to_cart?如果我把提交按钮放低,那么它就是不获取id。如果上面-我得到3个按钮我的脚本:函数add_to_cart(id,id2,id3){var key=“username's mix”(“+id+”,“+id2+”,“+id3+”);var x=window.localStorage.getItem(key);x++;window.localStorage.setItem(key,x);},当我按下按钮时得到:username's mix(未定义,未定义,未定义):“1”
<input name="drink" type="radio" value=<%= i.id %>>