自分の手で未来を創るーlav0

自分のために、誰かのために、今ここにないもの、もっと良くしたいもの、何でも自分の手で創っていく。そして、作ったものを公開していきます

Rails 6.0 scaffoldで試すための試し

今後、機能などを確かめるためや、テスト用のアプリケーションをscaffoldで作るためと、VSCodeでのデプロイをもう少し試すためにrails tutorialの第2章をやりまとめメモ

f:id:kslabo51:20200131205932j:plain

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チュートリアル