2015-01-08 4 views
0

사용자에게 할당 된 장바구니를 만들려고합니다. 장바구니에 항목이 추가 될 때마다 지금은 어떤 일이 발생합니까? 모든 사용자에 대해 추가됩니다. 모든 사용자가 동일한 장바구니를 사용하고 있으며 다른 사용자에게 장바구니에 추가 할 수 없게합니다. 각 사용자가 고유 한 장바구니를 보유하고 각 사용자가 장바구니에만 추가 할 수 있도록하려면 어떻게해야합니까? 여기 사용자에게 카트 할당

내가 가진 무엇 :

class CartsController < ApplicationController 
    before_action :set_cart, only: [:show, :edit, :update, :destroy] 
    before_action :logged_in_user, only: [:create, :edit, :update, :destroy, :show] 
    rescue_from ActiveRecord::RecordNotFound, with: :invalid_cart 

    # GET /carts 
    # GET /carts.json 
    def index 
    @carts = Cart.all 
    end 

    # GET /carts/1 
    # GET /carts/1.json 
    def show 
    end 

    # GET /carts/new 
    def new 
    @cart = Cart.new 
    end 

    # GET /carts/1/edit 
    def edit 
    end 

    # POST /carts 
    # POST /carts.json 
    def create 
    @cart = Cart.new(cart_params) 
    respond_to do |format| 
     if @cart.save 
     format.html { redirect_to @cart, notice: 'Cart was successfully created.' } 
     format.json { render action: 'show', status: :created, location: @cart } 
     else 
     format.html { render action: 'new' } 
     format.json { render json: @cart.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # PATCH/PUT /carts/1 
    # PATCH/PUT /carts/1.json 
    def update 
    respond_to do |format| 
     if @cart.update(cart_params) 
     format.html { redirect_to @cart, notice: 'Cart was successfully updated.' } 
     format.json { head :no_content } 
     else 
     format.html { render action: 'edit' } 
     format.json { render json: @cart.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /carts/1 
    # DELETE /carts/1.json 
    def destroy 
    @cart.destroy if @cart.id == session[:cart_id] 
    session[:cart_id] = nil 
    respond_to do |format| 
     format.html { redirect_to store_url, notice: "Your cart is currently empty" } 
     format.json { head :no_content } 
    end 
    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[:cart] 
    end 

    def invalid_cart 
     logger.error "Attempt to access invalid cart #{params[:id]}" 
     redirect_to store_url, notice: "Invalid Cart" 
    end 
end 

current_cart.rb

module CurrentCart 
    extend ActiveSupport::Concern 

    private 
    def set_cart 
     @cart = Cart.find(session[:cart_id]) 
     rescue ActiveRecord::RecordNotFound 
     @cart = Cart.create 
     session[:cart_id] = @cart.id 
    end 
end 

line_item_controller.rb

class LineItemsController < ApplicationController 
    include CurrentCart 
    before_action :set_cart, only: [:new, :create] 
    before_action :set_line_item, only: [:show, :edit, :update, :destroy] 


    # GET /line_items 
    # GET /line_items.json 
    def index 
    @line_items = LineItem.all 
    end 

    # GET /line_items/1 
    # GET /line_items/1.json 
    def show 
    end 

    # GET /line_items/new 
    def new 
    @line_item = LineItem.new 
    end 

    # GET /line_items/1/edit 
    def edit 
    end 

    # POST /line_items 
    # POST /line_items.json 
    def create 
    product = Product.find(params[:product_id]) 
    @line_item = @cart.add_product(product: product) 
    respond_to do |format| 
     if @line_item.save 
     format.html { redirect_to @line_item.cart, notice: 'Line item was successfully created.' } 
     format.json { render action: 'show', status: :created, location: @line_item } 
     else 
     format.html { render action: 'new' } 
     format.json { render json: @line_item.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # PATCH/PUT /line_items/1 
    # PATCH/PUT /line_items/1.json 
    def update 
    respond_to do |format| 
     if @line_item.update(line_item_params) 
     format.html { redirect_to @line_item.cart, notice: 'Line item was successfully updated.' } 
     format.json { head :no_content } 
     else 
     format.html { render action: 'edit' } 
     format.json { render json: @line_item.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /line_items/1 
    # DELETE /line_items/1.json 
    def destroy 
    @line_item.destroy 
    respond_to do |format| 
     format.html { redirect_to line_items_url } 
     format.json { head :no_content } 
    end 
    end 

    private 
    # Use callbacks to share common setup or constraints between actions. 
    def set_line_item 
     @line_item = LineItem.find(params[:id]) 
    end 

    # Never trust parameters from the scary internet, only allow the white list through. 
    def line_item_params 
     params.require(:line_item).permit(:product_id) 
    end 
end 
+0

Spree를보고 "cart"설정 방법을 확인하는 것이 좋습니다. 나에게 사용자는 카트가 많지 않고 사용자는 많은 주문과 주문을했으며 "완료", "유료", "선적 된"및 첫 번째로 "카트"와 같은 다양한 상태에 있습니다. – BenMorganIO

답변

0

대신 Cart.createCart.find CurrentCart.rb, 당신은해야 ~해라. 같은 힝 :

def set_cart 
    @cart = current_user.carts.find_by(id: session[:cart_id]) || current_user.carts.create 
    session[:cart_id] = @cart.id 
end 

find_by 그것을 찾을 수없는 경우 오류가 발생하지 않습니다; 당신이 구출에 대해 걱정할 필요가 없도록 그냥 nil을 반환합니다.

+0

또한, 사용자를 로그 아웃 할 때'session [: cart_id] = nil'을 설정해야합니다 – evanbikes