Skip to content

Instantly share code, notes, and snippets.

@dylanerichards
Last active May 25, 2022 01:13
Show Gist options
  • Save dylanerichards/78098e9434c4645f822699425244146f to your computer and use it in GitHub Desktop.
Save dylanerichards/78098e9434c4645f822699425244146f to your computer and use it in GitHub Desktop.
class CartProductsController < ApplicationController
def create
cart_products = current_user.cart.products
if cart_products.map(&:product_id).include?(params[:product_id])
cart_product = cart_products.select { |product| product.product_id == params[:product_id] }.first
cart_product.update(quantity: cart_product.quantity + 1)
cart_product.save
else
CartProduct.create(
cart_id: params[:cart_id],
product_id: params[:product_id],
quantity: params[:quantity]
)
end
redirect_to user_cart_path(current_user)
end
end
<div>
<h1>
This is the cart for <%= current_user.email %>
</h1>
<h2>
Items:
</h2>
<div class="products-grid flex flex-wrap justify-center space-x-2 ">
<% @products.each do |product| %>
<div class="flex-none mb-5">
<div>
<%= link_to image_tag(product.image, class: "product-image mb-2", width: 390, height: 550), product_path(product) %>
</div>
<div class="product-details mx-3">
<div class="flex justify-between">
<div>
<%= link_to product.name, product_path(product) %>
</div>
<div>
<svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
<path stroke-linecap="round" stroke-linejoin="round" d="M5 5a2 2 0 012-2h10a2 2 0 012 2v16l-7-3.5L5 21V5z" />
</svg>
</div>
</div>
<div>
$<%= product.price %>
</div>
<div class="mt-3 text-gray-400 text-sm">
<%= (1..10).to_a.sample %> colors
</div>
</div>
</div>
<% end %>
</div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment