Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
766 views
in Technique[技术] by (71.8m points)

ruby on rails - Creating multiple carts for every session

I m trying to build an e-commerce website using rails. I have controller( line_items_controller.rb) I have created a concern/set_cart module This is my line_items_controller.rb

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

  # 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 = LineItem.new(line_item_params)
    @line_item = @cart.add_product(product)
    @cart.user_id = current_user.id
    @cart.save
    respond_to do |format|
      if @line_item.save
        format.html { redirect_to root_path, notice: 'Line item was successfully created.' }
        format.json { render :show, status: :created, location: @line_item }
      else
        format.html { render :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, notice: 'Line item was successfully updated.' }
        format.json { render :show, status: :ok, location: @line_item }
      else
        format.html { render :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, notice: 'Line item was successfully destroyed.' }
      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])
      li = LineItem.find(params[:id])
      puts "This item is for #{li.product.title}"
      
      
    end

    # Only allow a list of trusted parameters through.
    def line_item_params
      params.require(:line_item).permit(:product_id, :cart_id) #Check this
    end
end

This is my module concerns/current_cart.rb

module CurrentCart
  private

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

     session[:cart_id] = @cart.id
  end

  
end

Each time I log in and a new cart is being created. I have a column user_id in carts table.

As soon as I log in if the user id is present I don't want to create a new cart. Just want the existing cart

Thank you in advance.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It looks like you might not have your rescue block set properly. Here's some more info on error handling. Try this:

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

Alternatively, without a rescue block:

def set_cart
  if session[:cart_id]
    @cart = Cart.find(session[:cart_id])
  else
    @cart = Cart.create
    session[:cart_id] = @cart.id
  end
end

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...