[RUBY] [Rails] EC site cart function

Introduction

I created an EC site with Ruby on Rails. At that time, I spent a lot of time implementing the cart function, so I will describe how to do it.

Prerequisites

In order to have the cart function, a table is required in the DB. The association with other tables is described in the following article. [Rails] DB design for EC site

The basic part of the cart function

Here we will create a session.

For sessions, the following articles are easy to understand. [Rails] How to use Session

In short, it's a mechanism that retains data. So, I coded the session creation for this cart as follows:

# application_controller.rb
class ApplicationController < ActionController::Base

private
  #Creating a session
  def current_cart
    #Cart obtained from session_Get Cart information from Cart table based on id
    current_cart = Cart.find_by(id: session[:cart_id])
    #If Cart information does not exist@current_Create cart
    current_cart = Cart.create unless current_cart
    #Obtain the ID from the obtained Cart information and set it in the session
    session[:cart_id] = current_cart.id
    #Return Cart information
    current_cart
  end
end

The "current_cart" defined here will be used for other controllers as well.

Add products to the cart

From here, we will use the method created earlier in carts_controller.

class CartsController < ApplicationController
  before_action :set_line_item, only: [:add_item, :destroy]
  before_action :set_user
  before_action :set_cart

  def show
    @line_items = @cart.line_items
  end

  def add_item
    @line_item = @cart.line_items.build(product_id: params[:product_id]) if @line_item.blank?
    @line_item.quantity += params[:quantity].to_i
    if @line_item.save
      redirect_to current_cart
    else
      redirect_to controller: "products", action: "show"
    end
  end

  def destroy
    @cart.destroy
    redirect_to current_cart
  end

  private
  def set_user
    @user = current_user
  end

  def set_line_item
    @line_item = current_cart.line_items.find_by(product_id: params[:product_id])
  end

  def set_cart
    @cart = current_cart
  end
end

Regarding the add_item action, the "Add to cart" button is installed on the product detail page, and the product is added to the cart using the HTTP method of POST.

@line_item = @cart.line_items.build(product_id: params[:product_id]) if @line_item.blank?

Above, I'm using the build method. build is an alias of new, but it seems that when creating an object of the associated model by convention, build is used and new is used in other cases.

This time, cart is the parent and line_item is the child, so we used build to create an object for the line_item model. (Excuse me if I am wrong)

Basically, I think that it is functioning as a cart so far.

By the way

You can create an EC site in seconds by using a gem called solidus.

Reference article ↓ Installation memo of Solidus, the successor EC system of Spree

I tried using it, but it seems that customization is slow. It may be an ant to try it depending on the purpose.

Recommended Posts

[Rails] EC site cart function
Create an EC site with Rails 5 ⑨ ~ Create a cart function ~
Create an EC site with Rails 5 ⑩ ~ Create an order function ~
[Rails] DB design for EC site
[Rails 6] Ranking function
[Rails] Category function
Rails follow function
[Rails] Notification function
Create an EC site with Rails5 ⑤ ~ Customer model ~
Launch Rails on EC2
[Rails] Implement search function
[Rails] Implemented hashtag function
Deploy RAILS on EC2
Rails search function implementation
Create an EC site with Rails5 ⑦ ~ Address, Genre model ~
Create an EC site with Rails5 ④ ~ Header and footer ~
Create an EC site with Rails5 ⑥ ~ seed data input ~
Implement application function in Rails
Rails fuzzy search function implementation
[Rails] Implement User search function
Introduced graph function with rails
Search function using [rails] ransack
Implement follow function in Rails
[Rails] Implementation of category function
Rails ~ Understanding the message function ~
[Rails] (Supplement) Implemented follow function
Login function implementation with rails
Ajax bookmark function using Rails
[Rails] Implementation of tutorial function
[Rails] Implement image posting function
[Rails] Implementation of like function
[Rails 6] Pagination function implementation (kaminari)
Create an EC site with Rails5 ② ~ Bootstrap4 settings, Controller / action definition ~
[Rails] Implementation of CSV import function
Add a search function in Rails.
[Rails] About the Punk List function
[Ruby on Rails] Introduced paging function
[Rails] Implementation of image preview function
[Rails] Tag management function (using acts-as-taggable-on)
Implemented mail sending function with rails
Kaminari --Added pagination function of Rails
[Rails] About implementation of like function
[Rails] Implementation of user withdrawal function
[Rails] Implementation of CSV export function
Create pagination function with Rails Kaminari
Implement simple login function in Rails
[Rails] Voice posting function ~ Cloudinary, CarrierWave
[Rails] Comment function (registration / display / deletion)
Launch Rails on EC2 (manual deployment)
[Ruby on Rails] Comment function implementation
[Ruby on Rails] DM, chat function
[Rails 6] Like function (synchronous → asynchronous) implementation
Implement CSV download function in Rails
[Rails] Comment function implementation procedure memo
[EC2 / Vue / Rails] EC2 deployment procedure for Vue + Rails
[Rails] AWS EC2 instance environment construction
Create an EC site with Rails5 ③-Set model associations and other things-