Add to cart button isn’t functioning

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
)

New contributor

user24686287 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật