I am setting up a shop, it consists of products, users, a cart, using several examples and getting in hand I make some modifications, but I find myself stuck in the following: At the end of the cart I want a button that closes it and allows the user to generate another separate cart, but I can not do something that works, any suggestions / help? I pass the code to you:
views / carts / show.html.erb:
<div class="cart_item">
<%= render "partials/cart_item" %>
</div>
views / partials / _cart_item.html.erb:
<% if !@order_item.nil? && @order_item.errors.any? %>
<ul>
<% @order_item.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
<br/>
<h3>Carrito de compras</h3>
<br/>
<div>
<%= link_to "Atras", root_path, class: 'btn btn-success' %>
</div>
<% if @order_items.size>0 %>
<% @order_items.each do |item| %>
<div>
<div class="well">
<div class="row">
<div class="col-sm-8">
<h3><%= item.product.title %></h3>
</div>
<div class="col-sm-4">
<div class="pull-right">
<%= form_for(item, remote: true) do |f| %>
<%= f.hidden_field :product_id, value: item.product.id %>
<h5><%= item.product.price %></h5>
<div class="form-group">
<%= f.number_field :quantity, value: item.quantity.to_i, min: 1, class: 'form-control' %>
</div>
<div class="btn-group">
<%= f.submit "Modificar", class: 'btn btn-primary' %>
<%= link_to "Eliminar", item, method: :delete, remote: true, class: 'btn btn-danger' %>
</div>
<span>Total Price: <%= item.total_price %></span>
<% end %>
</div>
</div>
</div>
</div>
</div>
<% end %>
<div>
<li><%= link_to "Finalizar compra", class: "fa fa-sign-out" %></li>
</div>
<% else %>
<% end %>
models / order.rb
class Order < ApplicationRecord
has_many :order_items
before_save :set_data
def subtotal
order_items.collect {|order_item| order_item.valid? ? (order_item.unit_price*order_item.quantity) : 0}.sum
end
def total
order_items.collect {|order_item| order_item.valid? ? (subtotal) : 0}.sum
end
# def usuario
# current_user.id
# end
private
def set_data
self[:subtotal] = subtotal
self[:total] = total
self[:status] = "to_checkout"
# self[:user_id] = usuario
end
end
I leave you the rar here with the project if you need more information: