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

3 minute read


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



Rails 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

# view decoration

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

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

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

# Debug

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


For the Routing, prepare a temporary one.


Rails.application.routes.draw do

# root path

# Customer site routing
devise_for :customers, controllers: {

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

# Admin site routing
devise_scope :admins do
devise_for :admins, controllers: {

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'



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!


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