見出し画像

railsチュートリアル挑戦記 第2章 Toyアプリケーション

railsチュートリアルをやりながらメモしたことをそのまま記述しています。

第2章Toyアプリケーション

大量の機能を自動的に生成するscaffoldジェネレータというスクリプトを使う
それを元に子ウッドなrailsプログラミングとWebプログラミングの概要を学ぶ
scaffoldはこの章でのみ取り扱う
RESTアーキテクチャについて考察する


・2 1 アプリケーションの計画

新しいアプリケーションを作成する
cd ~/environment
rails _5.1.6_ new toy_app
cd toy_app/

Gemfileを書き換える
bundle install

ログ
----------------------------
The dependency tzinfo-data (>= 0) will be unused by any of the platforms Bundler is installing for. Bundler is installing for ruby but the dependency is only for x86-mingw32, x86-mswin32, x64-mingw32, java. To add those platforms to the bundle, run `bundle lock --add-platform x86-mingw32 x86-mswin32 x64-mingw32 java`.
Fetching gem metadata from https://rubygems.org/............
Fetching gem metadata from https://rubygems.org/.
You have requested:
 spring = 2.0.2

The bundle currently has spring locked at 2.1.0.
Try running `bundle update spring`

If you are updating multiple gems in your Gemfile at once,
try passing them all to `bundle update`

やはりエラーが出たか
というかbundle install --without productionと打たなければならなかった
まあいいや
bundle updateを先にやるか

ログ
----------------------------
The dependency tzinfo-data (>= 0) will be unused by any of the platforms Bundler is installing for. Bundler is installing for ruby but the dependency is only for x86-mingw32, x86-mswin32, x64-mingw32, java. To add those platforms to the bundle, run `bundle lock --add-platform x86-mingw32 x86-mswin32 x64-mingw32 java`.
Fetching gem metadata from https://rubygems.org/............
Fetching gem metadata from https://rubygems.org/.
Resolving dependencies....
Using rake 13.0.1
Using concurrent-ruby 1.1.5
Using i18n 1.7.0
Using minitest 5.13.0
Using thread_safe 0.3.6
Using tzinfo 1.2.6
Using activesupport 5.1.6 (was 5.1.7)
Using builder 3.2.4
Using erubi 1.9.0
Using mini_portile2 2.4.0
Using nokogiri 1.10.7
Using rails-dom-testing 2.0.3
Using crass 1.0.5
Using loofah 2.4.0
Using rails-html-sanitizer 1.3.0
Using actionview 5.1.6 (was 5.1.7)
Using rack 2.0.8
Using rack-test 1.1.0
Using actionpack 5.1.6 (was 5.1.7)
Using nio4r 2.5.2
Using websocket-extensions 0.1.4
Using websocket-driver 0.6.5
Using actioncable 5.1.6 (was 5.1.7)
Using globalid 0.4.2
Using activejob 5.1.6 (was 5.1.7)
Using mini_mime 1.0.2
Using mail 2.7.1
Using actionmailer 5.1.6 (was 5.1.7)
Using activemodel 5.1.6 (was 5.1.7)
Using arel 8.0.0
Using activerecord 5.1.6 (was 5.1.7)
Using bindex 0.8.1
Using bundler 1.17.3
Using byebug 9.0.6 (was 11.0.1)
Using coffee-script-source 1.12.2
Using execjs 2.7.0
Using coffee-script 2.4.1
Using method_source 0.9.2
Using thor 1.0.1
Using railties 5.1.6 (was 5.1.7)
Using coffee-rails 4.2.2
Using ffi 1.11.3
Using multi_json 1.14.1
Fetching jbuilder 2.7.0 (was 2.9.1)
Installing jbuilder 2.7.0 (was 2.9.1)
Using jquery-rails 4.3.1
Using rb-fsevent 0.10.3
Using rb-inotify 0.10.1
Using ruby_dep 1.5.0
Using listen 3.1.5
Fetching pg 0.20.0
Installing pg 0.20.0 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

current directory: /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/pg-0.20.0/ext
/home/ec2-user/.rvm/rubies/ruby-2.6.3/bin/ruby -I
/home/ec2-user/.rvm/rubies/ruby-2.6.3/lib/ruby/site_ruby/2.6.0 -r
./siteconf20191228-6444-1x5cnh9.rb extconf.rb
checking for pg_config... no
No pg_config... trying anyway. If building fails, please try again with
--with-pg-config=/path/to/pg_config
checking for libpq-fe.h... no
Can't find the 'libpq-fe.h header
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

Provided configuration options:
   --with-opt-dir
   --without-opt-dir
   --with-opt-include
   --without-opt-include=${opt-dir}/include
   --with-opt-lib
   --without-opt-lib=${opt-dir}/lib
   --with-make-prog
   --without-make-prog
   --srcdir=.
   --curdir
   --ruby=/home/ec2-user/.rvm/rubies/ruby-2.6.3/bin/$(RUBY_BASE_NAME)
   --with-pg
   --without-pg
   --enable-windows-cross
   --disable-windows-cross
   --with-pg-config
   --without-pg-config
   --with-pg_config
   --without-pg_config
   --with-pg-dir
   --without-pg-dir
   --with-pg-include
   --without-pg-include=${pg-dir}/include
   --with-pg-lib
   --without-pg-lib=${pg-dir}/lib

To see why this extension failed to compile, please check the mkmf.log which can be found
here:

/home/ec2-user/.rvm/gems/ruby-2.6.3/extensions/x86_64-linux/2.6.0/pg-0.20.0/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/pg-0.20.0 for
inspection.
Results logged to
/home/ec2-user/.rvm/gems/ruby-2.6.3/extensions/x86_64-linux/2.6.0/pg-0.20.0/gem_make.out

An error occurred while installing pg (0.20.0), and Bundler cannot continue.
Make sure that `gem install pg -v '0.20.0' --source 'https://rubygems.org/'` succeeds
before bundling.

In Gemfile:
pg

ん?!
なんかエラーが出ておる

手順ミスったかな?
toy_appを削除してもう一度下記手順

cd ~/environment
rails _5.1.6_ new toy_app
cd ~/environment/toy_app

ここで手順を変更して、一度Gemfileを書き換えずにbundleupdateを実行
Bundle updated!と出て成功

このあとで、Gemfileを修正するという手順に戻る

修正内容
----------------------------
source 'https://rubygems.org'

