我需要团队项目的帮助。我的任务是“添加到购物车”功能。我被困住了。我需要在产品页面上的每个项目旁边添加一个按钮,单击该按钮时,它会添加该项目并将用户定向到购物车页面。下面我添加了代码以提供更好的理解。
//小车控制器
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';
// 种子.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