これは何?
下記のような人に有効な、基盤を作るためのレシピです。
- gemはlocalでプロジェクト毎に使い分けたい
- githubで基本的に管理したい
- turbolinksは不明なエラーを招くので回避したい
- envでバージョン管理したい
- dbはmysqlを利用したい
- デザインにはbootstrapを利用したい
※ 至らない点はご指摘願います。
前提
- rbenv
- bundler
- ターミナルでの作業です。
実行環境
- ruby --version
- 2.4.0
- rbenv --version
- 1.1.1
- macOS High Sierra
- 10.13.4
- bundler --version
- 1.16.3
Gem環境整備
- bundle init
source "https://rubygems.org"
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
gem "rails"
gem "rspec"
gem "dotenv-rails"
gem "bootsnap"
gem "listen"
gem "mysql2"
gem "bootstrap-sass"
gem "jquery-rails"
gem "jquery-ui-rails"
- bundle install --path=vendor/bundle
- 次回以降はbundle installでOK
Git環境整備
- git init
- git remote add origin {{レポジトリ}}
- vim .gitignore
# Ignore bundler config.
/.bundle
/vendor/bundle
# Ignore log and temp.
/log/*
/tmp/*
!/log/.keep
!/tmp/.keep
# Ignore ENV.
.env*
!.env.sample
# Ignore master key for decrypting credentials and more.
/config/master.key
Rails整備
- rails new . -d mysql -T -G -skip-turbolinks
- mv app/assets/stylesheets/application.css app/assets/stylesheets/application.scss
- vim app/assets/stylesheets/application.scss
@import "bootstrap-sprockets";
@import "bootstrap";
- vim app/assets/javascripts/application.js
▼ 前
//= require rails-ujs
//= require activestorage
//= require turbolinks
//= require_tree .
▼ 修正後
//= require rails-ujs
//= require activestorage
//= require jquery3
//= require bootstrap-sprockets
//= require_tree .
ENV整備
- touch .env.sample
- vim .env.sample
DATABASE_NAME=
DATABASE_USERNAME=
DATABASE_PASSWORD=
- cp .env.sample .env
- .envに環境情報記載
DATABASE整備
- vim config/database.yml
- username: <%= ENV['DATABASE_USERNAME'] %>
- password: <%= ENV['DATABASE_PASSWORD'] %>
- database: <%= ENV['DATABASE_NAME'] %>
- bundle exec rails db:create
- bundle exec rails db:migrate
実行
- bundle exec rails server
- bundle exec rails generate controller ~~
- bundle exec rails generate model ~~
- bundle exec rails generate migration ~~
ファイル