dimanche 9 décembre 2018

How to implement persistent Rails shopping cart?

I am working on a shopping cart for a rails shop app and have different implementations working.

A) carts controller

class CartsController < ApplicationController

  def add
  ...
  end

  def remove
  ...
  end

  def clear
  ...
  end
end

& products, cart and cart_items models

Here I add the product through something such as:

 <%= button_to "Confirm", :controller => 'carts', :action => "add", :id => params[:id] %>

And so on

B) carts controller

class CartsController < ApplicationController

  def show
    ...
  end
end

order_items controller

class OrderItemsController < ApplicationController

  def create
    ...
  end

  def update
    ...
  end

  def destroy
    ...
  end

private
  def order_item_params
    ...
  end
end

and

products_controller

  def index
    ...
  end

& order, order_item, order_status and product models.

They all work with the database and I am able to successfully add, subtract to the shopping cart or clear the shopping cart by clicking a button.

But in both implementations site navigation inside the shop catalogue changes the shopping cart. So when adding a product on the detail page and navigating back to the catalogue page through the back button of the browser, the navigation goes back to the catalogue page, but the product is removed from the cart too.

How can I implement a shopping cart which does not change when navigating inside the shop?

Aucun commentaire:

Enregistrer un commentaire