gem 'rails',        '5.1.6'
gem 'puma',         '3.9.1'
gem 'sass-rails',   '5.0.6'
gem 'uglifier',     '3.2.0'
gem 'coffee-rails', '4.2.2'
gem 'jquery-rails', '4.3.1'
gem 'turbolinks',   '5.0.1'
gem 'jbuilder',     '2.7.0'

group :development, :test do
 gem 'sqlite3', '1.3.13'
 gem 'byebug',  '9.0.6', platform: :mri
end

group :development do
 gem 'web-console',           '3.5.1'
 gem 'listen',                '3.1.5'
 gem 'spring',                '2.0.2'
 gem 'spring-watcher-listen', '2.0.1'
end

group :production do
 gem 'pg', '0.20.0'
end

# Windows環境ではtzinfo-dataというgemを含める必要があります
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

ここで改めてbundle update

ログ
----------------------------
The dependency tzinfo-data (>= 0) will be unused by any of the platforms Bundler is installing for. Bundler is installing for ruby but the dependency is only for x86-mingw32, x86-mswin32, x64-mingw32, java. To add those platforms to the bundle, run `bundle lock --add-platform x86-mingw32 x86-mswin32 x64-mingw32 java`.
Fetching gem metadata from https://rubygems.org/............
Fetching gem metadata from https://rubygems.org/.
Resolving dependencies....
Using rake 13.0.1
Using concurrent-ruby 1.1.5
Using i18n 1.7.0
Using minitest 5.13.0
Using thread_safe 0.3.6
Using tzinfo 1.2.6
Using activesupport 5.1.6 (was 5.1.7)
Using builder 3.2.4
Using erubi 1.9.0
Using mini_portile2 2.4.0
Using nokogiri 1.10.7
Using rails-dom-testing 2.0.3
Using crass 1.0.5
Using loofah 2.4.0
Using rails-html-sanitizer 1.3.0
Using actionview 5.1.6 (was 5.1.7)
Using rack 2.0.8
Using rack-test 1.1.0
Using actionpack 5.1.6 (was 5.1.7)
Using nio4r 2.5.2
Using websocket-extensions 0.1.4
Using websocket-driver 0.6.5
Using actioncable 5.1.6 (was 5.1.7)
Using globalid 0.4.2
Using activejob 5.1.6 (was 5.1.7)
Using mini_mime 1.0.2
Using mail 2.7.1
Using actionmailer 5.1.6 (was 5.1.7)
Using activemodel 5.1.6 (was 5.1.7)
Using arel 8.0.0
Using activerecord 5.1.6 (was 5.1.7)
Using bindex 0.8.1
Using bundler 1.17.3
Using byebug 9.0.6 (was 11.0.1)
Using coffee-script-source 1.12.2
Using execjs 2.7.0
Using coffee-script 2.4.1
Using method_source 0.9.2
Using thor 1.0.1
Using railties 5.1.6 (was 5.1.7)
Using coffee-rails 4.2.2
Using ffi 1.11.3
Using multi_json 1.14.1
Using jbuilder 2.7.0 (was 2.9.1)
Using jquery-rails 4.3.1
Using rb-fsevent 0.10.3
Using rb-inotify 0.10.1
Using ruby_dep 1.5.0
Using listen 3.1.5
Fetching pg 0.20.0
Installing pg 0.20.0 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

   current directory: /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/pg-0.20.0/ext
/home/ec2-user/.rvm/rubies/ruby-2.6.3/bin/ruby -I
/home/ec2-user/.rvm/rubies/ruby-2.6.3/lib/ruby/site_ruby/2.6.0 -r
./siteconf20191228-6904-62wk0.rb extconf.rb
checking for pg_config... no
No pg_config... trying anyway. If building fails, please try again with
--with-pg-config=/path/to/pg_config
checking for libpq-fe.h... no
Can't find the 'libpq-fe.h header
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

Provided configuration options:
       --with-opt-dir
       --without-opt-dir
       --with-opt-include
       --without-opt-include=${opt-dir}/include
       --with-opt-lib
       --without-opt-lib=${opt-dir}/lib
       --with-make-prog
       --without-make-prog
       --srcdir=.
       --curdir
       --ruby=/home/ec2-user/.rvm/rubies/ruby-2.6.3/bin/$(RUBY_BASE_NAME)
       --with-pg
       --without-pg
       --enable-windows-cross
       --disable-windows-cross
       --with-pg-config
       --without-pg-config
       --with-pg_config
       --without-pg_config
       --with-pg-dir
       --without-pg-dir
       --with-pg-include
       --without-pg-include=${pg-dir}/include
       --with-pg-lib
       --without-pg-lib=${pg-dir}/lib

To see why this extension failed to compile, please check the mkmf.log which can be found
here:

 /home/ec2-user/.rvm/gems/ruby-2.6.3/extensions/x86_64-linux/2.6.0/pg-0.20.0/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/pg-0.20.0 for
inspection.
Results logged to
/home/ec2-user/.rvm/gems/ruby-2.6.3/extensions/x86_64-linux/2.6.0/pg-0.20.0/gem_make.out

An error occurred while installing pg (0.20.0), and Bundler cannot continue.
Make sure that `gem install pg -v '0.20.0' --source 'https://rubygems.org/'` succeeds
before bundling.

In Gemfile:

1章の時は出なかったんだけどな
ちなみに1章の時とGemfileが違った
具体的には
1章 gem 'jbuilder', '2.6.4'
2章 gem 'jbuilder', '2.7.0'
になってた
でもこれが原因ではないっぽい
エラーは同じものが出続ける
ちなみにきちんとGemfileはセーブしているし、きちんとToy_appのGemfileを開いたことを確認済み

https://qiita.com/chabudai/items/1463a6c6155b380bcc02
の記事を見つけたので、ここの手順をテスト

とりあえずbundle updateを何回やってもエラーになったので
bundle update springを実行

ログ
----------------------------
The dependency tzinfo-data (>= 0) will be unused by any of the platforms Bundler is installing for. Bundler is installing for ruby but the dependency is only for x86-mingw32, x86-mswin32, x64-mingw32, java. To add those platforms to the bundle, run `bundle lock --add-platform x86-mingw32 x86-mswin32 x64-mingw32 java`.
Fetching gem metadata from https://rubygems.org/............
Fetching gem metadata from https://rubygems.org/.
Resolving dependencies...
Bundler could not find compatible versions for gem "activesupport":
In snapshot (Gemfile.lock):
activesupport (= 5.1.7)

