For my project, I need to create an ‘add to cart’ button that adds the item to cart and directs the user to the cart page (localhost:3030/cart). I have a button created but it is not functioning. I need help. I’m very new to rails so I don’t know much of how to navtigate through it all. I only know the little that I was taught in a couple months in a semester.
Cart Model
class Cart < ApplicationRecord
has_many :items
def add_item(prebuilt_id)
items.find_or_create_by(prebuilt_id: prebuilt_id)
end
end
Item Model
class Item < ApplicationRecord
belongs_to :cart
belongs_to :prebuilt
end
Cart Controller
class CartController < ApplicationController
def index
@cart = Prebuilt.all
render :index
end
def show
@cart = current_cart
end
def add_to_cart
current_cart.add_item(params[:prebuilt_id])
redirect_to cart_path(current_cart.id)
flash[:success] = 'Item successfully updated!'
end
end
Item Controller
class Item < ApplicationRecord
belongs_to :cart
belongs_to :prebuilt
end
Prebuilt Controller
class PrebuiltsController < ApplicationController
before_action :authenticate_user!, except: [:index]
def index
@prebuilts = Prebuilt.all
render :index
end
def show
@prebuilt = Prebuilt.find(params[:id])
render :show
end
end
Cart Page
<% content_for(:html_title) { 'Junior Jets | Cart' } %>
<h1 style="font-family:'Courier New'"><b>YOUR CART</b></h1>
<div class="cart">
<style>
#right {
text-align: right;
background-color: #ffffff;
}
</style>
<table id="line_items">
<tr>
<th>Product</th>
<th>Qty</th>
<th class="price">Unit Price</th>
<th class="price">Full Price</th>
</tr>
<% for item in @cart %>
<tr class="<%= cycle :odd, :even %>
<td><%=h item.name %></td>
</tr>
<% end %>
</tr>
</table>
<div id="right">
<div class="totals">
<div class="totals-item">
<label>Subtotal</label>
<div class="totals-value" id="cart-subtotal">71.97</div>
</div>
<div class="totals-item">
<label>Tax (10%)</label>
<div class="totals-value" id="cart-tax">3.60</div>
</div>
<div class="totals-item">
<label>Shipping</label>
<div class="totals-value" id="cart-shipping">15.00</div>
</div>
<div class="totals-item totals-item-total">
<label>Grand Total</label>
<div class="totals-value" id="cart-total">90.57</div>
</div>
</div>
<a href="http://localhost:3000/checkout">
<button>Checkout</button>
</a>
</div>
</div>
Prebuilt Page aka Product Page
<h1>Jets</h1>
<h3> Artistic Jet
<%= image_tag 'artisticplane.jpg',
class: 'float-start w-50 l-50 rounded-circle shadow-lg d-block mx-auto',
style: 'max-width: 16rem;',
loading: 'lazy'
%>
</h3>
<hr>
<h3> Barbie Jet
<%= image_tag 'barbieplane.jpg',
class: 'float-start w-10 l-10 rounded-circle shadow-lg d-block mx-auto',
style: 'max-width: 16rem;', loading: 'lazy'
%>
</h3>
<hr>
<h3> Prehistoric Jet
<%= image_tag 'prehistoricplane.jpg',
class: 'float-start w-50 l-50 rounded-circle shadow-lg d-block mx-auto',
style: 'max-width: 16rem;',
loading: 'lazy'
%>
</h3>
<hr>
<h3> Tractor-Themed Jet
<%= image_tag 'tractorplane.jpg',
class: 'float-start w-50 l-50 rounded-circle shadow-lg d-block mx-auto',
style: 'max-width: 16rem;',
loading: 'lazy'
%>
</h3>
<table class="table table-striped table-hover">
<thead class="table-dark">
<tr>
<th>Name</th>
<th>Description</th>
<th>Manufacturer</th>
<th>Color</th>
<th>Size</th>
<th>Price</th>
<th></th>
</tr>
</thead>
<tbody>
<% @prebuilts.each do |prebuilt| %>
<tr>
<td><%= prebuilt.name %></td>
<td><%= prebuilt.description %></td>
<td><%= prebuilt.description %></td>
<td><%= prebuilt.manufacturer %></td>
<td><%= prebuilt.color %></td>
<td><%= prebuilt.size %></td>
<td><%= prebuilt.price %></td>
<td class="text-nowrap">
<%= link_to 'show', prebuilt_path(prebuilt), class: 'btn btn-sm btn-outline-secondary' %>
<td><%= button_to 'add to cart', add_to_cart_path(prebuilt_id: prebuilt), method: :post %></td>
</td>
</tr>
<% end %>
</tbody>
</table>
<p>
<%= link_to 'New Jet', new_jet_path, class: 'btn btn-secondary' %>
</p>
Routes
get 'cart', to: 'cart#index', as: 'cart';
post '/add_to_cart/:prebuilt_id' => 'cart#add_to_cart', :as => 'add_to_cart'
get 'checkout', to: 'pages#checkout', as: 'checkout';
get 'confirm', to: 'pages#confirm', as: 'confirm';
get 'prebuilts', to: 'prebuilts#index', as: 'prebuilts'
get 'prebuilts/:id', to: 'prebuilts#show', as: 'prebuilt'
Seeds.rb
prebuilt1 = Prebuilt.create!(
name: "Alfred's Artistic Jet",
description: "Designed by Alfred",
manufacturer: "Boeing",
color: "Mixed",
size: "Large",
price: 2999999
)
prebuilt2 = Prebuilt.create!(
name: "Brittany's Barbie Jet",
description: "Designed by Brittany",
manufacturer: "Boeing",
color: "Pink",
size: "Large",
price: 4499999
)
prebuilt3 = Prebuilt.create!(
name: "Peter's Prehistoric Jet",
description: "Designed by Peter",
manufacturer: "Airbus",
color: "Blue",
size: "Small",
price: 7999999
)
prebuilt4 = Prebuilt.create!(
name: "Tianna's Tractor-Themed Jet",
description: "Designed by Tianna",
manufacturer: "Airbus",
color: "Mixed",
size: "Large",
price: 1999999
)
user24686287 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.