我正在跟隨來自Michael Hartl的教程,並創建了一個購物車,我遇到了幾個問題。紅寶石在軌道上,問題與購物車&current_user
每個用戶都可以創建不同的「身份證」一個新的購物車,但是當不同的用戶添加產品到購物車,增加產品在不同的「身份證」的所有車用CURRENT_USER
補充,而不是特定的購物車如何限制用戶只查看自己的購物車,而無法查看其他用戶購物車?
請指導解決上述問題,非常感謝!
user.rb(不是一個完整的代碼,因爲這將是漫長的,添加了 'HAS_ONE:購物車',除了從邁克爾·哈特爾教程原代碼)
class User < ActiveRecord::Base
attr_accessor :remember_token, :activation_token, :reset_token
before_save :downcase_email
before_create :create_activation_digest
has_many :orders
has_one :cart
cart.rb
class Cart < ActiveRecord::Base
has_many :line_items, dependent: :destroy
belongs_to :user
def add_product(product_id)
current_item = line_items.find_by(product_id: product_id)
if current_item
current_item.quantity += 1 #quantity of line_item, product in cart
else
current_item = line_items.build(product_id: product_id)
end
current_item
end
def total_price
line_items.to_a.sum { |item| item.total_price }
end
end
顧慮/ Current_Cart.rb
module CurrentCart
extend ActiveSupport::Concern
private
def set_cart
@cart = current_user.cart || current_user.create_cart
session[:cart_id] = @cart.id
end
end
line_items_controller.rb
class LineItemsController < ApplicationController
include CurrentCart
before_action :set_cart, only: [:create] #before create, execute :set_cart, find(or create) cart
before_action :set_line_item, only: [:show, :edit, :update, :destroy]
def index
@line_items = LineItem.all
end
def show
end
def new
@line_item = LineItem.new
end
def edit
end
def create
product = Product.find(params[:product_id])
@line_item = @cart.add_product(product.id)
if @line_item.save
redirect_to current_user.cart
else
render :new
end
end
def update
if @line_item.update(line_item_params)
redirect_to @line_item, notice: 'Line item was successfully updated.'
else
render :edit
end
end
def destroy
@line_item.destroy
redirect_to line_items_url, notice: 'Line item was successfully destroyed.'
end
private
def set_line_item
@line_item = LineItem.find(params[:id])
end
def line_item_params
params.require(:line_item).permit(:product_id)
end
end
carts_controller.rb
class CartsController < ApplicationController
before_action :set_cart, only: [:edit, :update, :destroy]
rescue_from ActiveRecord::RecordNotFound, with: :invalid_cart
def show
@cart = current_user.cart
end
def edit
end
def update
if @cart.update(cart_params)
redirect_to @cart, notice: 'Cart was successfully updated.'
else
render :edit
end
end
def destroy
@cart.destroy if @cart.id == session[:cart_id]
session[:cart_id] = nil
redirect_to store_url
end
private
# Use callbacks to share common setup or constraints between actions.
def set_cart
@cart = Cart.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def cart_params
params.fetch(:cart, {})
end
def invalid_cart
logger.error "Attempt to access invalid cart #{params[:id]}"
redirect_to store_url, notice: 'Invalid cart'
end
end
「我正在關注邁克爾哈特爾的教程並創建了購物車」,我不記得邁克爾哈特關於建立購物車的書。不過,這讓我想起了很多Agile Web Development with Rails書籍。如果是這樣,「如何限制用戶只能查看他們自己的購物車,而不能查看其他用戶購物車?」,我不明白結果會如何發生。我看着我的回購庫,你的'current_cart.rb'代碼看起來是正確的。 – fbelanger
是的,我基本上整合了兩個教程(用戶登錄/從邁克爾哈特爾和敏捷開發電子商務登錄),例如,即時登錄作爲ID爲'1'的用戶,我創建了我的購物車ID爲'1'。我退出後,再次登錄另一個ID爲'2'的帳戶,創建了一個id爲'2'的購物車,但是當我使用鏈接購物車/ 1訪問另一個購物車時,我仍然能夠從另一個用戶那裏看到推車,即將發生。希望你明白 –