In Gemfile:
rails (= 5.1.6) was resolved to 5.1.6, which depends on
 activesupport (= 5.1.6)

coffee-rails (= 4.2.2) was resolved to 4.2.2, which depends on
 railties (>= 4.0.0) was resolved to 5.1.7, which depends on
   activesupport (= 5.1.7)

Running `bundle update` will rebuild your snapshot from scratch, using only
the gems in your Gemfile, which may resolve the conflict.

更にbundle update minitestを実行

ログ
----------------------------
The dependency tzinfo-data (>= 0) will be unused by any of the platforms Bundler is installing for. Bundler is installing for ruby but the dependency is only for x86-mingw32, x86-mswin32, x64-mingw32, java. To add those platforms to the bundle, run `bundle lock --add-platform x86-mingw32 x86-mswin32 x64-mingw32 java`.
Fetching gem metadata from https://rubygems.org/............
Fetching gem metadata from https://rubygems.org/.
You have requested:
spring = 2.0.2

The bundle currently has spring locked at 2.1.0.
Try running `bundle update spring`

If you are updating multiple gems in your Gemfile at once,
try passing them all to `bundle update`

お?と思い、再度bundle update springを実行

ログ
----------------------------
The dependency tzinfo-data (>= 0) will be unused by any of the platforms Bundler is installing for. Bundler is installing for ruby but the dependency is only for x86-mingw32, x86-mswin32, x64-mingw32, java. To add those platforms to the bundle, run `bundle lock --add-platform x86-mingw32 x86-mswin32 x64-mingw32 java`.
Fetching gem metadata from https://rubygems.org/............
Fetching gem metadata from https://rubygems.org/.
Resolving dependencies...
Bundler could not find compatible versions for gem "activesupport":
In snapshot (Gemfile.lock):
activesupport (= 5.1.7)

In Gemfile:
rails (= 5.1.6) was resolved to 5.1.6, which depends on
 activesupport (= 5.1.6)

coffee-rails (= 4.2.2) was resolved to 4.2.2, which depends on
 railties (>= 4.0.0) was resolved to 5.1.7, which depends on
   activesupport (= 5.1.7)

Running `bundle update` will rebuild your snapshot from scratch, using only
the gems in your Gemfile, which may resolve the conflict.

ダメか
ダメ元で以下のコマンドをなんとなく実行
bundle update pg

Could not find gem 'pg'.

えらーみじかっ!
クラウドにpgがないよって言われてるな

bundle install --without productionを実行

ログ
----------------------------
The dependency tzinfo-data (>= 0) will be unused by any of the platforms Bundler is installing for. Bundler is installing for ruby but the dependency is only for x86-mingw32, x86-mswin32, x64-mingw32, java. To add those platforms to the bundle, run `bundle lock --add-platform x86-mingw32 x86-mswin32 x64-mingw32 java`.
Fetching gem metadata from https://rubygems.org/............
Fetching gem metadata from https://rubygems.org/.
You have requested:
spring = 2.0.2

The bundle currently has spring locked at 2.1.0.
Try running `bundle update spring`

If you are updating multiple gems in your Gemfile at once,
try passing them all to `bundle update`

ここでbundle update springを実行

ログ
----------------------------
The dependency tzinfo-data (>= 0) will be unused by any of the platforms Bundler is installing for. Bundler is installing for ruby but the dependency is only for x86-mingw32, x86-mswin32, x64-mingw32, java. To add those platforms to the bundle, run `bundle lock --add-platform x86-mingw32 x86-mswin32 x64-mingw32 java`.
Fetching gem metadata from https://rubygems.org/............
Fetching gem metadata from https://rubygems.org/.
Resolving dependencies...
Bundler could not find compatible versions for gem "activesupport":
In snapshot (Gemfile.lock):
activesupport (= 5.1.7)

In Gemfile:
rails (= 5.1.6) was resolved to 5.1.6, which depends on
 activesupport (= 5.1.6)

coffee-rails (= 4.2.2) was resolved to 4.2.2, which depends on
 railties (>= 4.0.0) was resolved to 5.1.7, which depends on
   activesupport (= 5.1.7)

Running `bundle update` will rebuild your snapshot from scratch, using only
the gems in your Gemfile, which may resolve the conflict.

んー

はい、わかりました
解決方法は以下です
toy_appを削除
environmentにパスを変更
toy_appを作成
Gemfileをrailsチュートリアルに従って書き換え
ポイント①Gemfileをきちんとsave
ポイント②bundle install --without production
ポイント③bundle update

ポイント①~③を「1ミリも間違えることなく」「順番を間違えずにやること」
1度でもコマンドミスしているなら上手く行かないっぽい(多分)
これを守れていなかったからエラーが取り除けなかったのだと思います(多分)
最終的なログは以下です。

ログ
----------------------------
The dependency tzinfo-data (>= 0) will be unused by any of the platforms Bundler is installing for. Bundler is installing for ruby but the dependency is only for x86-mingw32, x86-mswin32, x64-mingw32, java. To add those platforms to the bundle, run `bundle lock --add-platform x86-mingw32 x86-mswin32 x64-mingw32 java`.
Fetching gem metadata from https://rubygems.org/............
Fetching gem metadata from https://rubygems.org/.
Resolving dependencies....
Using rake 13.0.1
Using concurrent-ruby 1.1.5
Using i18n 1.7.0
Using minitest 5.13.0
Using thread_safe 0.3.6
Using tzinfo 1.2.6
Using activesupport 5.1.6 (was 5.1.7)
Using builder 3.2.4
Using erubi 1.9.0
Using mini_portile2 2.4.0
Using nokogiri 1.10.7
Using rails-dom-testing 2.0.3
Using crass 1.0.5
Using loofah 2.4.0
Using rails-html-sanitizer 1.3.0
Using actionview 5.1.6 (was 5.1.7)
Using rack 2.0.8
Using rack-test 1.1.0
Using actionpack 5.1.6 (was 5.1.7)
Using nio4r 2.5.2
Using websocket-extensions 0.1.4
Using websocket-driver 0.6.5
Using actioncable 5.1.6 (was 5.1.7)
Using globalid 0.4.2
Using activejob 5.1.6 (was 5.1.7)
Using mini_mime 1.0.2
Using mail 2.7.1
Using actionmailer 5.1.6 (was 5.1.7)
Using activemodel 5.1.6 (was 5.1.7)
Using arel 8.0.0
Using activerecord 5.1.6 (was 5.1.7)
Using bindex 0.8.1
Using bundler 1.17.3
Using byebug 9.0.6 (was 11.0.1)
Using coffee-script-source 1.12.2
Using execjs 2.7.0
Using coffee-script 2.4.1
Using method_source 0.9.2
Using thor 1.0.1
Using railties 5.1.6 (was 5.1.7)
Using coffee-rails 4.2.2
Using ffi 1.11.3
Using multi_json 1.14.1
Using jbuilder 2.7.0 (was 2.9.1)
Using jquery-rails 4.3.1
Using rb-fsevent 0.10.3
Using rb-inotify 0.10.1
Using ruby_dep 1.5.0
Using listen 3.1.5
Using puma 3.9.1 (was 3.12.2)
Using sprockets 3.7.2
Using sprockets-rails 3.2.1
Using rails 5.1.6 (was 5.1.7)
Using sass-listen 4.0.0
Using sass 3.7.4
Using tilt 2.0.10
Using sass-rails 5.0.6 (was 5.0.7)
Using spring 2.0.2 (was 2.1.0)
Using spring-watcher-listen 2.0.1
Using sqlite3 1.3.13 (was 1.4.2)
Using turbolinks-source 5.2.0
Using turbolinks 5.0.1 (was 5.2.1)
Using uglifier 3.2.0 (was 4.2.0)
Using web-console 3.5.1 (was 3.7.0)
Bundle updated!
Gems in the group production were not installed.

