[Ruby] Creating an EC site with Rails 5 ①-App configuration, various gem preparations, Model/Routing creation-

3 minute read

Introduction

The other day, I had an issue of team implementation at a programming school and created an EC site. I would like to make a review of what kind of code works except for the part I was in charge of.

I was wondering what kind of shop’s EC site to use, but I decided to call it a fictitious bakery because there were many photos of bread in the Google Photos album.

Source code

https://github.com/Sn16799/bakeryFUMIZUKI

Environment

Rails 5.2.4.2 Ruby 2.7.1 Centos7

App overview

・EC site (user can shop on site) ・Create user site and administrator site ・On the user site, after registering as a member and logging in, put the product in the cart and perform the order procedure. ・On the administrator site, change the order status (waiting for payment, manufacturing, …) and manufacturing status (cannot start, manufacturing, …) of ordered products and manage products and members

Launch app

$ rails new fumizuki # Since it is July, the store name (fictitious) is "Bakery Bunetsu".
$ cd fumizuki

Add the following to #Gemfile

# Login function
gem'devise'

# view decoration
gem'bootstrap'
gem'jquery-rails'

# Post image
gem'refile', require: "refile/rails", github:'manfe/refile'
gem'refile-mini_magick'

# Environment variable management
gem'dotenv-rails', require:'dotenv/rails-now'

# Pager
gem'kaminari','~> 1.1.1'
gem'nokogiri', '1.10.9'

# Debug
gem'pry-rails'

Once installed

$ rails g devise:install

Model creation

The structure of the DB is shown below. I also want to create my own administrator site without using gem. When using gem, active_admin was convenient. association.jpg (Steady work)

$ rails g devise Admin email:string
$ rails g devise Customer is_active:boolean first_name:string first_name_kana:string family_name:string family_name_kana:string post_code:string address:string tel:string email:string
$ rails g model Address customer_id:integer post_code:string addressee:string address:string
$ rails g model CartItem product_id:integer customer_id:integer quantity:integer
$ rails g model Genre name:string validity:boolean
$ rails g model OrderItem product_id:integer order_id:integer quantity:integer order_price:integer make_status:integer
$ rails g model Order customer_id:integer addressee:string post_code:string send_to_address:string how_to_pay:boolean deliver_fee:integer order_status:integer
$ rails g model Product genre_id:integer name:string introduction:text status:boolean image_id:string price:integer

# Once you make it, migrate
$ rails db:migrate

Routing

For the Routing, prepare a temporary one.

config/routes.rb


Rails.application.routes.draw do

# root path
root'homes#top'

# Customer site routing
devise_for :customers, controllers: {
registrations:'customers/registrations',
passwords:'customers/passwords',
sessions:'customers/sessions'}

get'homes/top' =>'homes#top', as:'customer_top'
get'homes/about' =>'homes#about', as:'customer_about'
resources :customers, only: [:edit, :show, :update]
  get'customers/:id/withdraw' =>'customers#withdraw', as:'customer_withdraw'
  patch'customers/:id/withdraw' =>'customers#withdraw_done', as:'customer_withdraw_done'
  put "/customers/:id/withdraw" => "customers#withdraw_done", as:'customers_withdraw_done'
resources :orders, only: [:new, :index, :create, :show]
  post'orders/confirm' =>'orders#confirm', as:'order_confirm'
  get'orders/thanks' =>'orders#thanks', as:'order_thanks'
resources :products, only: [:index, :show]
resources :order_items, only: [:index, :create, :new]
resources :addresses, only: [:index, :create, :edit, :update, :destroy]
resources :genres, only: [:show]

# Add all cart items for delete method
resources :cart_items, only: [:index, :create, :update, :destroy] do
collection do
delete'destroy_all'
end
end

# Admin site routing
devise_scope :admins do
devise_for :admins, controllers: {
registrations:'admins/registrations',
passwords:'admins/passwords',
sessions:'admins/sessions'
}
end

namespace :admins do
get'homes/top' =>'homes#top', as:'top'
resources :customers, only: [:index, :edit, :show, :update]
resources :products, only: [:index, :create, :new, :edit, :show, :update]
resources :orders, only: [:index, :create, :show, :update]
resources :order_items, only: [:index, :create, :show, :update]
resources :genres, only: [:index, :create, :edit, :update]
get'search' =>'searches#search', as:'search'
end

end

Postscript

For the time being, I made only Model and Routing. It seems that there is still a long way to go before you write actions and move like web apps. Even so, the amount of work would be extraordinary if one person made what was created by three people when implementing the team. However, the amount that I thought was quite difficult at the time, and now I feel that it is not that expensive. I made this app for the first time 3 months ago, but I may have grown during that time.

After this, I would like to align the Controller and the View. Continue to Next!

Reference

How to use active_admin (though not used in this article) Create a management screen with the fastest Rails!