私のグループ プロジェクトでは、Web サイトの「カートに追加」機能を作成しています。アイテムをカートに追加し、ユーザーをカート ページにリダイレクトする「カートに追加」ボタンを追加するにはどうすればよいですか?カート ページ内で、追加された商品を表示する必要があります。現時点ではボタンを作成しましたが、ユーザーはカート ページにリダイレクトされません。商品ページに残っております。
//カートコントローラー
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])
  end
end
//カートモデル
class Cart < ApplicationRecord
  has_many :items
  def add_item(prebuilt_id)
    item = items.where('prebuilt_id', prebuilt_id.first)
    save
  end
end
//アイテムモデル
class Item < ApplicationRecord
  belongs_to :cart
end
//ルート
# root to: redirect('/cart')
  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';
// 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
)
// 商品ページ
<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", cart_path(:prebuilt_id => prebuilt), method: :post %></td>
        </td>
      </tr>
    <% end %>
  </tbody>
</table>
// カートページ
<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>
class CartController < ApplicationController
  def add_to_cart
    current_cart.add_item(params[:prebuilt_id])
    redirect_to cart_path(current_cart.id)
  end
end
項目はおそらく Prebuild にも属しているはずです
class Item < ApplicationRecord
  belongs_to :cart
  belongs_to :prebuild
end
アイテムの追加は、既存のアイテムを検索して更新したり、新しいアイテムを作成したりすることに似ています。
class Cart < ApplicationRecord
  has_many :items
  def add_item(prebuilt_id)
    item = items.find_by(prebuilt_id: prebuilt_id)
    if item
      item.update(count: item.count + 1)
    else
      items.create(prebuilt_id: prebuilt_id, count: 1)
    end
  end
end
または、カウントがなく、各位置が 1 だけの場合
  def add_item(prebuilt_id)
    items.find_or_create_by(prebuilt_id: prebuilt_id)
  end