勘が当たった!うまくいった!
イイネ!
ただし「何故?」という部分が十分に理解できておらずマズいので、これは課題にする。
とりあえずbundle update 〇〇で個別にFetching(取り出し?)できるというのが分かったのでこのあたりの知識が不足しているのはなんとなくわかる。たぶんだけど。
今は先に進める。

次はgitをバージョン管理下に置く
git init
git add -A
git commit -m "Initialize repository"

ログ
----------------------------
ec2-user:~/environment/toy_app (master) $ git init
Reinitialized existing Git repository in /home/ec2-user/environment/toy_app/.git/
ec2-user:~/environment/toy_app (master) $ git add -A
ec2-user:~/environment/toy_app (master) $ git commit -m "Initialize repository"
[master (root-commit) 01289bf] Initialize repository
76 files changed, 1160 insertions(+)
create mode 100644 .gitignore
create mode 100644 Gemfile
create mode 100644 Gemfile.lock
create mode 100644 README.md
create mode 100644 Rakefile
create mode 100644 app/assets/config/manifest.js
create mode 100644 app/assets/images/.keep
create mode 100644 app/assets/javascripts/application.js
create mode 100644 app/assets/javascripts/cable.js
create mode 100644 app/assets/javascripts/channels/.keep
create mode 100644 app/assets/stylesheets/application.css
create mode 100644 app/channels/application_cable/channel.rb
create mode 100644 app/channels/application_cable/connection.rb
create mode 100644 app/controllers/application_controller.rb
create mode 100644 app/controllers/concerns/.keep
create mode 100644 app/helpers/application_helper.rb
create mode 100644 app/jobs/application_job.rb
create mode 100644 app/mailers/application_mailer.rb
create mode 100644 app/models/application_record.rb
create mode 100644 app/models/concerns/.keep
create mode 100644 app/views/layouts/application.html.erb
create mode 100644 app/views/layouts/mailer.html.erb
create mode 100644 app/views/layouts/mailer.text.erb
create mode 100755 bin/bundle
create mode 100755 bin/rails
create mode 100755 bin/rake
create mode 100755 bin/setup
create mode 100755 bin/spring
create mode 100755 bin/update
create mode 100755 bin/yarn
create mode 100644 config.ru
create mode 100644 config/application.rb
create mode 100644 config/boot.rb
create mode 100644 config/cable.yml
create mode 100644 config/database.yml
create mode 100644 config/environment.rb
create mode 100644 config/environments/development.rb
create mode 100644 config/environments/production.rb
create mode 100644 config/environments/test.rb
create mode 100644 config/initializers/application_controller_renderer.rb
create mode 100644 config/initializers/assets.rb
create mode 100644 config/initializers/backtrace_silencers.rb
create mode 100644 config/initializers/cookies_serializer.rb
create mode 100644 config/initializers/filter_parameter_logging.rb
create mode 100644 config/initializers/inflections.rb
create mode 100644 config/initializers/mime_types.rb
create mode 100644 config/initializers/wrap_parameters.rb
create mode 100644 config/locales/en.yml
create mode 100644 config/puma.rb
create mode 100644 config/routes.rb
create mode 100644 config/secrets.yml
create mode 100644 config/spring.rb
create mode 100644 db/seeds.rb
create mode 100644 lib/assets/.keep
create mode 100644 lib/tasks/.keep
create mode 100644 log/.keep
create mode 100644 package.json
create mode 100644 public/404.html
create mode 100644 public/422.html
create mode 100644 public/500.html
create mode 100644 public/apple-touch-icon-precomposed.png
create mode 100644 public/apple-touch-icon.png
create mode 100644 public/favicon.ico
create mode 100644 public/robots.txt
create mode 100644 test/application_system_test_case.rb
create mode 100644 test/controllers/.keep
create mode 100644 test/fixtures/.keep
create mode 100644 test/fixtures/files/.keep
create mode 100644 test/helpers/.keep
create mode 100644 test/integration/.keep
create mode 100644 test/mailers/.keep
create mode 100644 test/models/.keep
create mode 100644 test/system/.keep
create mode 100644 test/test_helper.rb
create mode 100644 tmp/.keep
create mode 100644 vendor/.keep

よさそう

次はBitbucketの設定に移る
toy_appリポジトリを作成する

git remote add origin git@bitbucket.org:アカウント名/toy_app.git
git push -u origin --all

ここで重大なミス!
上のremoteのコマンドをコピペして変なところにremoteしてしまった
git remote -vでremote先を確認すると以下のようになる

origin  git@bitbucket.org:アカウント名/toy_app.git (fetch)
origin  git@bitbucket.org:アカウント名/toy_app.git (push)

こうなったらgit remoteを取り消さなければならない
以下のコマンドでOK
git remote rm origin

以下をやり直す
git init取り消しからやる
rm -rf .git
これはUnixコマンド

そしてgit init

ログ
----------------------------
Initialized empty Git repository in /home/ec2-user/environment/toy_app/.git/

再度git initしてみるテスト

ログ
----------------------------
Reinitialized existing Git repository in /home/ec2-user/environment/toy_app/.git/

