私はMichael Hartlのチュートリアルに従っており、私はいくつかの問題に遭遇したショッピングカートを作成しました。ruby on rails、cart&current_userで問題
各ユーザは、異なる「ID」を持つ新しいショッピングカートを作成することができますが、別のユーザーがカートに製品を追加する場合、追加の製品は、CURRENT_USER
によって代わりにその特定のカートの異なる「ID」の全てカートに追加します他のユーザーカートを表示することなく、自分のカートを表示するようにユーザーを制限する方法を教えてください。
ありがとうございました!
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
「私はMichael Hartlのチュートリアルに従っており、ショッピングカートを作成しています」というマイケルハートの書籍はショッピングカートを構築することを思い出しません。しかし、これは私に多くのAgile Web Development with Railsの本を思い出させます。もしそうなら、「他のユーザーのカートを見ることができないまま、自分のカートを見るだけに制限する方法は?」と、私は結果がどうなるか理解していません。私は私のレポを見て、あなたの 'current_cart.rb'コードは正しいようでした。 – fbelanger
はい、基本的に両方のチュートリアルを統合しています(ユーザサインイン/ michael hartlからのログインとアジャイル開発のeコマース)、例としてid '1'のユーザとしてログインした場合、id '1'のカートを作成しました。私はログアウトし、ID「2」の別のアカウントでもう一度サインインし、ID「2」のカートを作成しましたが、リンクカート/ 1の別のカートにアクセスすると、発生する。あなたが理解してほしい –