Rails 6.0 scaffoldで試すための試し
今後、機能などを確かめるためや、テスト用のアプリケーションをscaffoldで作るためと、VSCodeでのデプロイをもう少し試すためにrails tutorialの第2章をやりまとめメモ
Scaffoldでのアプリケーション作成の手順
①rails new
$ rails new toy_app
※gemファイルの編集
sqlite3→移動
group :development, :test do
# Use sqlite3 as the database for Active Record
gem 'sqlite3', '~> 1.4'
pg →作成
group :production do
gem 'pg', '~> 0.18.4'
end
$ bundle install --without production
②git init
$ git init $ git add -A $ git commit -m "Initialize repository"
③リポジトリの作成
bitbucketでリポジトリを作成し生成したファイルをリポジトリにプッシュ
④テスト用アクションとルートの設定
class ApplicationController < ActionController::Base protect_from_forgery with: :exception def hello render html: "hello, world!" end end
Rails.application.routes.draw do root 'application#hello' end
⑤heroku createしherokuにプッシュ
$ git commit -am "Add hello" $ heroku create $ git push heroku master
⑥ユーザーリソースをscaffoldで作成
$ rails generate scaffold User name:string email:string
$ rails db:migrate
⑦ポストリソースをscaffoldで作成
$ rails generate scaffold Micropost content:text user_id:integer
$ rails db:migrate
⑧諸所設定
※ユーザーとポストの紐づけ
※バリデーション
※ルーティングなどの調整
⑨プッシュ
$ git status $ git add -A $ git commit -m "Finish toy app" $ git push
$ git push heroku
$ heroku run rails db:migrate
※忘れずに
以上で第2章のまとめ完了!
同じ要領で機能テスト、アプリケーションテストしていこう!
参照させていただいたサイト
第2章 Toyアプリケーション - Railsチュートリアル