追加
git add -A
追加内容を見る
git status
コミット
git commit -m "Initialize repository"
コミット履歴見る
git log
プッシュ
git push -u origin --all

・・・pushできない

ログ
----------------------------
o bitbucket.org:ナイショ/toy_app.git
! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'git@bitbucket.org:ナイショ/toy_app.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

ここにあるものを試してみる
https://www.softel.co.jp/blogs/tech/archives/3569

方法1 git pull

ログ
----------------------------
warning: no common commits
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From bitbucket.org:/toy_app
* [new branch]      master     -> origin/master
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.

git pull <remote> <branch>

If you wish to set tracking information for this branch you can do so with:

git branch --set-upstream-to=origin/<branch> master
----------------------------

pushしてもダメ

方法2
git fetch
git merge origin/master
pushしてもダメ

方法3
git fetch
git rebase origin/master

ログ
----------------------------
First, rewinding head to replay your work on top of it...
Applying: Initialize repository
Using index info to reconstruct a base tree...
Falling back to patching base and 3-way merge...
Auto-merging README.md
CONFLICT (add/add): Merge conflict in README.md
error: Failed to merge in the changes.
Patch failed at 0001 Initialize repository
The copy of the patch that failed is found in: .git/rebase-apply/patch

When you have resolved this problem, run "git rebase --continue".
If you prefer to skip this patch, run "git rebase --skip" instead.
To check out the original branch and stop rebasing, run "git rebase --abort".

git checkout -fやるとどうなるんだろう・・・?
※ここが悪夢の始まり
→toy_appの中身ほとんど消えたー!!!

泣く泣く最初からやり直す

ここまでやり直したが、pushコマンドでrejectされるまで同じ

以下のコマンドを試す
強制的push
git push -f origin master

ログ
----------------------------
Counting objects: 84, done.
Compressing objects: 100% (70/70), done.
Writing objects: 100% (84/84), 20.07 KiB | 1.11 MiB/s, done.
Total 84 (delta 2), reused 0 (delta 0)
To bitbucket.org:ナイショ/toy_app.git
+ ff76d9e...a7f641e master -> master (forced update)

これチーム開発でやったらバチクソ怒られそう
とりあえずpushできた!

ここまで成功させるための手順をまとめよう

手順
----------------------------
cd ~/environment (ディレクトリtopへ)
rails _5.1.6_ new toy_app (toy_appを作成)
cd ~/environment/toy_app (toy_appへ)
Gemfileを修正
Gemfileをきちんとsave
bundle install --without production (Gemfileに従って色々更新)
bundle update (Gemfileに従って色々更新)
git init (gitリポジトリを作成)
git add -A (gitに変更ファイルを全て追加)
git commit -m "Initialize repository" (変更ファイルにコメントをつける)
Bitbucketでtoy_appリポジトリを作成
git remote add origin git@bitbucket.org:*********/toy_app.git (gitをBitbucketに繋げる)
git push -u origin --all (Bitbucketにプッシュ)
git push -f origin master(上ので失敗したとき)
----------------------------

長かったー・・・。

