简体   繁体   中英

adding more than one item to a cart using redis and rails

I've been following this guide on how to create a shopping cart using Rails, Redis, and the Braintree API. http://www.sitepoint.com/build-online-store-rails/

The guide teaches how to add a single movie to the shopping cart, and once you add that movie to the cart, the only available option is to remove it from the cart. I am trying to make it such that I can add multiply copies of the same movie within the cart. How do I accomplish this goal?

As oppose to movies, I have panels. The model, view, and controller are given below

panels.rb

class Panel < ActiveRecord::Base
    has_many :purchases
    has_many :buyers, through: :purchases

    def cart_action(current_user_id)
      if $redis.sismember "cart#{current_user_id}", id
        "Remove from"
      else
        "Add to"
      end
    end
end

panels_controller.rb

class PanelsController < ApplicationController
        before_action :logged_in_user
        before_action :set_panel, only: [:show, :edit, :update, :destroy]

        # GET /panels
        # GET /panels.json
        def index
            @panels = Panel.all
        end

        def show
            @panel = Panel.find(params[:id])
            @cart_action = @panel.cart_action current_user.try :id
        end



   panels/show.html.erb

    <p id="notice"><%= notice %></p>

    <p>
      <strong>Title:</strong>
      <%= @panel.title %>
    </p>

    <p>
      <strong>Location:</strong>
      <%= @panel.location %>
    </p>

    <p>
      <strong>Price:</strong>
      <%= @panel.price %>
    </p>

    <%=link_to "", class: "btn btn-danger", data: {target: @cart_action, addUrl: add_to_cart_path(@panel), removeUrl: remove_from_cart_path(@panel)} do%>
        <i class="fa fa-shopping-cart"></i>
        <span><%=@cart_action%></span> Cart
    <%end%>

panels.js.coffee
$(window).load ->
  $('a[data-target]').click (e) ->
    e.preventDefault()
    $this = $(this)
    if $this.data('target') == 'Add to'
      url = $this.data('addurl')
      new_target = "Remove from"
    else
      url = $this.data('removeurl')
      new_target = "Add to"
    $.ajax url: url, type: 'put', success: (data) ->
      $('.cart-count').html(data)
      $this.find('span').html(new_target)
      $this.data('target', new_target)

As I've only gotten into redis since the beginning of this guide, Any help would would greatly appreciated!

One way I found to accomplish this was to add the panel ids to a hash instead, where the key is the panel id and the qty is the value:

{ panel_id => qty }

using hmset :

$redis.hmset current_user_cart, panel, item_qty

This will add the key=>value pair under the current_user_cart key, to retrieve the panel id's you can use hkeys , which will retrieve all the hash keys:

panel_ids = $redis.hkeys current_user_cart

Then to get the qtys you could call hgetall:

@cart_qtys = $redis.hgetall current_user_cart

Which will return the complete hash, eg { panel_id => qty }, which you can then reference.(it should be noted that the qty's will be returned as strings)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM