Creating an EC site with Rails5 ①-App configuration, various gem preparation, Model / Routing creation-

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 other than the one I was in charge of.

I was wondering what kind of store the EC site should be, but since there were many pictures of bread in the Google Photos album, I decided to call it a fictitious bakery.

Source code

https://github.com/Sn16799/bakeryFUMIZUKI

environment

Rails 5.2.4.2 Ruby 2.7.1 Centos7

App overview

・ EC site (users can shop on the 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 complete the order procedure. ・ On the administrator site, change the order status (waiting for payment, in production, ...) and production status (cannot start, in production, ...) of the ordered product, and manage the product and members.

Launch the app

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

Add the following to the Gemfile

#Login function
gem 'devise'

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

#Image posting
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 DB configuration is as shown in the figure below. I would like to create my own administrator site without using gems. 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 Prepare a temporary routing as well.

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

#Administrator 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 until we write an action and make it behave like a web application. Even so, if you try to make something that was made by three people when implementing a team, the amount of work is unusual. However, the amount that I thought was quite difficult at that time is not so high when I look at it now. I made this app for the first time three months ago, but I may have grown up in the meantime.

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

reference

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

Recommended Posts

Creating an EC site with Rails5 ①-App configuration, various gem preparation, Model / Routing creation-
Create an EC site with Rails5 ⑤ ~ Customer model ~
Create an EC site with Rails5 ⑦ ~ Address, Genre model ~
Create an EC site with Rails5 ③-Set model associations and other things-
Create an EC site with Rails 5 ⑩ ~ Create an order function ~
Create an EC site with Rails 5 ⑨ ~ Create a cart function ~
Create an EC site with Rails5 ④ ~ Header and footer ~
Create an EC site with Rails5 ⑥ ~ seed data input ~
Rough procedure verbalized output when creating an app with Rails
Summary of initial work when creating an app with Rails
How to specify db when creating an app with rails
Create an EC site with Rails5 ② ~ Bootstrap4 settings, Controller / action definition ~
Downgrade an existing app created with rails 5.2.4 to 5.1.6
Create an EC site using stripe! (Account creation)
Create an EC site with Rails5 ③-Set model associations and other things-
Create an EC site with Rails5 ⑤ ~ Customer model ~
Create an EC site with Rails5 ⑦ ~ Address, Genre model ~
Create an EC site with Rails5 ④ ~ Header and footer ~
Creating an EC site with Rails5 ①-App configuration, various gem preparation, Model / Routing creation-
Building an environment for creating apps with Rails and Vue
Create an EC site with Rails 5 ⑩ ~ Create an order function ~
Create an EC site with Rails 5 ⑨ ~ Create a cart function ~
Create an EC site with Rails5 ⑥ ~ seed data input ~
Create an EC site with Rails5 ② ~ Bootstrap4 settings, Controller / action definition ~
How to push an app developed with Rails to Github
How to get started with creating a Rails app