I have some problem in my simple shop in rails. I want to add group of chosen products in cart, but I don't know how.
I use radio buttons on my 3 groups of products and I want user choose all 3 items and then press the button "add to cart".
In cart it's must by array like this: username-mix(1-id, 2-id, 3-id). But i have one more problem. I can't get id's of my products and don't know why.
Products are all from seed.db
with fields: :category_id, :menu_id, :title, :price, :discribe and:path_to_image
.
Here is my code:
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
My Product
model:
class Product < ApplicationRecord
validates :title, presence: true
validates :price, presence: true
belongs_to :category
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>
It's look like you didn't set value inside your radio-buttons. Maybe it will be better write something like
<input name="drink" type="radio" value=<%= i.id %>>
Note. I didn't check my code in the IDE, it's just thoughts. Actually I worked with ERB about year ago and I'm not sure about syntax.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.