次に、Applicationコントローラにhelloアクションを追加

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にプッシュ
git commit -am "Add hello"
source <(curl -sL https://cdn.learnenough.com/heroku_install)
heroku login --interactive
heroku create
git push heroku master

これで
http://limitless-forest-70076.herokuapp.com/
に表示された

・2 1 1 ユーザーのモデル設計

2章でのユーザーのデータモデル
users
id integer
name string
email string

・2 1 2 マイクロポストのモデル設計

2章でのマイクロポストのデータモデル
microposts
id integer
content text
user_id integer


・2 2 Usersリソース

まずパスをしっかりしておく
cd ~/environment/toy_app

今回は以下のコマンドでモデルを作成
rails generate scaffold User name:string email:string
詳細な説明はしないよとrailsチュートリアルでも言ってる

ログ
----------------------------
 invoke  active_record
 create    db/migrate/20191228072234_create_users.rb
 create    app/models/user.rb
 invoke    test_unit
 create      test/models/user_test.rb
 create      test/fixtures/users.yml
 invoke  resource_route
  route    resources :users
 invoke  scaffold_controller
 create    app/controllers/users_controller.rb
 invoke    erb
 create      app/views/users
 create      app/views/users/index.html.erb
 create      app/views/users/edit.html.erb
 create      app/views/users/show.html.erb
 create      app/views/users/new.html.erb
 create      app/views/users/_form.html.erb
 invoke    test_unit
 create      test/controllers/users_controller_test.rb
 invoke    helper
 create      app/helpers/users_helper.rb
 invoke      test_unit
 invoke    jbuilder
 create      app/views/users/index.json.jbuilder
 create      app/views/users/show.json.jbuilder
 create      app/views/users/_user.json.jbuilder
 invoke  test_unit
 create    test/system/users_test.rb
 invoke  assets
 invoke    coffee
 create      app/assets/javascripts/users.coffee
 invoke    scss
 create      app/assets/stylesheets/users.scss
 invoke  scss
 create    app/assets/stylesheets/scaffolds.scss
 ----------------------------

次に、データベースをマイグレートする
rails db:migrate

ログ
----------------------------
== 20191228072234 CreateUsers: migrating ======================================
-- create_table(:users)
-> 0.0020s
== 20191228072234 CreateUsers: migrated (0.0024s) =============================

rakeとmakeの違い
rakeはrubyをmakeするからrake

rails4以前はrakeコマンドだったけどrailsコマンドに統合された

最後に、rails serverでサーバーを立てる


・2 2 1 ユーザーページを探検する

今後作っていくURLとアクションと用途まとめ
URL アクション 用途
/user index すべてのユーザーを一覧するページ
/users/1 show id=1のユーザーを表示するページ
/users/new new 新規ユーザーを作成するページ
/users/1/edit edit id=1のユーザーを編集するページ

cloud9でpreview->preview running applicationして
別ページで開くの操作してから、URLの末尾にそれぞれ上のやつ打ち込むとページが遷移していく

users でnew userを押して文字適当に打ち込んでCreateUserすると
どんどん作られる


・2 2 2 MVCの挙動

以下ざっくり挙動
①ブラウザからrailsルーターへURLを渡してHTMLを要求する
②ルーターからコントローラーへURLを元にインデックスを渡す
③コントローラーからモデルへインデックスを元にデータを要求する
④モデルはデータベースからデータを取得する
⑤モデルからコントローラーへデータを渡す
⑥コントローラからビューへデータを渡す
⑦ビューからコントローラへHTMLを渡す
⑧コントローラからブラウザへHTMLを渡す

もうちょっとちゃんと書くと
1.ブラウザから「/users」とういURLのリクエストをrailsサーバーに送信する

2.「/users」のリクエストは、Raisのルーティング機構(ルーター)によってUsersコントローラ内のindexアクションに割り当てられる(config/routes.rb)

3.indexアクションが実行され、そこからUserモデルに、「すべてのユーザーを取り出せ(User.all)」と問い合わせる

4.Userモデルは問い合わせを受け、すべてのユーザーをデータベースから取り出す

5.データベースから取り出したユーザーの一覧をUserモデルからコントローラに返す

6.Usersコントローラは、ユーザーの一覧を@users変数(@はRubyのインスタンス変数を表す)に保存し、indexビューに渡す

7.indexビューガ起動し、ERB(Embedded Ruby: ビューのHTMLに埋め込まれているRubyコード)を実行して、HTMLとして正式に生成(レンダリング)する

8.コントローラは、ビューで生成されたHTMLを受け取り、ブラウザに返す

ルートを変更してみよう
config/routes.rb
root 'application#hello'
を、
root 'users#index'
に変更!
するとトップページが変わるはず!
→変わった!

app/controllers/users_controller.rbは、
scaffoldで自動的に以下のアクションが追加されている
index
show
new
edit
create
update
destroy

app/controllers/users_controller.rbのindexについてみてみる
index

app/models/user.rbについてみてみる
今は特になにもない

app/views/users/index.html.erbについてみてみる
HTMLが書かれてる


・2 2 3 Usersリソースの欠点

Usersリソースの欠点
scaffoldで生成したUsersリソースは以下の問題がある
データ検証が行われていない
ユーザー認証が行われていない
テストが書かれていない
レイアウトやスタイルが整っていない
理解が困難


・2 3 Micropostsリソース

Usersリソースと比べてみてみると理解が早いよって言ってる


・2 3 1 マイクロポストを探検する

まずぱパスをしっかりさせる
rails generate scaffold Micropost content:text user_id:integer
を実行してMicropostを作成する

ログ
----------------------------
Usage:
rails new APP_PATH [options]

Options:
-r, [--ruby=PATH]                                      # Path to the Ruby binary of your choice
                                                    # Default: /home/ec2-user/.rvm/rubies/ruby-2.6.3/bin/ruby
-m, [--template=TEMPLATE]                              # Path to some application template (can be a filesystem path or URL)
-d, [--database=DATABASE]                              # Preconfigure for selected database (options: mysql/postgresql/sqlite3/oracle/frontbase/ibm_db/sqlserver/jdbcmysql/jdbcsqlite3/jdbcpostgresql/jdbc)
                                                    # Default: sqlite3
 [--skip-yarn], [--no-skip-yarn]                    # Don't use Yarn for managing JavaScript dependencies
 [--skip-gemfile], [--no-skip-gemfile]              # Don't create a Gemfile
-G, [--skip-git], [--no-skip-git]                      # Skip .gitignore file
 [--skip-keeps], [--no-skip-keeps]                  # Skip source control .keep files
-M, [--skip-action-mailer], [--no-skip-action-mailer]  # Skip Action Mailer files
-O, [--skip-active-record], [--no-skip-active-record]  # Skip Active Record files
-P, [--skip-puma], [--no-skip-puma]                    # Skip Puma related files
-C, [--skip-action-cable], [--no-skip-action-cable]    # Skip Action Cable files
-S, [--skip-sprockets], [--no-skip-sprockets]          # Skip Sprockets files
 [--skip-spring], [--no-skip-spring]                # Don't install Spring application preloader
 [--skip-listen], [--no-skip-listen]                # Don't generate configuration that depends on the listen gem
 [--skip-coffee], [--no-skip-coffee]                # Don't use CoffeeScript
-J, [--skip-javascript], [--no-skip-javascript]        # Skip JavaScript files
 [--skip-turbolinks], [--no-skip-turbolinks]        # Skip turbolinks gem
-T, [--skip-test], [--no-skip-test]                    # Skip test files
 [--skip-system-test], [--no-skip-system-test]      # Skip system test files
 [--dev], [--no-dev]                                # Setup the application with Gemfile pointing to your Rails checkout
 [--edge], [--no-edge]                              # Setup the application with Gemfile pointing to Rails repository
 [--rc=RC]                                          # Path to file containing extra configuration options for rails command
 [--no-rc], [--no-no-rc]                            # Skip loading of extra configuration options from .railsrc file
 [--api], [--no-api]                                # Preconfigure smaller stack for API only apps
-B, [--skip-bundle], [--no-skip-bundle]                # Don't run bundle install
 [--webpack=WEBPACK]                                # Preconfigure for app-like JavaScript with Webpack (options: react/vue/angular)

Runtime options:
-f, [--force]                    # Overwrite files that already exist
-p, [--pretend], [--no-pretend]  # Run but do not make any changes
-q, [--quiet], [--no-quiet]      # Suppress status output
-s, [--skip], [--no-skip]        # Skip files that already exist

Rails options:
-h, [--help], [--no-help]        # Show this help message and quit
-v, [--version], [--no-version]  # Show Rails version number and quit

Description:
The 'rails new' command creates a new Rails application with a default
directory structure and configuration at the path you specify.

You can specify extra command-line arguments to be used every time
'rails new' runs in the .railsrc configuration file in your home directory.

Note that the arguments specified in the .railsrc file don't affect the
defaults values shown above in this help message.

Example:
rails new ~/Code/Ruby/weblog

This generates a skeletal Rails installation in ~/Code/Ruby/weblog.
----------------------------

あ、でた!パスミスで出るやつや!
しっかりパスを合わせないと!

cd ~/environment/toy_app

再度rails generate scaffold Micropost content:text user_id:integer

ログ
----------------------------
Running via Spring preloader in process 20035
 invoke  active_record
 create    db/migrate/20191228105842_create_microposts.rb
 create    app/models/micropost.rb
 invoke    test_unit
 create      test/models/micropost_test.rb
 create      test/fixtures/microposts.yml
 invoke  resource_route
  route    resources :microposts
 invoke  scaffold_controller
 create    app/controllers/microposts_controller.rb
 invoke    erb
 create      app/views/microposts
 create      app/views/microposts/index.html.erb
 create      app/views/microposts/edit.html.erb
 create      app/views/microposts/show.html.erb
 create      app/views/microposts/new.html.erb
 create      app/views/microposts/_form.html.erb
 invoke    test_unit
 create      test/controllers/microposts_controller_test.rb
 invoke    helper
 create      app/helpers/microposts_helper.rb
 invoke      test_unit
 invoke    jbuilder
 create      app/views/microposts/index.json.jbuilder
 create      app/views/microposts/show.json.jbuilder
 create      app/views/microposts/_micropost.json.jbuilder
 invoke  test_unit
 create    test/system/microposts_test.rb
 invoke  assets
 invoke    coffee
 create      app/assets/javascripts/microposts.coffee
 invoke    scss
 create      app/assets/stylesheets/microposts.scss
 invoke  scss
identical    app/assets/stylesheets/scaffolds.scss
----------------------------

OK
やればできるじゃん、俺

データベースを更新するコマンド
rails db:migrate
を実行

ログ
----------------------------
rails db:migrate
== 20191228105842 CreateMicroposts: migrating =================================
-- create_table(:microposts)
-> 0.0027s
== 20191228105842 CreateMicroposts: migrated (0.0032s) ========================
----------------------------

イマイチrails db:migrateを使うタイミングがわかってなかったけど、データベースを更新する時に使うコマンドね!把握!

マイクロポストは以下のような感じ
HTTPリクエスト URL アクション 用途
GET /microposts index すべてのマイクロポストを表示するページ
GET /microposts/1 show id=1のマイクロポストを表示するページ
GET /microposts/new new マイクロポストを新規作成するページ
POST /microposts create マイクロポストを新規作成するアクション
GET /microposts/1/edit edit id=1のマイクロポストを編集するページ
PATCH /microposts/1 update id=1のマイクロポストを更新するアクション
DELETE /microposts/1 destroy id=1のマイクロポストを削除する

新しく作られたapp/controllers/microposts_controller.rbのアクションは以下のようになっている
index
show
new
edit
create
update
destroy


・2 3 2 マイクロポストをマイクロにする

バリデーションを加えていくよ!
これはProgateでもやったな!
app/models/micropost.rb
に、
validates :content, length: { maximum: 140 }
を加えることによって、140文字制限にできる!

これで140文字以上のものをポストしてみる
1 error prohibited this micropost from being saved:
Content is too long (maximum is 140 characters)

と出てきて、ポストできないことを確認!


・2 3 3 ユーザーはたくさんマイクロポストを持っている

異なるデータモデル同士の関連付けは、Railsの強力な機能とのこと。

1人のユーザーに対して複数のマイクロポストがあることを表現する。

以下のように修正してみる
1人のユーザーに複数のマイクロポストがある
app/models/user.rb
class User < ApplicationRecord
has_many :microposts
end

1つのマイクロポストは1人のユーザーにのみ属する
app/models/micropost.rb
class Micropost < ApplicationRecord
belongs_to :user
validates :content, length: { maximum: 140 }
end

ここで、Railsアプリケーションを対話的に操作することができる便利なツールrails consoleを立ち上げる
何度も打つことになりそうだからCliborに登録しておく
rails console

ログ
----------------------------
Running via Spring preloader in process 21703
Loading development environment (Rails 5.1.6)

そして、以下のコマンドを実行してみる
1.一番最初のユーザーを取得してください
first_user = User.first

ログ
----------------------------
2.6.3 :001 > first_user = User.first
User Load (0.1ms)  SELECT  "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT ?  [["LIMIT", 1]]
=> #<User id: 3, name: "1", email: "aaaa@aaaa", created_at: "2019-12-28 11:42:12", updated_at: "2019-12-28 11:43:15"> 

2.一番最初のユーザーの全ポストを表示してください
first_user.microposts

ログ
----------------------------
2.6.3 :002 > first_user.microposts
Micropost Load (0.1ms)  SELECT  "microposts".* FROM "microposts" WHERE "microposts"."user_id" = ? LIMIT ?  [["user_id", 3], ["LIMIT", 11]]
=> #<ActiveRecord::Associations::CollectionProxy [#<Micropost id: 9, content: "test5", user_id: 3, created_at: "2019-12-28 11:50:26", updated_at: "2019-12-28 11:50:26">, #<Micropost id: 10, content: "test6", user_id: 3, created_at: "2019-12-28 11:50:34", updated_at: "2019-12-28 11:50:34">]> 

3.一番最初のユーザーの一番最初のポストを取得してください
micropost = first_user.microposts.first

ログ
----------------------------
2.6.3 :003 > micropost = first_user.microposts.first
Micropost Load (0.2ms)  SELECT  "microposts".* FROM "microposts" WHERE "microposts"."user_id" = ? ORDER BY "microposts"."id" ASC LIMIT ?  [["user_id", 3], ["LIMIT", 1]]
=> #<Micropost id: 9, content: "test5", user_id: 3, created_at: "2019-12-28 11:50:26", updated_at: "2019-12-28 11:50:26"> 

4.一番最初のユーザーの一番最初のポストのユーザーが誰かを表示してください
micropost.user

ログ
----------------------------
2.6.3 :004 > micropost.user
=> #<User id: 3, name: "1", email: "aaaa@aaaa", created_at: "2019-12-28 11:42:12", updated_at: "2019-12-28 11:43:15"> 

ほーん、SQLで取ってきてくれるんだねえ

久々に演習やってみた
演習1 ユーザーのshowで一番最初のポスト表示してみて
→ほい
<p>
<strong>Microposts:</strong>
<%= @user.microposts.first.content %>
</p>

演習2 ポストにバリデーション増やしてみて
ポストのコンテンツが空ならエラー
app/models/micropost.rb
class Micropost < ApplicationRecord
belongs_to :user
validates :content, length: { maximum: 140 },
presence: true
end

コンテンツが空のときにContent can't be blankになるのを確認

演習3 ユーザーにバリデーション増やしてみて
app/models/user.rb
class User < ApplicationRecord
has_many :microposts
validates :name, presence: true
validates :mail, presence: true
end

nameとmailが空のときに
Name can't be blank
Email can't be blank
がそれぞれ出るのを確認

・2 3 4 継承の階層

UserモデルとMicropostモデルはApplicationRecordというクラスを継承
さらに、ApplicationRecordクラスは、Active Recordが提供する基本クラス ActiveRecord::Base を継承

UsersコントローラとMicropostsコントローラはいずれもApplicationControllerを継承
さらに、ApplicationControllerは、ActionController::Baseというクラスを継承


・2 3 5 アプリケーションをデプロイする

以下のコマンドでBitbucketに登録
git status
git add -A
git status
git commit -m "Finish toy app"
git push -u origin master

以下のコマンドでHerokuに展開
git push heroku master

Herokuで表示しようとすると
We're sorry, but something went wrong.
って出るな
なんでやろ

とりあえずにconfig/routes.rbの
root 'users#index'

root 'application#hello'
に変えとく

もう一度pushしとく
今はusersとかmicropostsとか表示できないって思っとく

と思ったけどherokuでデータベース作らんといかんかったんかーい!先書いておくれ。。。

下のコマンドでOK
heroku run rails db:migrate

ダメだった
herokuのインストールからやれということか

source <(curl -sL https://cdn.learnenough.com/heroku_install)
heroku login --interactive
heroku create

https://murmuring-citadel-23094.herokuapp.com/ ができた

heroku run rails db:migrate

ログ
----------------------------
Running rails db:migrate on  limitless-forest-70076... up, run.1661 (Free)
D, [2019-12-28T13:22:47.963568 #4] DEBUG -- :    (47.5ms)  CREATE TABLE "schema_migrations" ("version" character varying NOT NULL PRIMARY KEY)
D, [2019-12-28T13:22:47.974683 #4] DEBUG -- :    (8.4ms)  CREATE TABLE "ar_internal_metadata" ("key" character varying NOT NULL PRIMARY KEY, "value" character varying, "created_at" timestamp NOT NULL, "updated_at" timestamp NOT NULL)
D, [2019-12-28T13:22:47.976328 #4] DEBUG -- :    (0.7ms)  SELECT pg_try_advisory_lock(7323854440341520305)
D, [2019-12-28T13:22:48.002722 #4] DEBUG -- :    (5.3ms)  SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
I, [2019-12-28T13:22:48.003900 #4]  INFO -- : Migrating to CreateUsers (20191228072234)
D, [2019-12-28T13:22:48.006172 #4] DEBUG -- :    (0.6ms)  BEGIN
== 20191228072234 CreateUsers: migrating ======================================
-- create_table(:users)
D, [2019-12-28T13:22:48.015845 #4] DEBUG -- :    (9.2ms)  CREATE TABLE "users" ("id" bigserial primary key, "name" character varying, "email" character varying, "created_at" timestamp NOT NULL, "updated_at" timestamp NOT NULL)
-> 0.0096s
== 20191228072234 CreateUsers: migrated (0.0097s) =============================

D, [2019-12-28T13:22:48.019914 #4] DEBUG -- :   SQL (0.8ms)  INSERT INTO "schema_migrations" ("version") VALUES ($1) RETURNING "version"  [["version", "20191228072234"]]
D, [2019-12-28T13:22:48.022156 #4] DEBUG -- :    (1.9ms)  COMMIT
I, [2019-12-28T13:22:48.022251 #4]  INFO -- : Migrating to CreateMicroposts (20191228105842)
D, [2019-12-28T13:22:48.023326 #4] DEBUG -- :    (0.6ms)  BEGIN
== 20191228105842 CreateMicroposts: migrating =================================
-- create_table(:microposts)
D, [2019-12-28T13:22:48.030185 #4] DEBUG -- :    (6.4ms)  CREATE TABLE "microposts" ("id" bigserial primary key, "content" text, "user_id" integer, "created_at" timestamp NOT NULL, "updated_at" timestamp NOT NULL)
-> 0.0068s
== 20191228105842 CreateMicroposts: migrated (0.0069s) ========================

D, [2019-12-28T13:22:48.031514 #4] DEBUG -- :   SQL (0.7ms)  INSERT INTO "schema_migrations" ("version") VALUES ($1) RETURNING "version"  [["version", "20191228105842"]]
D, [2019-12-28T13:22:48.034573 #4] DEBUG -- :    (2.9ms)  COMMIT
D, [2019-12-28T13:22:48.039097 #4] DEBUG -- :   ActiveRecord::InternalMetadata Load (0.7ms)  SELECT  "ar_internal_metadata".* FROM "ar_internal_metadata" WHERE "ar_internal_metadata"."key" = $1 LIMIT $2  [["key", "environment"], ["LIMIT", 1]]
D, [2019-12-28T13:22:48.045176 #4] DEBUG -- :    (0.6ms)  BEGIN
D, [2019-12-28T13:22:48.046688 #4] DEBUG -- :   SQL (0.8ms)  INSERT INTO "ar_internal_metadata" ("key", "value", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "key"  [["key", "environment"], ["value", "production"], ["created_at", "2019-12-28 13:22:48.045384"], ["updated_at", "2019-12-28 13:22:48.045384"]]
D, [2019-12-28T13:22:48.048193 #4] DEBUG -- :    (1.3ms)  COMMIT
D, [2019-12-28T13:22:48.049014 #4] DEBUG -- :    (0.7ms)  SELECT pg_advisory_unlock(7323854440341520305)
----------------------------

heroku rename jun-killer-rails2
で名前変更して公開!
https://jun-killer-rails2.herokuapp.com/


・2 4 最後に

本番環境でデータベースつきのWebアプリケーション動かせて良かったねみたいなこといわれた


・2 4 1 本章のまとめ

Scaffold機能でコードを自動生成すると、Webのあらゆる部分からモデルデータにアクセスしてやり取りできる
Scaffoldは手っ取り早いがRailsを理解するのには向いてない
railsはMVCモデルを採用
railsが解釈するRESTは、標準的なURLセットとデータモデルをやり取りするためのコントローラアクションが含まれてる
railsにはデータのバリデーション(validation)がサポートされており、データモデルの属性の値に制限をかけることができる
railsにはさまざまなデータモデル同士の関連付けを定義するための組み込み関数が多数用意されてる
railsコンソールを使うとコマンドラインからrailsアプリケーションとやり取りすることができる

・専門用語

Scaffold
足場という意味
MVCを勝手に作ってくれる優れもの

REST
REpresentational State Transfer
インターネットそのものやWebアプリケーションなどの、分散・ネットワーク化されたシステムやアプリケーションを構築するためのアーキテクチャスタイルの1つ

validation
検証

invoke
関与する

migrate
移行する

belong
属する

presence
存在感、出席、参列とかそんな感じ

OOP
Object オブジェクト
Oriented 指向
Programing プログラミング

この記事が気に入ったらサポートをしてみませんか?