Tambourine作業メモ

主にスキル習得のためにやった作業のメモ。他人には基本的に無用のものです。

Rails チュートリアルをやってみる(4) 2章を開始〜2.2まで

2章はscaffoldを使ってみるらしい。

さて、前回Railsのバージョンでかなりヒドい目に遭って、訳がわからなくなった。 恐らくは、rails newしたタイミングではbundle installが走ってはいけないのだと思う。 こんな記事もある

rails new でバージョン指定したのに上のバージョンになって困った話 - のえら

うん、たぶんそうすれば良かったんだな。

というか、このお勉強用ディレクトリのvendor/bundleにRailsのgemを入れるんであれば、最初から5.1.6をいれろやという話かもしれない。

なので、乱暴かもしれないが、お勉強用ディレクトリをrm -fr ./*して、Railsを入れ直すところからやることにする。

> bundle init
Writing new Gemfile to /Users/tambara/study/rails_study/Gemfile

> vi Gemfile 
> cat Gemfile 
# frozen_string_literal: true

source "https://rubygems.org"

git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }

gem "rails", '5.1.6'

念のため、ここでもバージョンを指定しておこう。

> bundle install --path=vendor/bundle
Fetching gem metadata from https://rubygems.org/..........
Fetching gem metadata from https://rubygems.org/.
Resolving dependencies...
Fetching rake 12.3.1
Installing rake 12.3.1
Fetching concurrent-ruby 1.1.3
Installing concurrent-ruby 1.1.3
Fetching i18n 1.1.1
Installing i18n 1.1.1
Fetching minitest 5.11.3
Installing minitest 5.11.3
Fetching thread_safe 0.3.6
Installing thread_safe 0.3.6
Fetching tzinfo 1.2.5
Installing tzinfo 1.2.5
Fetching activesupport 5.1.6
Installing activesupport 5.1.6
Fetching builder 3.2.3
Installing builder 3.2.3
Fetching erubi 1.7.1
Installing erubi 1.7.1
Fetching mini_portile2 2.3.0
Installing mini_portile2 2.3.0
Fetching nokogiri 1.8.5
Installing nokogiri 1.8.5 with native extensions
Fetching rails-dom-testing 2.0.3
Installing rails-dom-testing 2.0.3
Fetching crass 1.0.4
Installing crass 1.0.4
Fetching loofah 2.2.3
Installing loofah 2.2.3
Fetching rails-html-sanitizer 1.0.4
Installing rails-html-sanitizer 1.0.4
Fetching actionview 5.1.6
Installing actionview 5.1.6
Fetching rack 2.0.6
Installing rack 2.0.6
Fetching rack-test 1.1.0
Installing rack-test 1.1.0
Fetching actionpack 5.1.6
Installing actionpack 5.1.6
Fetching nio4r 2.3.1
Installing nio4r 2.3.1 with native extensions
Fetching websocket-extensions 0.1.3
Installing websocket-extensions 0.1.3
Fetching websocket-driver 0.6.5
Installing websocket-driver 0.6.5 with native extensions
Fetching actioncable 5.1.6
Installing actioncable 5.1.6
Fetching globalid 0.4.1
Installing globalid 0.4.1
Fetching activejob 5.1.6
Installing activejob 5.1.6
Fetching mini_mime 1.0.1
Installing mini_mime 1.0.1
Fetching mail 2.7.1
Installing mail 2.7.1
Fetching actionmailer 5.1.6
Installing actionmailer 5.1.6
Fetching activemodel 5.1.6
Installing activemodel 5.1.6
Fetching arel 8.0.0
Installing arel 8.0.0
Fetching activerecord 5.1.6
Installing activerecord 5.1.6
Using bundler 1.17.1
Fetching method_source 0.9.2
Installing method_source 0.9.2
Fetching thor 0.20.3
Installing thor 0.20.3
Fetching railties 5.1.6
Installing railties 5.1.6
Fetching sprockets 3.7.2
Installing sprockets 3.7.2
Fetching sprockets-rails 3.2.1
Installing sprockets-rails 3.2.1
Fetching rails 5.1.6
Installing rails 5.1.6
Bundle complete! 1 Gemfile dependency, 38 gems now installed.
Bundled gems are installed into `./vendor/bundle`
> rails -v
Rails 5.2.1
> which rails
/Users/tambara/.rbenv/shims/rails
> bundle exec rails -v
Rails 5.1.6

あ、.ruby-versionも消えてしまったはずだ。 遅ればせながら、rbenv local 2.5.1もやり直しておく。

では、チュートリアルの2章の先頭からやっていこう

> bundle exec rails _5.1.6_ new toy_app --skip-bundle
      create  
      create  README.md
      create  Rakefile
      create  config.ru
      create  .gitignore
      create  Gemfile
         run  git init from "."
Initialized empty Git repository in /Users/tambara/study/rails_study/toy_app/.git/
      create  app
      create  app/assets/config/manifest.js
      create  app/assets/javascripts/application.js
      create  app/assets/javascripts/cable.js
      create  app/assets/stylesheets/application.css
      create  app/channels/application_cable/channel.rb
      create  app/channels/application_cable/connection.rb
      create  app/controllers/application_controller.rb
      create  app/helpers/application_helper.rb
      create  app/jobs/application_job.rb
      create  app/mailers/application_mailer.rb
      create  app/models/application_record.rb
      create  app/views/layouts/application.html.erb
      create  app/views/layouts/mailer.html.erb
      create  app/views/layouts/mailer.text.erb
      create  app/assets/images/.keep
      create  app/assets/javascripts/channels
      create  app/assets/javascripts/channels/.keep
      create  app/controllers/concerns/.keep
      create  app/models/concerns/.keep
      create  bin
      create  bin/bundle
      create  bin/rails
      create  bin/rake
      create  bin/setup
      create  bin/update
      create  bin/yarn
      create  config
      create  config/routes.rb
      create  config/application.rb
      create  config/environment.rb
      create  config/secrets.yml
      create  config/cable.yml
      create  config/puma.rb
      create  config/spring.rb
      create  config/environments
      create  config/environments/development.rb
      create  config/environments/production.rb
      create  config/environments/test.rb
      create  config/initializers
      create  config/initializers/application_controller_renderer.rb
      create  config/initializers/assets.rb
      create  config/initializers/backtrace_silencers.rb
      create  config/initializers/cookies_serializer.rb
      create  config/initializers/cors.rb
      create  config/initializers/filter_parameter_logging.rb
      create  config/initializers/inflections.rb
      create  config/initializers/mime_types.rb
      create  config/initializers/new_framework_defaults_5_1.rb
      create  config/initializers/wrap_parameters.rb
      create  config/locales
      create  config/locales/en.yml
      create  config/boot.rb
      create  config/database.yml
      create  db
      create  db/seeds.rb
      create  lib
      create  lib/tasks
      create  lib/tasks/.keep
      create  lib/assets
      create  lib/assets/.keep
      create  log
      create  log/.keep
      create  public
      create  public/404.html
      create  public/422.html
      create  public/500.html
      create  public/apple-touch-icon-precomposed.png
      create  public/apple-touch-icon.png
      create  public/favicon.ico
      create  public/robots.txt
      create  test/fixtures
      create  test/fixtures/.keep
      create  test/fixtures/files
      create  test/fixtures/files/.keep
      create  test/controllers
      create  test/controllers/.keep
      create  test/mailers
      create  test/mailers/.keep
      create  test/models
      create  test/models/.keep
      create  test/helpers
      create  test/helpers/.keep
      create  test/integration
      create  test/integration/.keep
      create  test/test_helper.rb
      create  test/system
      create  test/system/.keep
      create  test/application_system_test_case.rb
      create  tmp
      create  tmp/.keep
      create  tmp/cache
      create  tmp/cache/assets
      create  vendor
      create  vendor/.keep
      create  package.json
      remove  config/initializers/cors.rb
      remove  config/initializers/new_framework_defaults_5_1.rb

リスト2.1にGemfileを書き換えて、bundle installする

> cd toy_app
> vi Gemfile
> bundle install --without production
Fetching gem metadata from https://rubygems.org/..........
Fetching gem metadata from https://rubygems.org/.
Resolving dependencies...
Using rake 12.3.1
Using concurrent-ruby 1.1.3
Using i18n 1.1.1
Using minitest 5.11.3
Using thread_safe 0.3.6
Using tzinfo 1.2.5
Fetching activesupport 5.1.6
Installing activesupport 5.1.6
Using builder 3.2.3
Using erubi 1.7.1
Using mini_portile2 2.3.0
Using nokogiri 1.8.5
Using rails-dom-testing 2.0.3
Using crass 1.0.4
Using loofah 2.2.3
Using rails-html-sanitizer 1.0.4
Fetching actionview 5.1.6
Installing actionview 5.1.6
Using rack 2.0.6
Using rack-test 1.1.0
Fetching actionpack 5.1.6
Installing actionpack 5.1.6
Using nio4r 2.3.1
Using websocket-extensions 0.1.3
Fetching websocket-driver 0.6.5
Installing websocket-driver 0.6.5 with native extensions
Fetching actioncable 5.1.6
Installing actioncable 5.1.6
Using globalid 0.4.1
Fetching activejob 5.1.6
Installing activejob 5.1.6
Using mini_mime 1.0.1
Using mail 2.7.1
Fetching actionmailer 5.1.6
Installing actionmailer 5.1.6
Fetching activemodel 5.1.6
Installing activemodel 5.1.6
Fetching arel 8.0.0
Installing arel 8.0.0
Fetching activerecord 5.1.6
Installing activerecord 5.1.6
Using bindex 0.5.0
Using bundler 1.17.1
Fetching byebug 9.0.6
Installing byebug 9.0.6 with native extensions
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 0.20.3
Fetching railties 5.1.6
Installing railties 5.1.6
Using coffee-rails 4.2.2
Using ffi 1.9.25
Using multi_json 1.13.1
Fetching jbuilder 2.7.0
Installing jbuilder 2.7.0
Fetching jquery-rails 4.3.1
Installing jquery-rails 4.3.1
Using rb-fsevent 0.10.3
Using rb-inotify 0.9.10
Using ruby_dep 1.5.0
Using listen 3.1.5
Fetching puma 3.9.1
Installing puma 3.9.1 with native extensions
Using sprockets 3.7.2
Using sprockets-rails 3.2.1
Fetching rails 5.1.6
Installing rails 5.1.6
Using sass-listen 4.0.0
Using sass 3.7.2
Using tilt 2.0.8
Fetching sass-rails 5.0.6
Installing sass-rails 5.0.6
Using spring 2.0.2
Using spring-watcher-listen 2.0.1
Using sqlite3 1.3.13
Using turbolinks-source 5.2.0
Fetching turbolinks 5.0.1
Installing turbolinks 5.0.1
Fetching uglifier 3.2.0
Installing uglifier 3.2.0
Fetching web-console 3.5.1
Installing web-console 3.5.1
Bundle complete! 15 Gemfile dependencies, 64 gems now installed.
Gems in the group production were not installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.

あ、また~/.rbenvの下に入っちゃった。どうもよくわからないな・・・。まあ、しょうがない。続き。

> git add -A
> git commit -m 'Initialize repository'
(略)
> vi app/controllers/application_controller.rb
> vi config/routes.rb 
> git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   app/controllers/application_controller.rb
    modified:   config/routes.rb

no changes added to commit (use "git add" and/or "git commit -a")
> git add --all
> git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    modified:   app/controllers/application_controller.rb
    modified:   config/routes.rb

> git commit -m 'Add hello'
[master b08b557] Add hello
 2 files changed, 5 insertions(+), 1 deletion(-)
> heroku create
Creating app... done, ⬢ sleepy-tundra-20972
https://sleepy-tundra-20972.herokuapp.com/ | https://git.heroku.com/sleepy-tundra-20972.git
> git push heroku master
Counting objects: 90, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (76/76), done.
Writing objects: 100% (90/90), 20.00 KiB | 1.67 MiB/s, done.
Total 90 (delta 6), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote: 
remote:  !     Warning: Multiple default buildpacks reported the ability to handle this app. The first buildpack in the list below will be used.
remote:             Detected buildpacks: Ruby,Node.js
remote:             See https://devcenter.heroku.com/articles/buildpacks#buildpack-detect-order
remote: -----> Ruby app detected
remote: -----> Compiling Ruby/Rails
remote: -----> Using Ruby version: ruby-2.4.5
remote: -----> Installing dependencies using bundler 1.15.2
remote:        Running: bundle install --without development:test --path vendor/bundle --binstubs vendor/bundle/bin -j4 --deployment
remote:        Warning: the running version of Bundler (1.15.2) is older than the version that created the lockfile (1.17.1). We suggest you upgrade to the latest version of Bundler by running `gem install bundler`.
remote:        Fetching gem metadata from https://rubygems.org/.........
remote:        Fetching version metadata from https://rubygems.org/..
remote:        Fetching dependency metadata from https://rubygems.org/.
remote:        Fetching rake 12.3.1
remote:        Fetching concurrent-ruby 1.1.3
remote:        Fetching minitest 5.11.3
remote:        Installing rake 12.3.1
remote:        Installing minitest 5.11.3
remote:        Installing concurrent-ruby 1.1.3
remote:        Fetching thread_safe 0.3.6
remote:        Fetching builder 3.2.3
remote:        Installing builder 3.2.3
remote:        Installing thread_safe 0.3.6
remote:        Fetching erubi 1.7.1
remote:        Installing erubi 1.7.1
remote:        Fetching mini_portile2 2.3.0
remote:        Installing mini_portile2 2.3.0
remote:        Fetching crass 1.0.4
remote:        Installing crass 1.0.4
remote:        Fetching rack 2.0.6
remote:        Installing rack 2.0.6
remote:        Fetching nio4r 2.3.1
remote:        Fetching websocket-extensions 0.1.3
remote:        Installing nio4r 2.3.1 with native extensions
remote:        Installing websocket-extensions 0.1.3
remote:        Fetching mini_mime 1.0.1
remote:        Installing mini_mime 1.0.1
remote:        Fetching arel 8.0.0
remote:        Installing arel 8.0.0
remote:        Using bundler 1.15.2
remote:        Fetching coffee-script-source 1.12.2
remote:        Installing coffee-script-source 1.12.2
remote:        Fetching execjs 2.7.0
remote:        Installing execjs 2.7.0
remote:        Fetching method_source 0.9.2
remote:        Installing method_source 0.9.2
remote:        Fetching thor 0.20.3
remote:        Installing thor 0.20.3
remote:        Fetching ffi 1.9.25
remote:        Fetching multi_json 1.13.1
remote:        Installing multi_json 1.13.1
remote:        Fetching pg 0.20.0
remote:        Installing ffi 1.9.25 with native extensions
remote:        Installing pg 0.20.0 with native extensions
remote:        Fetching puma 3.9.1
remote:        Installing puma 3.9.1 with native extensions
remote:        Fetching rb-fsevent 0.10.3
remote:        Installing rb-fsevent 0.10.3
remote:        Fetching tilt 2.0.8
remote:        Installing tilt 2.0.8
remote:        Fetching turbolinks-source 5.2.0
remote:        Installing turbolinks-source 5.2.0
remote:        Fetching tzinfo 1.2.5
remote:        Installing tzinfo 1.2.5
remote:        Fetching nokogiri 1.8.5
remote:        Installing nokogiri 1.8.5 with native extensions
remote:        Fetching i18n 1.1.1
remote:        Installing i18n 1.1.1
remote:        Fetching websocket-driver 0.6.5
remote:        Installing websocket-driver 0.6.5 with native extensions
remote:        Fetching mail 2.7.1
remote:        Installing mail 2.7.1
remote:        Fetching rack-test 1.1.0
remote:        Installing rack-test 1.1.0
remote:        Fetching sprockets 3.7.2
remote:        Installing sprockets 3.7.2
remote:        Fetching coffee-script 2.4.1
remote:        Installing coffee-script 2.4.1
remote:        Fetching uglifier 3.2.0
remote:        Installing uglifier 3.2.0
remote:        Fetching turbolinks 5.0.1
remote:        Installing turbolinks 5.0.1
remote:        Fetching rb-inotify 0.9.10
remote:        Installing rb-inotify 0.9.10
remote:        Fetching activesupport 5.1.6
remote:        Installing activesupport 5.1.6
remote:        Fetching sass-listen 4.0.0
remote:        Installing sass-listen 4.0.0
remote:        Fetching globalid 0.4.1
remote:        Installing globalid 0.4.1
remote:        Fetching activemodel 5.1.6
remote:        Installing activemodel 5.1.6
remote:        Fetching jbuilder 2.7.0
remote:        Installing jbuilder 2.7.0
remote:        Fetching sass 3.7.2
remote:        Installing sass 3.7.2
remote:        Fetching activejob 5.1.6
remote:        Installing activejob 5.1.6
remote:        Fetching activerecord 5.1.6
remote:        Installing activerecord 5.1.6
remote:        Fetching rails-dom-testing 2.0.3
remote:        Fetching loofah 2.2.3
remote:        Installing rails-dom-testing 2.0.3
remote:        Installing loofah 2.2.3
remote:        Fetching rails-html-sanitizer 1.0.4
remote:        Installing rails-html-sanitizer 1.0.4
remote:        Fetching actionview 5.1.6
remote:        Installing actionview 5.1.6
remote:        Fetching actionpack 5.1.6
remote:        Installing actionpack 5.1.6
remote:        Fetching actionmailer 5.1.6
remote:        Fetching railties 5.1.6
remote:        Fetching actioncable 5.1.6
remote:        Installing actionmailer 5.1.6
remote:        Installing actioncable 5.1.6
remote:        Installing railties 5.1.6
remote:        Fetching sprockets-rails 3.2.1
remote:        Installing sprockets-rails 3.2.1
remote:        Fetching jquery-rails 4.3.1
remote:        Fetching rails 5.1.6
remote:        Fetching coffee-rails 4.2.2
remote:        Installing rails 5.1.6
remote:        Installing coffee-rails 4.2.2
remote:        Fetching sass-rails 5.0.6
remote:        Installing sass-rails 5.0.6
remote:        Installing jquery-rails 4.3.1
remote:        Bundle complete! 15 Gemfile dependencies, 57 gems now installed.
remote:        Gems in the groups development and test were not installed.
remote:        Bundled gems are installed into ./vendor/bundle.
remote:        Post-install message from sass:
remote:        
remote:        Ruby Sass is deprecated and will be unmaintained as of 26 March 2019.
remote:        
remote:        * If you use Sass as a command-line tool, we recommend using Dart Sass, the new
remote:          primary implementation: https://sass-lang.com/install
remote:        
remote:        * If you use Sass as a plug-in for a Ruby web framework, we recommend using the
remote:          sassc gem: https://github.com/sass/sassc-ruby#readme
remote:        
remote:        * For more details, please refer to the Sass blog:
remote:          http://sass.logdown.com/posts/7081811
remote:        
remote:        Bundle completed (43.69s)
remote:        Cleaning up the bundler cache.
remote: -----> Installing node-v8.10.0-linux-x64
remote: -----> Detecting rake tasks
remote: -----> Preparing app for Rails asset pipeline
remote:        Running: rake assets:precompile
remote:        Yarn executable was not detected in the system.
remote:        Download Yarn at https://yarnpkg.com/en/docs/install
remote:        I, [2018-11-21T07:21:47.419386 #1376]  INFO -- : Writing /tmp/build_a47d32405f236aed0cca858ff64dcf0e/public/assets/application-7e084e44b9a13db0cda25bad2ac6fdbbfe31462ec93176e245cd0bcbc079f436.js
remote:        I, [2018-11-21T07:21:47.420053 #1376]  INFO -- : Writing /tmp/build_a47d32405f236aed0cca858ff64dcf0e/public/assets/application-7e084e44b9a13db0cda25bad2ac6fdbbfe31462ec93176e245cd0bcbc079f436.js.gz
remote:        I, [2018-11-21T07:21:47.425870 #1376]  INFO -- : Writing /tmp/build_a47d32405f236aed0cca858ff64dcf0e/public/assets/application-e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855.css
remote:        I, [2018-11-21T07:21:47.426144 #1376]  INFO -- : Writing /tmp/build_a47d32405f236aed0cca858ff64dcf0e/public/assets/application-e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855.css.gz
remote:        Asset precompilation completed (2.70s)
remote:        Cleaning assets
remote:        Running: rake assets:clean
remote: -----> Detecting rails configuration
remote: 
remote: ###### WARNING:
remote: 
remote:        You have not declared a Ruby version in your Gemfile.
remote:        To set your Ruby version add this line to your Gemfile:
remote:        ruby '2.4.5'
remote:        # See https://devcenter.heroku.com/articles/ruby-versions for more information.
remote: 
remote: ###### WARNING:
remote: 
remote:        Detecting rails configuration failed
remote:        set HEROKU_DEBUG_RAILS_RUNNER=1 to debug
remote: 
remote: ###### WARNING:
remote: 
remote:        No Procfile detected, using the default web server.
remote:        We recommend explicitly declaring how to boot your server process via a Procfile.
remote:        https://devcenter.heroku.com/articles/ruby-default-web-server
remote: 
remote: 
remote: -----> Discovering process types
remote:        Procfile declares types     -> (none)
remote:        Default types for buildpack -> console, rake, web
remote: 
remote: -----> Compressing...
remote:        Done: 41.1M
remote: -----> Launching...
remote:        Released v6
remote:        https://sleepy-tundra-20972.herokuapp.com/ deployed to Heroku
remote: 
remote: Verifying deploy... done.
To https://git.heroku.com/sleepy-tundra-20972.git
 * [new branch]      master -> master

おー、今度はエラーにならなかった。よしよし。つか、ここまでは1章と何も変わらない。

さて、scaffoldはデータモデルからえいやっとする方法なので、データモデルを作る。10年前にRailsをちょっとだけ勉強したときには普通にCREATE TABLEしてそこから作ってた気がするけど、もうそういう時代ではないらしい。

> bundle exec rails generate scaffold User name:string email:string
      invoke  active_record
      create    db/migrate/20181121083708_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
> bundle exec rails db:migrate
== 20181121083708 CreateUsers: migrating ======================================
-- create_table(:users)
   -> 0.0009s
== 20181121083708 CreateUsers: migrated (0.0010s) =============================

作る順番が逆転してるわー。

rails newしてからDBの設定なんかしていないわけだけども、デフォルトがSQLiteになっているようだ。config/database.ymlをみるとそう書いてある。で、DBが作られた訳ね、どこかに。というか、database.ymlによれば、db/development.sqlite3ですな。

> ls db
development.sqlite3 migrate             schema.rb           seeds.rb

さて、この段階でrails serverしてscaffoldで出来たUserの画面を見てみよう。

> bundle exec rails server
=> Booting Puma
=> Rails 5.1.6 application starting in development 
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.9.1 (ruby 2.5.1-p57), codename: Private Caller
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://0.0.0.0:3000
Use Ctrl-C to stop

これでUSERテーブルのCRUDの機能に/usersでアクセスできる。お馴染みのscaffoldだ。Railsの原点とも言える。

Railsは「Webアプリなんて所詮、ドメインモデルをそのままテーブル構造に反映させたDBへのCRUD機能でほとんどまかなえるんだ」と言い放った。もちろん、そんな簡単なアプリばかりではないけども、簡単なアプリ(それこそブログとか)ならそれで作れて、それを最速で作れるように特化したのがRailsであり、その際に使われたのがRubyメタプログラミング(=黒魔術)だったというのが、Ruby on Railsの成り立ちである。そこから思えば遠くにきたものだ。

感慨にふけっていても仕方が無いので、どんどんいこう。

その前に2.2.1の演習だ。

1つ目は、ユーザー作成時のメッセージを調べてみろとのことだ。

f:id:Tambourine:20181121212056p:plain

こうなってる。

リロードすると、<p id="notice">の中身は消える。

app/views/users/show.html.erbを見ると、

<p id="notice"><%= notice %></p>

となっている。

app/controllers/users_controller.rbを見ると、

  # POST /users
  # POST /users.json
  def create
    @user = User.new(user_params)

    respond_to do |format|
      if @user.save
        format.html { redirect_to @user, notice: 'User was successfully created.' }
        format.json { render :show, status: :created, location: @user }
      else
        format.html { render :new }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

こうなっているので、メッセージの元はここだ。

リロードすると呼び出されるのは

  # GET /users/1
  # GET /users/1.json
  def show
  end

こっちなので、何にも出ないのだと思われる。というか、空のメソッド定義を作ると何が起きるの?

さて、2つ目。名前だけで、emailを入力しないとどうなるか。普通に作られる。 テーブル定義を確認してみよう。

> sqlite3 development.sqlite3 
SQLite version 3.19.3 2017-06-27 16:48:08
Enter ".help" for usage hints.
sqlite> .table
ar_internal_metadata  schema_migrations     users               
sqlite> .schema users
CREATE TABLE IF NOT EXISTS "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar, "email" varchar, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL);

NOT NULLじゃないのでNULLが入るのか・・・と思いきや、単に長さ0の文字列は入っているっぽい。

sqlite> SELECT name, '>' || email || '<' FROM users;
太郎冠者|>taro@ex.com<
次郎冠者|>jiro@ex.com<
三郎|><

まあ、自然だ。演習3も、別に普通に入る。validationはまだ何にもしていないんだし。

sqlite> sqlite> SELECT name, '>' || email || '<' FROM users;
太郎冠者|>taro@ex.com<
次郎冠者|>jiro@ex.com<
三郎|><
士郎正宗|>@example.com<

演習4。削除の挙動を確認する。削除は、ダイアログが出る。 どうなっているかというと、削除のリンクはこう定義されてる。

<a data-confirm="Are you sure?" rel="nofollow" data-method="delete" href="/users/3">Destroy</a>

よくわからない・・・。

ググるとこういうことらしい。

[コードリーディング] link_toの:methodと:confirmの挙動 - Qiita

な、なるほど・・・javascriptとrackの合わせ技でDELETEメソッドHTMLから送れるようにしているってことか・・・。すごいね。

2.2.2は仕組みの説明。読んで、演習に行ってみる。

演習は、/users/1/editの振る舞いの説明。編集はupdateアクション。updateはPATCHメソッドに紐づいている。 PATCHって初めて聞いた。PUTじゃないのか・・・。

/users/1/editに行くと、こんなHTMLが表示される。

<form action="/users/3" accept-charset="UTF-8" method="post">
  <input name="utf8" value="✓" type="hidden">
  <input name="_method" value="patch" type="hidden">
  <input name="authenticity_token" 
    value="5QWnf/p1Bjq4xi/HM12xu0gzsvS40Q9VjImgUP4sCDCrK9xq/fRn5vO1/VpT4UR167Ac858VSlZlWU6lX6KQ5Q==" 
    type="hidden">

  <div class="field">
    <label for="user_name">Name</label>
    <input id="user_name" value="三郎" name="user[name]" type="text">
  </div>

  <div class="field">
    <label for="user_email">Email</label>
    <input id="user_email" value="" name="user[email]" type="text">
  </div>

  <div class="actions">
    <input name="commit" value="Update User" data-disable-with="Update User" type="submit">
  </div>
</form>

_method=patchを送ることにより、PATCHメソッドに変換されるようにhiddenがセットされてる。ふむふむ。

これの元になったviewはこんなerbだ。app/views/users/edit.html.erb。

<h1>Editing User</h1>

<%= render 'form', user: @user %>

<%= link_to 'Show', @user %> |
<%= link_to 'Back', users_path %>

えええ、まじでーって感じ。上の<form>は1行かー・・・。

さて、演習の2はUPDATE文を投げている部分はどこか探してみようの巻。updateメソッドを見れば良いはず。

  # PATCH/PUT /users/1
  # PATCH/PUT /users/1.json
  def update
    respond_to do |format|
      if @user.update(user_params)
        format.html { redirect_to @user, notice: 'User was successfully updated.' }
        format.json { render :show, status: :ok, location: @user }
      else
        format.html { render :edit }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

あー、PUTでもいいらしい。UPDATEをやっているのは@user.update(user_params)だろう。 でも、user_paramsって何だろう。あ、privateなメソッドがある。

    # Never trust parameters from the scary internet, only allow the white list through.
    def user_params
      params.require(:user).permit(:name, :email)
    end

セキュリティ的にちゃばいかもよというコメントがついている。親切だ。 しかし、このメソッドを見ても何にもわからないな・・・。 まあ、おそらくparamsはリクエストのパラメータすべてで、そこからuser[:name]とuser[:email]だけを 選んだものを返すということなんだろう。たぶん。

3は上でもう答えたので完了。

2.2.3はさらっと読んで終わり。validationとtestとdesignがなく、意味もわからないから困るねと書いてある。 「scaffoldのコードを理解できるぐらいなら、そもそも本書を読む必要はないでしょう」ごもっとも。

Rails チュートリアルをやってみる(3) 1.4〜1.5 Git、そしてHerokuへのデプロイ

1.4はGitの話。しかし、rails newしたときにgit initもされているように見える。お節介だなあ。

git add -Aして全部をコミットする。ん?bundle installのパスは要らないかもしれない。ミスった。まあ、いいや。

Bitbucketにコードを上げる説明が書いてあるけども、ここはパス。次からはGitHubかGitLabのどちらかにあげるけど、今回はまあ、いいでしょ。

1.5はHerokuへのデプロイ。

まず、productionはPostgreSQLを使うようにGemfileを修正。--without production をつけて、bundle installする。

> tail -n5 Gemfile
group :production do
  gem 'pg', '0.20.0'
end


> bundle install --without production
Fetching gem metadata from https://rubygems.org/..........
Fetching gem metadata from https://rubygems.org/.
Resolving dependencies...
Using rake 12.3.1
Using concurrent-ruby 1.1.3
Using i18n 1.1.1
Using minitest 5.11.3
Using thread_safe 0.3.6
Using tzinfo 1.2.5
Using activesupport 5.1.6
Using builder 3.2.3
Using erubi 1.7.1
Using mini_portile2 2.3.0
Using nokogiri 1.8.5
Using rails-dom-testing 2.0.3
Using crass 1.0.4
Using loofah 2.2.3
Using rails-html-sanitizer 1.0.4
Using actionview 5.1.6
Using rack 2.0.6
Using rack-test 1.1.0
Using actionpack 5.1.6
Using nio4r 2.3.1
Using websocket-extensions 0.1.3
Using websocket-driver 0.6.5
Using actioncable 5.1.6
Using globalid 0.4.1
Using activejob 5.1.6
Using mini_mime 1.0.1
Using mail 2.7.1
Using actionmailer 5.1.6
Using activemodel 5.1.6
Using arel 8.0.0
Using activerecord 5.1.6
Using bindex 0.5.0
Using bundler 1.17.1
Using byebug 9.0.6
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 0.20.3
Using railties 5.1.6
Using coffee-rails 4.2.2
Using ffi 1.9.25
Using multi_json 1.13.1
Using jbuilder 2.6.4
Using jquery-rails 4.3.1
Using rb-fsevent 0.10.3
Using rb-inotify 0.9.10
Using ruby_dep 1.5.0
Using listen 3.1.5
Using puma 3.9.1
Using sprockets 3.7.2
Using sprockets-rails 3.2.1
Using rails 5.1.6
Using sass-listen 4.0.0
Using sass 3.7.2
Using tilt 2.0.8
Using sass-rails 5.0.6
Using spring 2.0.2
Using spring-watcher-listen 2.0.1
Using sqlite3 1.3.13
Using turbolinks-source 5.2.0
Using turbolinks 5.0.1
Using uglifier 3.2.0
Using web-console 3.5.1
Bundle complete! 15 Gemfile dependencies, 64 gems now installed.
Gems in the group production were not installed.
Bundled gems are installed into `./vender/bundle`

次に、Heroku CLIを入れる。

> brew install heroku/brew/heroku
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/core).
==> Updated Formulae
abcm2ps                    curaengine                 grunt-completion           maxwell                    sleuthkit
azure-storage-cpp          dnscrypt-proxy             hunspell                   micronaut                  sratoolkit
binaryen                   docfx                      imageoptim-cli             mill                       stgit
bundletool                 docker-machine-completion  influxdb                   mysqltuner                 tile38
caddy                      erlang@18                  jenkins                    nodeenv                    topgrade
certbot                    erlang@20                  jetty                      paket                      tunnel
chakra                     exploitdb                  kibana@5.6                 picard-tools               uhd
chamber                    fio                        kitchen-sync               rabbitmq                   vault
circleci                   fluxctl                    kobalt                     range-v3                   wireguard-tools
clojurescript              fn                         lego                       restic
consul                     gitlab-gem                 libqalculate               rst-lint
convox                     glslang                    libxlsxwriter              shadowsocks-libev
crystal-icr                go-bindata                 lmod                       shc

==> Tapping heroku/brew
Cloning into '/usr/local/Homebrew/Library/Taps/heroku/homebrew-brew'...
remote: Enumerating objects: 10, done.
remote: Counting objects: 100% (10/10), done.
remote: Compressing objects: 100% (8/8), done.
remote: Total 10 (delta 0), reused 7 (delta 0), pack-reused 0
Unpacking objects: 100% (10/10), done.
Tapped 2 formulae (38 files, 28.6KB).
==> Installing heroku from heroku/brew
==> Installing dependencies for heroku/brew/heroku: heroku/brew/heroku-node
==> Installing heroku/brew/heroku dependency: heroku/brew/heroku-node
==> Downloading https://cli-assets.heroku.com/homebrew/node-11.1.0.tar.xz
######################################################################## 100.0%
🍺  /usr/local/Cellar/heroku-node/11.1.0: 3 files, 38.9MB, built in 13 seconds
==> Installing heroku/brew/heroku
==> Downloading https://cli-assets.heroku.com/heroku-v7.18.9/heroku-v7.18.9.tar.xz
######################################################################## 100.0%
==> Caveats
To use the Heroku CLI's autocomplete --
  Via homebrew's shell completion:
    1) Follow homebrew's install instructions https://docs.brew.sh/Shell-Completion
        NOTE: For zsh, as the instructions mention, be sure compinit is autoloaded
              and called, either explicitly or via a framework like oh-my-zsh.
    2) Then run
      $ heroku autocomplete --refresh-cache

  OR

  Use our standalone setup:
    1) Run and follow the install steps:
      $ heroku autocomplete

Bash completion has been installed to:
  /usr/local/etc/bash_completion.d

zsh completions have been installed to:
  /usr/local/share/zsh/site-functions
==> Summary
🍺  /usr/local/Cellar/heroku/7.18.9: 17,360 files, 44.7MB, built in 48 seconds
==> Caveats
==> heroku
To use the Heroku CLI's autocomplete --
  Via homebrew's shell completion:
    1) Follow homebrew's install instructions https://docs.brew.sh/Shell-Completion
        NOTE: For zsh, as the instructions mention, be sure compinit is autoloaded
              and called, either explicitly or via a framework like oh-my-zsh.
    2) Then run
      $ heroku autocomplete --refresh-cache

  OR

  Use our standalone setup:
    1) Run and follow the install steps:
      $ heroku autocomplete

Bash completion has been installed to:
  /usr/local/etc/bash_completion.d

zsh completions have been installed to:

確認

> heroku --version
heroku/7.18.9 darwin-x64 node-v11.1.0

ログインを打つと、認証はブラウザで行われる。仕組みはよくわかんない・・・

> heroku login
heroku: Press any key to open up the browser to login or q to exit: 
Logging in... done
Logged in as m-tam@mbp.nifty.com

f:id:Tambourine:20181120190452p:plain

そして、heroku createする。うっかりこのときのコマンド実行ログを失った・・・。

まあ、とりあえず、空のアプリは動き出す。面白いね。

f:id:Tambourine:20181120195051p:plain

リポジトリもできるので、git pushするとアプリが動き出す。仕組みが気になるけど、ここでは深入りしない。

> git push heroku master
Counting objects: 5500, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (5136/5136), done.
Writing objects: 100% (5500/5500), 21.77 MiB | 1.07 MiB/s, done.
Total 5500 (delta 673), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote: 
remote:  !     Warning: Multiple default buildpacks reported the ability to handle this app. The first buildpack in the list below will be used.
remote:             Detected buildpacks: Ruby,Node.js
remote:             See https://devcenter.heroku.com/articles/buildpacks#buildpack-detect-order
remote: -----> Ruby app detected
remote: -----> Compiling Ruby/Rails
remote: -----> Using Ruby version: ruby-2.4.5
remote: -----> Installing dependencies using bundler 1.15.2
remote:        Running: bundle install --without development:test --path vendor/bundle --binstubs vendor/bundle/bin -j4 --deployment
remote:        Warning: the running version of Bundler (1.15.2) is older than the version that created the lockfile (1.17.1). We suggest you upgrade to the latest version of Bundler by running `gem install bundler`.
remote:        Fetching gem metadata from https://rubygems.org/.........
remote:        Fetching version metadata from https://rubygems.org/..
remote:        Fetching dependency metadata from https://rubygems.org/.
remote:        Fetching rake 12.3.1
remote:        Fetching concurrent-ruby 1.1.3
remote:        Fetching minitest 5.11.3
remote:        Installing rake 12.3.1
remote:        Installing minitest 5.11.3
remote:        Installing concurrent-ruby 1.1.3
remote:        Fetching thread_safe 0.3.6
remote:        Fetching builder 3.2.3
remote:        Installing thread_safe 0.3.6
remote:        Installing builder 3.2.3
remote:        Fetching erubi 1.7.1
remote:        Installing erubi 1.7.1
remote:        Fetching mini_portile2 2.3.0
remote:        Fetching crass 1.0.4
remote:        Installing mini_portile2 2.3.0
remote:        Installing crass 1.0.4
remote:        Fetching rack 2.0.6
remote:        Installing rack 2.0.6
remote:        Fetching nio4r 2.3.1
remote:        Fetching websocket-extensions 0.1.3
remote:        Installing websocket-extensions 0.1.3
remote:        Fetching mini_mime 1.0.1
remote:        Installing nio4r 2.3.1 with native extensions
remote:        Installing mini_mime 1.0.1
remote:        Fetching arel 8.0.0
remote:        Installing arel 8.0.0
remote:        Using bundler 1.15.2
remote:        Fetching coffee-script-source 1.12.2
remote:        Fetching execjs 2.7.0
remote:        Installing coffee-script-source 1.12.2
remote:        Installing execjs 2.7.0
remote:        Fetching method_source 0.9.2
remote:        Fetching thor 0.20.3
remote:        Installing method_source 0.9.2
remote:        Installing thor 0.20.3
remote:        Fetching ffi 1.9.25
remote:        Fetching multi_json 1.13.1
remote:        Installing multi_json 1.13.1
remote:        Fetching pg 0.20.0
remote:        Installing ffi 1.9.25 with native extensions
remote:        Installing pg 0.20.0 with native extensions
remote:        Fetching puma 3.9.1
remote:        Installing puma 3.9.1 with native extensions
remote:        Fetching rb-fsevent 0.10.3
remote:        Installing rb-fsevent 0.10.3
remote:        Fetching tilt 2.0.8
remote:        Installing tilt 2.0.8
remote:        Fetching turbolinks-source 5.2.0
remote:        Installing turbolinks-source 5.2.0
remote:        Fetching tzinfo 1.2.5
remote:        Installing tzinfo 1.2.5
remote:        Fetching nokogiri 1.8.5
remote:        Installing nokogiri 1.8.5 with native extensions
remote:        Fetching i18n 1.1.1
remote:        Installing i18n 1.1.1
remote:        Fetching websocket-driver 0.6.5
remote:        Installing websocket-driver 0.6.5 with native extensions
remote:        Fetching mail 2.7.1
remote:        Installing mail 2.7.1
remote:        Fetching rack-test 1.1.0
remote:        Installing rack-test 1.1.0
remote:        Fetching sprockets 3.7.2
remote:        Installing sprockets 3.7.2
remote:        Fetching coffee-script 2.4.1
remote:        Installing coffee-script 2.4.1
remote:        Fetching uglifier 3.2.0
remote:        Installing uglifier 3.2.0
remote:        Fetching turbolinks 5.0.1
remote:        Installing turbolinks 5.0.1
remote:        Fetching rb-inotify 0.9.10
remote:        Installing rb-inotify 0.9.10
remote:        Fetching activesupport 5.1.6
remote:        Installing activesupport 5.1.6
remote:        Fetching sass-listen 4.0.0
remote:        Installing sass-listen 4.0.0
remote:        Fetching globalid 0.4.1
remote:        Installing globalid 0.4.1
remote:        Fetching activemodel 5.1.6
remote:        Installing activemodel 5.1.6
remote:        Fetching jbuilder 2.6.4
remote:        Installing jbuilder 2.6.4
remote:        Fetching sass 3.7.2
remote:        Installing sass 3.7.2
remote:        Fetching activejob 5.1.6
remote:        Installing activejob 5.1.6
remote:        Fetching activerecord 5.1.6
remote:        Installing activerecord 5.1.6
remote:        Fetching rails-dom-testing 2.0.3
remote:        Fetching loofah 2.2.3
remote:        Installing rails-dom-testing 2.0.3
remote:        Installing loofah 2.2.3
remote:        Fetching rails-html-sanitizer 1.0.4
remote:        Installing rails-html-sanitizer 1.0.4
remote:        Fetching actionview 5.1.6
remote:        Installing actionview 5.1.6
remote:        Fetching actionpack 5.1.6
remote:        Installing actionpack 5.1.6
remote:        Fetching actionmailer 5.1.6
remote:        Fetching railties 5.1.6
remote:        Fetching actioncable 5.1.6
remote:        Installing actioncable 5.1.6
remote:        Installing actionmailer 5.1.6
remote:        Fetching sprockets-rails 3.2.1
remote:        Installing railties 5.1.6
remote:        Installing sprockets-rails 3.2.1
remote:        Fetching jquery-rails 4.3.1
remote:        Fetching coffee-rails 4.2.2
remote:        Fetching rails 5.1.6
remote:        Installing rails 5.1.6
remote:        Installing coffee-rails 4.2.2
remote:        Fetching sass-rails 5.0.6
remote:        Installing sass-rails 5.0.6
remote:        Installing jquery-rails 4.3.1
remote:        Bundle complete! 15 Gemfile dependencies, 57 gems now installed.
remote:        Gems in the groups development and test were not installed.
remote:        Bundled gems are installed into ./vendor/bundle.
remote:        Post-install message from sass:
remote:        
remote:        Ruby Sass is deprecated and will be unmaintained as of 26 March 2019.
remote:        
remote:        * If you use Sass as a command-line tool, we recommend using Dart Sass, the new
remote:          primary implementation: https://sass-lang.com/install
remote:        
remote:        * If you use Sass as a plug-in for a Ruby web framework, we recommend using the
remote:          sassc gem: https://github.com/sass/sassc-ruby#readme
remote:        
remote:        * For more details, please refer to the Sass blog:
remote:          http://sass.logdown.com/posts/7081811
remote:        
remote:        Bundle completed (43.57s)
remote:        Cleaning up the bundler cache.
remote: -----> Installing node-v8.10.0-linux-x64
remote: -----> Detecting rake tasks
remote: -----> Preparing app for Rails asset pipeline
remote:        Running: rake assets:precompile
remote:        rake aborted!
remote:        NoMethodError: undefined method `active_storage' for #<Rails::Application::Configuration:0x0000559958ffe4b0>
remote:        /tmp/build_810c7306c9346ae9969fea5f59763b29/vendor/bundle/ruby/2.4.0/gems/railties-5.1.6/lib/rails/railtie/configuration.rb:95:in `method_missing'
remote:        /tmp/build_810c7306c9346ae9969fea5f59763b29/config/environments/production.rb:42:in `block in <top (required)>'
remote:        /tmp/build_810c7306c9346ae9969fea5f59763b29/vendor/bundle/ruby/2.4.0/gems/railties-5.1.6/lib/rails/railtie.rb:211:in `instance_eval'
remote:        /tmp/build_810c7306c9346ae9969fea5f59763b29/vendor/bundle/ruby/2.4.0/gems/railties-5.1.6/lib/rails/railtie.rb:211:in `configure'
remote:        /tmp/build_810c7306c9346ae9969fea5f59763b29/config/environments/production.rb:1:in `<top (required)>'
remote:        /tmp/build_810c7306c9346ae9969fea5f59763b29/vendor/bundle/ruby/2.4.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:292:in `require'
remote:        /tmp/build_810c7306c9346ae9969fea5f59763b29/vendor/bundle/ruby/2.4.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:292:in `block in require'
remote:        /tmp/build_810c7306c9346ae9969fea5f59763b29/vendor/bundle/ruby/2.4.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:258:in `load_dependency'
remote:        /tmp/build_810c7306c9346ae9969fea5f59763b29/vendor/bundle/ruby/2.4.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:292:in `require'
remote:        /tmp/build_810c7306c9346ae9969fea5f59763b29/vendor/bundle/ruby/2.4.0/gems/railties-5.1.6/lib/rails/engine.rb:600:in `block (2 levels) in <class:Engine>'
remote:        /tmp/build_810c7306c9346ae9969fea5f59763b29/vendor/bundle/ruby/2.4.0/gems/railties-5.1.6/lib/rails/engine.rb:599:in `each'
remote:        /tmp/build_810c7306c9346ae9969fea5f59763b29/vendor/bundle/ruby/2.4.0/gems/railties-5.1.6/lib/rails/engine.rb:599:in `block in <class:Engine>'
remote:        /tmp/build_810c7306c9346ae9969fea5f59763b29/vendor/bundle/ruby/2.4.0/gems/railties-5.1.6/lib/rails/initializable.rb:30:in `instance_exec'
remote:        /tmp/build_810c7306c9346ae9969fea5f59763b29/vendor/bundle/ruby/2.4.0/gems/railties-5.1.6/lib/rails/initializable.rb:30:in `run'
remote:        /tmp/build_810c7306c9346ae9969fea5f59763b29/vendor/bundle/ruby/2.4.0/gems/railties-5.1.6/lib/rails/initializable.rb:59:in `block in run_initializers'
remote:        /tmp/build_810c7306c9346ae9969fea5f59763b29/vendor/bundle/ruby/2.4.0/gems/railties-5.1.6/lib/rails/initializable.rb:48:in `each'
remote:        /tmp/build_810c7306c9346ae9969fea5f59763b29/vendor/bundle/ruby/2.4.0/gems/railties-5.1.6/lib/rails/initializable.rb:48:in `tsort_each_child'
remote:        /tmp/build_810c7306c9346ae9969fea5f59763b29/vendor/bundle/ruby/2.4.0/gems/railties-5.1.6/lib/rails/initializable.rb:58:in `run_initializers'
remote:        /tmp/build_810c7306c9346ae9969fea5f59763b29/vendor/bundle/ruby/2.4.0/gems/railties-5.1.6/lib/rails/application.rb:353:in `initialize!'
remote:        /tmp/build_810c7306c9346ae9969fea5f59763b29/config/environment.rb:5:in `<top (required)>'
remote:        /tmp/build_810c7306c9346ae9969fea5f59763b29/vendor/bundle/ruby/2.4.0/gems/railties-5.1.6/lib/rails/application.rb:329:in `require'
remote:        /tmp/build_810c7306c9346ae9969fea5f59763b29/vendor/bundle/ruby/2.4.0/gems/railties-5.1.6/lib/rails/application.rb:329:in `require_environment!'
remote:        /tmp/build_810c7306c9346ae9969fea5f59763b29/vendor/bundle/ruby/2.4.0/gems/railties-5.1.6/lib/rails/application.rb:445:in `block in run_tasks_blocks'
remote:        /tmp/build_810c7306c9346ae9969fea5f59763b29/vendor/bundle/ruby/2.4.0/gems/sprockets-rails-3.2.1/lib/sprockets/rails/task.rb:62:in `block (2 levels) in define'
remote:        /tmp/build_810c7306c9346ae9969fea5f59763b29/vendor/bundle/ruby/2.4.0/gems/rake-12.3.1/exe/rake:27:in `<top (required)>'
remote:        Tasks: TOP => environment
remote:        (See full trace by running task with --trace)
remote: 
remote:  !
remote:  !     Precompiling assets failed.
remote:  !
remote:  !     Push rejected, failed to compile Ruby app.
remote: 
remote:  !     Push failed
remote: Verifying deploy...
remote: 
remote: !   Push rejected to rocky-falls-53583.
remote: 
To https://git.heroku.com/rocky-falls-53583.git
 ! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'https://git.heroku.com/rocky-falls-53583.git'

って、エラーになっとるやないの。

まあ、今回はもうこれ以上踏み込まないことにする。このエントリはもう十分長い。

Rails チュートリアルをやってみる(2) 1.3 最初のアプリケーション

1.3節から、チュートリアルに合流する

> bundle exec rails _5.1.6_ new hello_app
      create  
      create  README.md
      create  Rakefile
      create  .ruby-version
      create  config.ru
      create  .gitignore
      create  Gemfile
         run  git init from "."
Initialized empty Git repository in /Users/tambara/study/rails_study/hello_app/.git/
      create  package.json
      create  app
      create  app/assets/config/manifest.js
      create  app/assets/javascripts/application.js
      create  app/assets/javascripts/cable.js
      create  app/assets/stylesheets/application.css
      create  app/channels/application_cable/channel.rb
      create  app/channels/application_cable/connection.rb
      create  app/controllers/application_controller.rb
      create  app/helpers/application_helper.rb
      create  app/jobs/application_job.rb
      create  app/mailers/application_mailer.rb
      create  app/models/application_record.rb
      create  app/views/layouts/application.html.erb
      create  app/views/layouts/mailer.html.erb
      create  app/views/layouts/mailer.text.erb
      create  app/assets/images/.keep
      create  app/assets/javascripts/channels
      create  app/assets/javascripts/channels/.keep
      create  app/controllers/concerns/.keep
      create  app/models/concerns/.keep
      create  bin
      create  bin/bundle
      create  bin/rails
      create  bin/rake
      create  bin/setup
      create  bin/update
      create  bin/yarn
      create  config
      create  config/routes.rb
      create  config/application.rb
      create  config/environment.rb
      create  config/cable.yml
      create  config/puma.rb
      create  config/spring.rb
      create  config/storage.yml
      create  config/environments
      create  config/environments/development.rb
      create  config/environments/production.rb
      create  config/environments/test.rb
      create  config/initializers
      create  config/initializers/application_controller_renderer.rb
      create  config/initializers/assets.rb
      create  config/initializers/backtrace_silencers.rb
      create  config/initializers/content_security_policy.rb
      create  config/initializers/cookies_serializer.rb
      create  config/initializers/cors.rb
      create  config/initializers/filter_parameter_logging.rb
      create  config/initializers/inflections.rb
      create  config/initializers/mime_types.rb
      create  config/initializers/new_framework_defaults_5_2.rb
      create  config/initializers/wrap_parameters.rb
      create  config/locales
      create  config/locales/en.yml
      create  config/master.key
      append  .gitignore
      create  config/boot.rb
      create  config/database.yml
      create  db
      create  db/seeds.rb
      create  lib
      create  lib/tasks
      create  lib/tasks/.keep
      create  lib/assets
      create  lib/assets/.keep
      create  log
      create  log/.keep
      create  public
      create  public/404.html
      create  public/422.html
      create  public/500.html
      create  public/apple-touch-icon-precomposed.png
      create  public/apple-touch-icon.png
      create  public/favicon.ico
      create  public/robots.txt
      create  tmp
      create  tmp/.keep
      create  tmp/cache
      create  tmp/cache/assets
      create  vendor
      create  vendor/.keep
      create  test/fixtures
      create  test/fixtures/.keep
      create  test/fixtures/files
      create  test/fixtures/files/.keep
      create  test/controllers
      create  test/controllers/.keep
      create  test/mailers
      create  test/mailers/.keep
      create  test/models
      create  test/models/.keep
      create  test/helpers
      create  test/helpers/.keep
      create  test/integration
      create  test/integration/.keep
      create  test/test_helper.rb
      create  test/system
      create  test/system/.keep
      create  test/application_system_test_case.rb
      create  storage
      create  storage/.keep
      create  tmp/storage
      create  tmp/storage/.keep
      remove  config/initializers/cors.rb
      remove  config/initializers/new_framework_defaults_5_2.rb
         run  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/.
Resolving dependencies...
Fetching rake 12.3.1
Installing rake 12.3.1
Fetching concurrent-ruby 1.1.3
Installing concurrent-ruby 1.1.3
Fetching i18n 1.1.1
Installing i18n 1.1.1
Fetching minitest 5.11.3
Installing minitest 5.11.3
Fetching thread_safe 0.3.6
Installing thread_safe 0.3.6
Fetching tzinfo 1.2.5
Installing tzinfo 1.2.5
Fetching activesupport 5.2.1
Installing activesupport 5.2.1
Fetching builder 3.2.3
Installing builder 3.2.3
Fetching erubi 1.7.1
Installing erubi 1.7.1
Fetching mini_portile2 2.3.0
Installing mini_portile2 2.3.0
Fetching nokogiri 1.8.5
Installing nokogiri 1.8.5 with native extensions
Fetching rails-dom-testing 2.0.3
Installing rails-dom-testing 2.0.3
Fetching crass 1.0.4
Installing crass 1.0.4
Fetching loofah 2.2.3
Installing loofah 2.2.3
Fetching rails-html-sanitizer 1.0.4
Installing rails-html-sanitizer 1.0.4
Fetching actionview 5.2.1
Installing actionview 5.2.1
Fetching rack 2.0.6
Installing rack 2.0.6
Fetching rack-test 1.1.0
Installing rack-test 1.1.0
Fetching actionpack 5.2.1
Installing actionpack 5.2.1
Fetching nio4r 2.3.1
Installing nio4r 2.3.1 with native extensions
Fetching websocket-extensions 0.1.3
Installing websocket-extensions 0.1.3
Fetching websocket-driver 0.7.0
Installing websocket-driver 0.7.0 with native extensions
Fetching actioncable 5.2.1
Installing actioncable 5.2.1
Fetching globalid 0.4.1
Installing globalid 0.4.1
Fetching activejob 5.2.1
Installing activejob 5.2.1
Fetching mini_mime 1.0.1
Installing mini_mime 1.0.1
Fetching mail 2.7.1
Installing mail 2.7.1
Fetching actionmailer 5.2.1
Installing actionmailer 5.2.1
Fetching activemodel 5.2.1
Installing activemodel 5.2.1
Fetching arel 9.0.0
Installing arel 9.0.0
Fetching activerecord 5.2.1
Installing activerecord 5.2.1
Fetching mimemagic 0.3.2
Installing mimemagic 0.3.2
Fetching marcel 0.3.3
Installing marcel 0.3.3
Fetching activestorage 5.2.1
Installing activestorage 5.2.1
Fetching public_suffix 3.0.3
Installing public_suffix 3.0.3
Fetching addressable 2.5.2
Installing addressable 2.5.2
Fetching io-like 0.3.0
Installing io-like 0.3.0
Fetching archive-zip 0.11.0
Installing archive-zip 0.11.0
Fetching bindex 0.5.0
Installing bindex 0.5.0 with native extensions
Fetching msgpack 1.2.4
Installing msgpack 1.2.4 with native extensions
Fetching bootsnap 1.3.2
Installing bootsnap 1.3.2 with native extensions
Using bundler 1.17.1
Fetching byebug 10.0.2
Installing byebug 10.0.2 with native extensions
Fetching regexp_parser 1.3.0
Installing regexp_parser 1.3.0
Fetching xpath 3.2.0
Installing xpath 3.2.0
Fetching capybara 3.11.0
Installing capybara 3.11.0
Fetching ffi 1.9.25
Installing ffi 1.9.25 with native extensions
Fetching childprocess 0.9.0
Installing childprocess 0.9.0
Fetching chromedriver-helper 2.1.0
Installing chromedriver-helper 2.1.0
Fetching coffee-script-source 1.12.2
Installing coffee-script-source 1.12.2
Fetching execjs 2.7.0
Installing execjs 2.7.0
Fetching coffee-script 2.4.1
Installing coffee-script 2.4.1
Fetching method_source 0.9.2
Installing method_source 0.9.2
Fetching thor 0.20.3
Installing thor 0.20.3
Fetching railties 5.2.1
Installing railties 5.2.1
Fetching coffee-rails 4.2.2
Installing coffee-rails 4.2.2
Fetching multi_json 1.13.1
Installing multi_json 1.13.1
Fetching jbuilder 2.8.0
Installing jbuilder 2.8.0
Fetching rb-fsevent 0.10.3
Installing rb-fsevent 0.10.3
Fetching rb-inotify 0.9.10
Installing rb-inotify 0.9.10
Fetching ruby_dep 1.5.0
Installing ruby_dep 1.5.0
Fetching listen 3.1.5
Installing listen 3.1.5
Fetching puma 3.12.0
Installing puma 3.12.0 with native extensions
Fetching sprockets 3.7.2
Installing sprockets 3.7.2
Fetching sprockets-rails 3.2.1
Installing sprockets-rails 3.2.1
Fetching rails 5.2.1
Installing rails 5.2.1
Fetching rubyzip 1.2.2
Installing rubyzip 1.2.2
Fetching sass-listen 4.0.0
Installing sass-listen 4.0.0
Fetching sass 3.7.2
Installing sass 3.7.2
Fetching tilt 2.0.8
Installing tilt 2.0.8
Fetching sass-rails 5.0.7
Installing sass-rails 5.0.7
Fetching selenium-webdriver 3.141.0
Installing selenium-webdriver 3.141.0
Fetching spring 2.0.2
Installing spring 2.0.2
Fetching spring-watcher-listen 2.0.1
Installing spring-watcher-listen 2.0.1
Fetching sqlite3 1.3.13
Installing sqlite3 1.3.13 with native extensions
Fetching turbolinks-source 5.2.0
Installing turbolinks-source 5.2.0
Fetching turbolinks 5.2.0
Installing turbolinks 5.2.0
Fetching uglifier 4.1.19
Installing uglifier 4.1.19
Fetching web-console 3.7.0
Installing web-console 3.7.0
Bundle complete! 18 Gemfile dependencies, 79 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.
Post-install message from sass:

Ruby Sass is deprecated and will be unmaintained as of 26 March 2019.

* If you use Sass as a command-line tool, we recommend using Dart Sass, the new
  primary implementation: https://sass-lang.com/install

* If you use Sass as a plug-in for a Ruby web framework, we recommend using the
  sassc gem: https://github.com/sass/sassc-ruby#readme

* For more details, please refer to the Sass blog:
  http://sass.logdown.com/posts/7081811

         run  bundle exec spring binstub --all
* bin/rake: spring inserted
* bin/rails: spring inserted

nokogiriのビルドに時間かかるね

さて、チュートリアルでは、Gemfileの中身を置き換えろと言っているので、置き換えてbundle installする。んー?この場合も--pathの設定は要るのだろうか?

というか、rails new で実行されたbundlerはどこにgemを入れたんだろう。

えっと、可能性としては、

  • システムのRuby配下
  • rbenvのRuby配下
  • rails newしたディレクトリ(最初にbundle install --pathしたところ)
  • hello_app配下

くらいがあり得るのかな。難しいなあ。

で、探してみたら、どうやらrails newした時に実行されたbundle installは ~/.rbenv以下に入ったっぽい。ありゃー。

まあ、この辺りはもう悩んでも仕方ないので、もう一度hello_app以下でbundle installをパス指定で実行することにする。難しいなあ。

> bundle install --path=vender/bundle
Fetching gem metadata from https://rubygems.org/..........
Fetching gem metadata from https://rubygems.org/.
Resolving dependencies...
Bundler could not find compatible versions for gem "actionpack":
  In snapshot (Gemfile.lock):
    actionpack (= 5.2.1)

  In Gemfile:
    rails (= 5.1.6) was resolved to 5.1.6, which depends on
      actionpack (= 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.2.1, which depends on
        actionpack (= 5.2.1)

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

Gemfile.lockと合わないと怒られる。うーん、lock消すか。

> rm Gemfile.lock 
> bundle install --path=vender/bundle
Fetching gem metadata from https://rubygems.org/..........
Fetching gem metadata from https://rubygems.org/.
Resolving dependencies...
Fetching rake 12.3.1
Installing rake 12.3.1
Fetching concurrent-ruby 1.1.3
Installing concurrent-ruby 1.1.3
Fetching i18n 1.1.1
Installing i18n 1.1.1
Fetching minitest 5.11.3
Installing minitest 5.11.3
Fetching thread_safe 0.3.6
Installing thread_safe 0.3.6
Fetching tzinfo 1.2.5
Installing tzinfo 1.2.5
Fetching activesupport 5.1.6
Installing activesupport 5.1.6
Fetching builder 3.2.3
Installing builder 3.2.3
Fetching erubi 1.7.1
Installing erubi 1.7.1
Fetching mini_portile2 2.3.0
Installing mini_portile2 2.3.0
Fetching nokogiri 1.8.5
Installing nokogiri 1.8.5 with native extensions
Fetching rails-dom-testing 2.0.3
Installing rails-dom-testing 2.0.3
Fetching crass 1.0.4
Installing crass 1.0.4
Fetching loofah 2.2.3
Installing loofah 2.2.3
Fetching rails-html-sanitizer 1.0.4
Installing rails-html-sanitizer 1.0.4
Fetching actionview 5.1.6
Installing actionview 5.1.6
Fetching rack 2.0.6
Installing rack 2.0.6
Fetching rack-test 1.1.0
Installing rack-test 1.1.0
Fetching actionpack 5.1.6
Installing actionpack 5.1.6
Fetching nio4r 2.3.1
Installing nio4r 2.3.1 with native extensions
Fetching websocket-extensions 0.1.3
Installing websocket-extensions 0.1.3
Fetching websocket-driver 0.6.5
Installing websocket-driver 0.6.5 with native extensions
Fetching actioncable 5.1.6
Installing actioncable 5.1.6
Fetching globalid 0.4.1
Installing globalid 0.4.1
Fetching activejob 5.1.6
Installing activejob 5.1.6
Fetching mini_mime 1.0.1
Installing mini_mime 1.0.1
Fetching mail 2.7.1
Installing mail 2.7.1
Fetching actionmailer 5.1.6
Installing actionmailer 5.1.6
Fetching activemodel 5.1.6
Installing activemodel 5.1.6
Fetching arel 8.0.0
Installing arel 8.0.0
Fetching activerecord 5.1.6
Installing activerecord 5.1.6
Fetching bindex 0.5.0
Installing bindex 0.5.0 with native extensions
Using bundler 1.17.1
Fetching byebug 9.0.6
Installing byebug 9.0.6 with native extensions
Fetching coffee-script-source 1.12.2
Installing coffee-script-source 1.12.2
Fetching execjs 2.7.0
Installing execjs 2.7.0
Fetching coffee-script 2.4.1
Installing coffee-script 2.4.1
Fetching method_source 0.9.2
Installing method_source 0.9.2
Fetching thor 0.20.3
Installing thor 0.20.3
Fetching railties 5.1.6
Installing railties 5.1.6
Fetching coffee-rails 4.2.2
Installing coffee-rails 4.2.2
Fetching ffi 1.9.25
Installing ffi 1.9.25 with native extensions
Fetching multi_json 1.13.1
Installing multi_json 1.13.1
Fetching jbuilder 2.6.4
Installing jbuilder 2.6.4
Fetching jquery-rails 4.3.1
Installing jquery-rails 4.3.1
Fetching rb-fsevent 0.10.3
Installing rb-fsevent 0.10.3
Fetching rb-inotify 0.9.10
Installing rb-inotify 0.9.10
Fetching ruby_dep 1.5.0
Installing ruby_dep 1.5.0
Fetching listen 3.1.5
Installing listen 3.1.5
Fetching puma 3.9.1
Installing puma 3.9.1 with native extensions
Fetching sprockets 3.7.2
Installing sprockets 3.7.2
Fetching sprockets-rails 3.2.1
Installing sprockets-rails 3.2.1
Fetching rails 5.1.6
Installing rails 5.1.6
Fetching sass-listen 4.0.0
Installing sass-listen 4.0.0
Fetching sass 3.7.2
Installing sass 3.7.2
Fetching tilt 2.0.8
Installing tilt 2.0.8
Fetching sass-rails 5.0.6
Installing sass-rails 5.0.6
Fetching spring 2.0.2
Installing spring 2.0.2
Fetching spring-watcher-listen 2.0.1
Installing spring-watcher-listen 2.0.1
Fetching sqlite3 1.3.13
Installing sqlite3 1.3.13 with native extensions
Fetching turbolinks-source 5.2.0
Installing turbolinks-source 5.2.0
Fetching turbolinks 5.0.1
Installing turbolinks 5.0.1
Fetching uglifier 3.2.0
Installing uglifier 3.2.0
Fetching web-console 3.5.1
Installing web-console 3.5.1
Bundle complete! 14 Gemfile dependencies, 64 gems now installed.
Bundled gems are installed into `./vender/bundle`
Post-install message from sass:

Ruby Sass is deprecated and will be unmaintained as of 26 March 2019.

* If you use Sass as a command-line tool, we recommend using Dart Sass, the new
  primary implementation: https://sass-lang.com/install

* If you use Sass as a plug-in for a Ruby web framework, we recommend using the
  sassc gem: https://github.com/sass/sassc-ruby#readme

* For more details, please refer to the Sass blog:
  http://sass.logdown.com/posts/7081811

できた。あれ?vendorをvenderにスペルミスしてる。くぅ・・・まあ、今回はもういいか。.bundle以下に--pathの設定は維持されるようだ。

> cat .bundle/config 
---
BUNDLE_PATH: "vender/bundle"

動かしてみる。

> bundle exec rails server
Traceback (most recent call last):
    14: from bin/rails:3:in `<main>'
    13: from bin/rails:3:in `load'
    12: from /Users/tambara/study/rails_study/hello_app/bin/spring:15:in `<top (required)>'
    11: from /Users/tambara/study/rails_study/hello_app/bin/spring:15:in `require'
    10: from /Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/spring-2.0.2/lib/spring/binstub.rb:31:in `<top (required)>'
     9: from /Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/spring-2.0.2/lib/spring/binstub.rb:31:in `load'
     8: from /Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/spring-2.0.2/bin/spring:49:in `<top (required)>'
     7: from /Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/spring-2.0.2/lib/spring/client.rb:30:in `run'
     6: from /Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/spring-2.0.2/lib/spring/client/command.rb:7:in `call'
     5: from /Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/spring-2.0.2/lib/spring/client/rails.rb:28:in `call'
     4: from /Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/spring-2.0.2/lib/spring/client/rails.rb:28:in `load'
     3: from /Users/tambara/study/rails_study/hello_app/bin/rails:8:in `<top (required)>'
     2: from /Users/tambara/study/rails_study/hello_app/bin/rails:8:in `require_relative'
     1: from /Users/tambara/study/rails_study/hello_app/config/boot.rb:4:in `<top (required)>'
/Users/tambara/study/rails_study/hello_app/config/boot.rb:4:in `require': cannot load such file -- bootsnap/setup (LoadError)

bootsnapが足りんと怒られるんじゃが・・・。

解決方法は、boot.rbのrequireを外すか、Gemfileにbootsnapを足すか、どちらかだと思う。ググってみた感じでは、boot.rbを修正するほうが簡単そう。いやー、こういうところが初心者殺しなんだろうねぇ。Cloud9は偉大だ。甘えてると知識が身につかないので、なんとか頑張ろう。

boot.rbを修正してみたら、やっぱりエラー

> bundle exec rails server
Traceback (most recent call last):
    26: from bin/rails:3:in `<main>'
    25: from bin/rails:3:in `load'
    24: from /Users/tambara/study/rails_study/hello_app/bin/spring:15:in `<top (required)>'
    23: from /Users/tambara/study/rails_study/hello_app/bin/spring:15:in `require'
    22: from /Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/spring-2.0.2/lib/spring/binstub.rb:31:in `<top (required)>'
    21: from /Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/spring-2.0.2/lib/spring/binstub.rb:31:in `load'
    20: from /Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/spring-2.0.2/bin/spring:49:in `<top (required)>'
    19: from /Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/spring-2.0.2/lib/spring/client.rb:30:in `run'
    18: from /Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/spring-2.0.2/lib/spring/client/command.rb:7:in `call'
    17: from /Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/spring-2.0.2/lib/spring/client/rails.rb:28:in `call'
    16: from /Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/spring-2.0.2/lib/spring/client/rails.rb:28:in `load'
    15: from /Users/tambara/study/rails_study/hello_app/bin/rails:9:in `<top (required)>'
    14: from /Users/tambara/study/rails_study/hello_app/bin/rails:9:in `require'
    13: from /Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/railties-5.1.6/lib/rails/commands.rb:16:in `<top (required)>'
    12: from /Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/railties-5.1.6/lib/rails/command.rb:44:in `invoke'
    11: from /Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/railties-5.1.6/lib/rails/command/base.rb:63:in `perform'
    10: from /Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/thor-0.20.3/lib/thor.rb:387:in `dispatch'
     9: from /Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/thor-0.20.3/lib/thor/invocation.rb:126:in `invoke_command'
     8: from /Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/thor-0.20.3/lib/thor/command.rb:27:in `run'
     7: from /Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/railties-5.1.6/lib/rails/commands/server/server_command.rb:130:in `perform'
     6: from /Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/railties-5.1.6/lib/rails/commands/server/server_command.rb:130:in `tap'
     5: from /Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/railties-5.1.6/lib/rails/commands/server/server_command.rb:133:in `block in perform'
     4: from /Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/railties-5.1.6/lib/rails/commands/server/server_command.rb:133:in `require'
     3: from /Users/tambara/study/rails_study/hello_app/config/application.rb:9:in `<top (required)>'
     2: from /Users/tambara/study/rails_study/hello_app/config/application.rb:10:in `<module:HelloApp>'
     1: from /Users/tambara/study/rails_study/hello_app/config/application.rb:12:in `<class:Application>'
/Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/railties-5.1.6/lib/rails/application/configuration.rb:81:in `load_defaults': Unknown version "5.2" (RuntimeError)

これ、後からバージョン下げるの辛くない?

application.rbを見ると

> cat application.rb 
require_relative 'boot'

require 'rails/all'

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

module HelloApp
  class Application < Rails::Application
    # Initialize configuration defaults for originally generated Rails version.
    config.load_defaults 5.2

    # Settings in config/environments/* take precedence over those specified here.
    # Application configuration can go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded after loading
    # the framework and any gems in your application.
  end
end

よーわからんけど、5.2って書いてあっちゃいかん気がするので、5.1に書き換える

> bundle exec rails server
=> Booting Puma
=> Rails 5.1.6 application starting in development 
=> Run `rails server -h` for more startup options
Exiting
Traceback (most recent call last):
    69: from bin/rails:3:in `<main>'
    68: from bin/rails:3:in `load'
    67: from /Users/tambara/study/rails_study/hello_app/bin/spring:15:in `<top (required)>'
    66: from /Users/tambara/study/rails_study/hello_app/bin/spring:15:in `require'
    65: from /Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/spring-2.0.2/lib/spring/binstub.rb:31:in `<top (required)>'
    64: from /Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/spring-2.0.2/lib/spring/binstub.rb:31:in `load'
    63: from /Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/spring-2.0.2/bin/spring:49:in `<top (required)>'
    62: from /Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/spring-2.0.2/lib/spring/client.rb:30:in `run'
    61: from /Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/spring-2.0.2/lib/spring/client/command.rb:7:in `call'
    60: from /Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/spring-2.0.2/lib/spring/client/rails.rb:28:in `call'
    59: from /Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/spring-2.0.2/lib/spring/client/rails.rb:28:in `load'
    58: from /Users/tambara/study/rails_study/hello_app/bin/rails:9:in `<top (required)>'
    57: from /Users/tambara/study/rails_study/hello_app/bin/rails:9:in `require'
    56: from /Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/railties-5.1.6/lib/rails/commands.rb:16:in `<top (required)>'
    55: from /Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/railties-5.1.6/lib/rails/command.rb:44:in `invoke'
    54: from /Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/railties-5.1.6/lib/rails/command/base.rb:63:in `perform'
    53: from /Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/thor-0.20.3/lib/thor.rb:387:in `dispatch'
    52: from /Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/thor-0.20.3/lib/thor/invocation.rb:126:in `invoke_command'
    51: from /Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/thor-0.20.3/lib/thor/command.rb:27:in `run'
    50: from /Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/railties-5.1.6/lib/rails/commands/server/server_command.rb:130:in `perform'
    49: from /Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/railties-5.1.6/lib/rails/commands/server/server_command.rb:130:in `tap'
    48: from /Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/railties-5.1.6/lib/rails/commands/server/server_command.rb:135:in `block in perform'
    47: from /Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/railties-5.1.6/lib/rails/commands/server/server_command.rb:42:in `start'
    46: from /Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/railties-5.1.6/lib/rails/commands/server/server_command.rb:80:in `log_to_stdout'
    45: from /Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/rack-2.0.6/lib/rack/server.rb:354:in `wrapped_app'
    44: from /Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/railties-5.1.6/lib/rails/commands/server/server_command.rb:24:in `app'
    43: from /Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/rack-2.0.6/lib/rack/server.rb:219:in `app'
    42: from /Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/rack-2.0.6/lib/rack/server.rb:319:in `build_app_and_options_from_config'
    41: from /Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/rack-2.0.6/lib/rack/builder.rb:40:in `parse_file'
    40: from /Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/rack-2.0.6/lib/rack/builder.rb:49:in `new_from_string'
    39: from /Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/rack-2.0.6/lib/rack/builder.rb:49:in `eval'
    38: from config.ru:in `<main>'
    37: from config.ru:in `new'
    36: from /Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/rack-2.0.6/lib/rack/builder.rb:55:in `initialize'
    35: from /Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/rack-2.0.6/lib/rack/builder.rb:55:in `instance_eval'
    34: from config.ru:3:in `block in <main>'
    33: from config.ru:3:in `require_relative'
    32: from /Users/tambara/study/rails_study/hello_app/config/environment.rb:5:in `<top (required)>'
    31: from /Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/railties-5.1.6/lib/rails/application.rb:353:in `initialize!'
    30: from /Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/railties-5.1.6/lib/rails/initializable.rb:58:in `run_initializers'
    29: from /Users/tambara/.rbenv/versions/2.5.1/lib/ruby/2.5.0/tsort.rb:205:in `tsort_each'
    28: from /Users/tambara/.rbenv/versions/2.5.1/lib/ruby/2.5.0/tsort.rb:226:in `tsort_each'
    27: from /Users/tambara/.rbenv/versions/2.5.1/lib/ruby/2.5.0/tsort.rb:347:in `each_strongly_connected_component'
    26: from /Users/tambara/.rbenv/versions/2.5.1/lib/ruby/2.5.0/tsort.rb:347:in `call'
    25: from /Users/tambara/.rbenv/versions/2.5.1/lib/ruby/2.5.0/tsort.rb:347:in `each'
    24: from /Users/tambara/.rbenv/versions/2.5.1/lib/ruby/2.5.0/tsort.rb:349:in `block in each_strongly_connected_component'
    23: from /Users/tambara/.rbenv/versions/2.5.1/lib/ruby/2.5.0/tsort.rb:415:in `each_strongly_connected_component_from'
    22: from /Users/tambara/.rbenv/versions/2.5.1/lib/ruby/2.5.0/tsort.rb:415:in `call'
    21: from /Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/railties-5.1.6/lib/rails/initializable.rb:48:in `tsort_each_child'
    20: from /Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/railties-5.1.6/lib/rails/initializable.rb:48:in `each'
    19: from /Users/tambara/.rbenv/versions/2.5.1/lib/ruby/2.5.0/tsort.rb:421:in `block in each_strongly_connected_component_from'
    18: from /Users/tambara/.rbenv/versions/2.5.1/lib/ruby/2.5.0/tsort.rb:431:in `each_strongly_connected_component_from'
    17: from /Users/tambara/.rbenv/versions/2.5.1/lib/ruby/2.5.0/tsort.rb:422:in `block (2 levels) in each_strongly_connected_component_from'
    16: from /Users/tambara/.rbenv/versions/2.5.1/lib/ruby/2.5.0/tsort.rb:350:in `block (2 levels) in each_strongly_connected_component'
    15: from /Users/tambara/.rbenv/versions/2.5.1/lib/ruby/2.5.0/tsort.rb:228:in `block in tsort_each'
    14: from /Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/railties-5.1.6/lib/rails/initializable.rb:59:in `block in run_initializers'
    13: from /Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/railties-5.1.6/lib/rails/initializable.rb:30:in `run'
    12: from /Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/railties-5.1.6/lib/rails/initializable.rb:30:in `instance_exec'
    11: from /Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/railties-5.1.6/lib/rails/engine.rb:599:in `block in <class:Engine>'
    10: from /Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/railties-5.1.6/lib/rails/engine.rb:599:in `each'
     9: from /Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/railties-5.1.6/lib/rails/engine.rb:600:in `block (2 levels) in <class:Engine>'
     8: from /Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:292:in `require'
     7: from /Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:258:in `load_dependency'
     6: from /Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:292:in `block in require'
     5: from /Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:292:in `require'
     4: from /Users/tambara/study/rails_study/hello_app/config/environments/development.rb:1:in `<top (required)>'
     3: from /Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/railties-5.1.6/lib/rails/railtie.rb:211:in `configure'
     2: from /Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/railties-5.1.6/lib/rails/railtie.rb:211:in `instance_eval'
     1: from /Users/tambara/study/rails_study/hello_app/config/environments/development.rb:31:in `block in <top (required)>'
/Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/railties-5.1.6/lib/rails/railtie/configuration.rb:95:in `method_missing': undefined method `active_storage' for #<Rails::Application::Configuration:0x00007f982c959b80> (NoMethodError)

もう諦めて~/.rbenvのgemsで行きたい気になってきた。

これはたぶん、ActiveStorageを取ってきてないからだね。config/environments/development.rbのconfig.active_storage.service = :localコメントアウトする。

> bundle exec rails server
=> Booting Puma
=> Rails 5.1.6 application starting in development 
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.9.1 (ruby 2.5.1-p57), codename: Private Caller
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://0.0.0.0:3000
Use Ctrl-C to stop

うごいたっぽ?

ブラウザでチェックしたら、エラーが出てる。

Puma caught this error: Missing `secret_key_base` for 'development' environment, set this value in `config/secrets.yml` (RuntimeError)
/Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/railties-5.1.6/lib/rails/application.rb:510:in `validate_secret_key_config!'
/Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/railties-5.1.6/lib/rails/application.rb:247:in `env_config'
/Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/railties-5.1.6/lib/rails/engine.rb:692:in `build_request'
/Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/railties-5.1.6/lib/rails/application.rb:518:in `build_request'
/Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/railties-5.1.6/lib/rails/engine.rb:521:in `call'
/Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/puma-3.9.1/lib/puma/configuration.rb:224:in `call'
/Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/puma-3.9.1/lib/puma/server.rb:602:in `handle_request'
/Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/puma-3.9.1/lib/puma/server.rb:435:in `process_client'
/Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/puma-3.9.1/lib/puma/server.rb:299:in `block in run'
/Users/tambara/study/rails_study/hello_app/vender/bundle/ruby/2.5.0/gems/puma-3.9.1/lib/puma/thread_pool.rb:120:in `block in spawn_thread'

なんだよー。

ぐぐると、5.2でconfig/secrets.ymlはなくなって、config/credentials.yml.encになったらしい。たしかにcredentials.yml.encはある。なんか、キーっぽいものが入ってる。

適当なものを作る。

> cat config/secrets.yml 
development:
  secret_key_base: hoge

またエラー

undefined method `verbose_query_logs=' for ActiveRecord::Base:Class

これも5.2の新機能っぽ。この苦労要るのか?

development.rbのconfig.active_record.verbose_query_logs = trueコメントアウトしてみる。

これでやっと、Welcomeページが出た。

route.rbを修正して、画面にhello, worldと出すのはすんなり。起動中でもちゃんと変わるのな。偉いな。

しかし、てこずった・・・

Rails チュートリアルをやってみる(1) 〜1.2 前置きと環境のセットアップ

いまや、新人プログラマの最初の難関として知られるようになったRailsチュートリアルをやってみることにする。

Railsが無かった頃からのRubyユーザーで、なんだかんだと日常の道具としてRubyを使って20年だが、 Railsは仕事が暇な時期にちょびっと本を読んでちょっとだけ触ってみたことしかない。それも、もう10年以上前の話だ。

Railsチュートリアルに書いてあることもやったことないのにRubyistを名乗るのもおこがましいし、 プロトタイピングの機会はいろいろとあるので、ちゃんとやってみようと思う。

しかし、Rubyでちゃんとアプリを作ったことがないから、Rubygemsはまだしも、 bundlerをちゃんと理解してない。後の世に大きな影響を与えたプロダクトなんだからとても良くないなあ。

さて、心新たに第1章から読んで行ってみることにする。

1.2で環境セットアップをしているが、いきなりクラウドIDE(Cloud9)を使えと言ってくる。 時代を感じるなあ・・・。とりあえず、ローカルにセットアップしてみよう。

Ruby初学者のRuby On Rails 環境構築【Mac】 - Qiita

に従って導入する。rbenvまでは入れてあるので、bundlerの導入から。

> gem install bundler
Fetching: bundler-1.17.1.gem (100%)
Successfully installed bundler-1.17.1
Parsing documentation for bundler-1.17.1
Installing ri documentation for bundler-1.17.1
Done installing documentation for bundler after 4 seconds
1 gem installed

MySQL

> brew install mysql
==> Installing dependencies for mysql: openssl
==> Installing mysql dependency: openssl
==> Downloading https://homebrew.bintray.com/bottles/openssl-1.0.2p.high_sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring openssl-1.0.2p.high_sierra.bottle.tar.gz
==> Caveats
A CA file has been bootstrapped using certificates from the SystemRoots
keychain. To add additional certificates (e.g. the certificates added in
the System keychain), place .pem files in
  /usr/local/etc/openssl/certs

and run
  /usr/local/opt/openssl/bin/c_rehash

openssl is keg-only, which means it was not symlinked into /usr/local,
because Apple has deprecated use of OpenSSL in favor of its own TLS and crypto libraries.

If you need to have openssl first in your PATH run:
  echo 'set -g fish_user_paths "/usr/local/opt/openssl/bin" $fish_user_paths' >> ~/.config/fish/config.fish

For compilers to find openssl you may need to set:
  set -gx LDFLAGS "-L/usr/local/opt/openssl/lib"
  set -gx CPPFLAGS "-I/usr/local/opt/openssl/include"

For pkg-config to find openssl you may need to set:
  set -gx PKG_CONFIG_PATH "/usr/local/opt/openssl/lib/pkgconfig"

==> Summary
🍺  /usr/local/Cellar/openssl/1.0.2p: 1,793 files, 12.3MB
==> Installing mysql
==> Downloading https://homebrew.bintray.com/bottles/mysql-8.0.12.high_sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring mysql-8.0.12.high_sierra.bottle.tar.gz
==> /usr/local/Cellar/mysql/8.0.12/bin/mysqld --initialize-insecure --user=tambara --basedir=/usr/lo
==> Caveats
We've installed your MySQL database without a root password. To secure it run:
    mysql_secure_installation

MySQL is configured to only allow connections from localhost by default

To connect run:
    mysql -uroot

To have launchd start mysql now and restart at login:
  brew services start mysql
Or, if you don't want/need a background service you can just run:
  mysql.server start
==> Summary
🍺  /usr/local/Cellar/mysql/8.0.12: 255 files, 233.0MB
==> Caveats
==> openssl
A CA file has been bootstrapped using certificates from the SystemRoots
keychain. To add additional certificates (e.g. the certificates added in
the System keychain), place .pem files in
  /usr/local/etc/openssl/certs

and run
  /usr/local/opt/openssl/bin/c_rehash

openssl is keg-only, which means it was not symlinked into /usr/local,
because Apple has deprecated use of OpenSSL in favor of its own TLS and crypto libraries.

If you need to have openssl first in your PATH run:
  echo 'set -g fish_user_paths "/usr/local/opt/openssl/bin" $fish_user_paths' >> ~/.config/fish/config.fish

For compilers to find openssl you may need to set:
  set -gx LDFLAGS "-L/usr/local/opt/openssl/lib"
  set -gx CPPFLAGS "-I/usr/local/opt/openssl/include"

For pkg-config to find openssl you may need to set:
  set -gx PKG_CONFIG_PATH "/usr/local/opt/openssl/lib/pkgconfig"

==> mysql
We've installed your MySQL database without a root password. To secure it run:
    mysql_secure_installation

MySQL is configured to only allow connections from localhost by default

To connect run:
    mysql -uroot

To have launchd start mysql now and restart at login:
  brew services start mysql
Or, if you don't want/need a background service you can just run:
  mysql.server start
> mysql.server start
Starting MySQL
. SUCCESS! 

Rubyのバージョンを固定しておく。

> cd study/
> mkdir rails_study
> cd rails_study/
> rbenv local 2.5.1
> ls -a
.             ..            .ruby-version

Railsのインストール

> bundle init
Writing new Gemfile to /Users/tambara/study/rails_study/Gemfile
> vi Gemfile ### <== gem "rails"の行のコメントアウトを外す 
> cat Gemfile 
# frozen_string_literal: true

source "https://rubygems.org"

git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }

gem "rails"
> bundle install --path=vender/bundle
Fetching gem metadata from https://rubygems.org/..........
Fetching gem metadata from https://rubygems.org/.
Resolving dependencies...
Fetching rake 12.3.1
Installing rake 12.3.1
Fetching concurrent-ruby 1.1.3
Installing concurrent-ruby 1.1.3
Fetching i18n 1.1.1
Installing i18n 1.1.1
Fetching minitest 5.11.3
Installing minitest 5.11.3
Fetching thread_safe 0.3.6
Installing thread_safe 0.3.6
Fetching tzinfo 1.2.5
Installing tzinfo 1.2.5
Fetching activesupport 5.2.1
Installing activesupport 5.2.1
Fetching builder 3.2.3
Installing builder 3.2.3
Fetching erubi 1.7.1
Installing erubi 1.7.1
Fetching mini_portile2 2.3.0
Installing mini_portile2 2.3.0
Fetching nokogiri 1.8.5
Installing nokogiri 1.8.5 with native extensions
Fetching rails-dom-testing 2.0.3
Installing rails-dom-testing 2.0.3
Fetching crass 1.0.4
Installing crass 1.0.4
Fetching loofah 2.2.3
Installing loofah 2.2.3
Fetching rails-html-sanitizer 1.0.4
Installing rails-html-sanitizer 1.0.4
Fetching actionview 5.2.1
Installing actionview 5.2.1
Fetching rack 2.0.6
Installing rack 2.0.6
Fetching rack-test 1.1.0
Installing rack-test 1.1.0
Fetching actionpack 5.2.1
Installing actionpack 5.2.1
Fetching nio4r 2.3.1
Installing nio4r 2.3.1 with native extensions
Fetching websocket-extensions 0.1.3
Installing websocket-extensions 0.1.3
Fetching websocket-driver 0.7.0
Installing websocket-driver 0.7.0 with native extensions
Fetching actioncable 5.2.1
Installing actioncable 5.2.1
Fetching globalid 0.4.1
Installing globalid 0.4.1
Fetching activejob 5.2.1
Installing activejob 5.2.1
Fetching mini_mime 1.0.1
Installing mini_mime 1.0.1
Fetching mail 2.7.1
Installing mail 2.7.1
Fetching actionmailer 5.2.1
Installing actionmailer 5.2.1
Fetching activemodel 5.2.1
Installing activemodel 5.2.1
Fetching arel 9.0.0
Installing arel 9.0.0
Fetching activerecord 5.2.1
Installing activerecord 5.2.1
Fetching mimemagic 0.3.2
Installing mimemagic 0.3.2
Fetching marcel 0.3.3
Installing marcel 0.3.3
Fetching activestorage 5.2.1
Installing activestorage 5.2.1
Using bundler 1.17.1
Fetching method_source 0.9.2
Installing method_source 0.9.2
Fetching thor 0.20.3
Installing thor 0.20.3
Fetching railties 5.2.1
Installing railties 5.2.1
Fetching sprockets 3.7.2
Installing sprockets 3.7.2
Fetching sprockets-rails 3.2.1
Installing sprockets-rails 3.2.1
Fetching rails 5.2.1
Installing rails 5.2.1
Bundle complete! 1 Gemfile dependency, 41 gems now installed.
Bundled gems are installed into `./vender/bundle`
> bundle exec rails -v
Rails 5.2.1

CSS GRIDを勉強してみる(12) - アスキーアートっぽい指定

ガイドは新しいページに入った。

developer.mozilla.org

グリッド線に沿って配置するのが王道なんだけども、そうではないやり方もある。 アスキーアートっぽいやり方が。 いや、ガイドにそう書いてあんだもんよ("we sometimes call this the ascii-art method of grid layout")。

さて、今回はこんな2ペインアプリっぽい構成を作る。 まずは、前回までの通り、グリッド線を使って構成してみよう

HTML

<div class="wrapper">
  <div class="header">Header</div>
  <div class="footer">Footer</div>
  <div class="content">Content</div>
  <div class="sidebar">
    <div style="height: 100px">Sidebar</div>
  </div>
</div>

CSS。わかりやすいと思う。

.wrapper {
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  grid-gap: 2px;
}

.header {
  grid-row: 1;
  grid-column: 1 / -1;
}

.footer {
  grid-row: 3;
  grid-column: 1 / -1;
}

.content {
  grid-row: 2;
  grid-column: 4 / -1;
}

.sidebar {
  grid-row: 2;
  grid-column: 1 / 4;
}

レンダリング結果はこうなる。

f:id:Tambourine:20181025123926p:plain

アプリっぽく見えるように、 真ん中の高さを稼ぐためにSidebarのところに高さ100pxダミーコンテンツをいれてみた。

さて、これをgrid-template-areasを使って表現してみよう。

各アイテムにgrid-areaで名前を付ける。

.header {
  grid-area: h;
}

.footer {
  grid-area: f;
}

.content {
  grid-area: c;
}

.sidebar {
  grid-area: s;
}

そして、それらの配置をアスキーアートっぽく書く。こんな感じ。 "でクォートしなきゃいけないのがイケてないな。

.wrapper {
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  grid-gap: 2px;
  grid-template-areas: 
    "h h h h h h h h h"
    "s s s c c c c c c"
    "f f f f f f f f f";
}

空白のセルを指定する場合には.で埋める。こんな感じ

wrapper {
  grid-template-areas: 
     "h h h h h h h h ."
     "s s . c c c c c c"
     ". . f f f f f f f"
}

f:id:Tambourine:20181025130754p:plain

メディアクエリとの組み合わせ

Bootstrapが流行ったのは、お手軽にレスポンシブルなデザインを 組み込むことができることが一因だったと思うけど、 正統派なやり方は、メディアクエリを使う方法かと思う。

というわけで、このgrid-template-areaをメディアクエリと組み合わせた例はこんな感じ。

wrapper {
  display: grid;
  grid-template-columns: 1fr;
  grid-gap: 2px;
  grid-template-areas: 
    "h" 
    "c"
    "s"
    "f";
}

@media (min-width: 500px) {
  .wrapper {
    grid-template-columns: repeat(9, 1fr);
    grid-template-areas: 
      "h h h h h h h h h"
      "s s s c c c c c c"
      "f f f f f f f f f";
  }
}

ウィンドウを狭めていくと、表示が変わる。

CSS GRIDを勉強してみる(11) - さまざまなshorthand

ここからは

developer.mozilla.org

を読む。エゲレス語の文書を読んでいて、かつ、私の英語能力がプアなので、英語そのままの用語が出てきたり、 英文をそのまま引用したり、内容を勘違いしていたりするかもしれない。知ったこっちゃないけど。

あと、少しペースアップしないと読み終わらない気がする・・・。

今回のスタートは、この状態だ。

まずはHTML

<div class="wrapper">
  <div class="box1">One</div>
  <div class="box2">Two</div>
  <div class="box3">Three</div>
  <div class="box4">Four</div>
  <div class="box5">Five</div>
</div>

次にCSS。ちょっと長い。 3×3のグリッドを指定していて、box1〜4はその中に配置。 box5は横幅3列分を指定しているが、 縦の位置は指定していないので最後にくっついている。 つまり、box5がある位置は、暗黙の列だ。

.wrapper {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 50px);
  grid-gap: 2px;
}

.box1 {
  grid-column-start: 1;
  grid-column-end: 2;
  grid-row-start: 1;
  grid-row-end: 4;
}

.box2 {
  grid-column-start: 3;
  grid-column-end: 4;
  grid-row-start: 1;
  grid-row-end: 3;
}

.box3 {
  grid-column-start: 2;
  grid-column-end: 3;
  grid-row-start: 1;
  grid-row-end: 2;
}

.box4 {
  grid-column-start: 2;
  grid-column-end: 4;
  grid-row-start: 3;
  grid-row-end: 4;
}

.box5 {
  grid-column-start: 1;
  grid-column-end: 4;
}

レンダリング結果は、こうなる。

f:id:Tambourine:20181023233402p:plain

今回はここからレンダリング結果は変わらない。 位置の指定の仕方の省略記法の紹介だけだ。

*-endの省略

まず、グリットひとつ分の範囲を指定する場合、*-endは指定しなくても良い。

そのため、少し短く出来る。

.box1 {
  grid-column-start: 1;
  grid-row-start: 1;
  grid-row-end: 4;
}

.box2 {
  grid-column-start: 3;
  grid-row-start: 1;
  grid-row-end: 3;
}

.box3 {
  grid-column-start: 2;
  grid-row-start: 1;
}

.box4 {
  grid-column-start: 2;
  grid-column-end: 4;
  grid-row-start: 3;
}

.box5 {
  grid-column-start: 1;
  grid-column-end: 4;
}

負のグリッド線番号

グリッド線の番号は正の数と負の数両方がついている。 最初の番号は1であり、最後は行あるいは列の最大値+1である。 今は3×3のグリッドなので最小が1、最大が4だ。 負の番号は最大を-1として同じようについている。最小が-4, 最大が−1だ。 要するに何行何列のグリッドを作ろうが、左端あるいは上端のグリッド線の番号は1であるように、 右端あるいは下端のグリッド線の番号は-1である。

なので、右端に接しているエリアを指定したいのであれば、

.box1 {
  grid-column-start: 1;
  grid-row-start: 1;
  grid-row-end: -1;
}

のように-1を使う方がわかりやすいことが多いだろう。 この例では、box1は上端から下端まで前列をつないだエリアだと言うことがわかりやすい。

ただし、この下端は明示的なグリッドの下端である。 下のグリッド線番号を見て欲しい。右端の負の番号が切れて見えなくならないように列幅を狭めてある。

f:id:Tambourine:20181024001834p:plain

グリッドの右端の縦のグリッド線の番号は4で-1だ。 同じように横のグリッド線も4が-1だ。 明示的に指定した3×3のグリッドからbox5がはみ出したために、 横のグリッド線番号5が存在するが、これには負の番号がつかないことに注意。

grid-column 省略記法

startとendを1行で書く省略記法がある。単に/で区切って並べればよい。こんな感じ。

.box1 {
  grid-column: 1;
  grid-row: 1 / -1;
}

.box2 {
  grid-column: 3;
  grid-row: 1 / 3;
}

.box3 {
  grid-column: 2;
  grid-row: 1;
}

.box4 {
  grid-column: 2 / -1;
  grid-row: 3;
}

.box5 {
  grid-column: 1 / -1;
}

ぐっと短くできた。*-endが省略できていたものは、 同じように/以降は省略できる。

grid-area

エリアを取り囲む4つのグリッド線を1行で指定する記法もある。 grid-areaを使う。指定する順番は、上 / 左 / 下 / 右の順。 少し上で出したグリッド線の番号表示を見ながら確認して欲しいのだが、box1であれば 1 / 1 / 4 / 2になる。もちろん1 / 1 / -1 / 2でも同じだ。

というわけで、以下のようにCSSは短く出来るが、わかりやすいかは微妙。

.box1 {
  grid-area: 1 / 1 / -1 / 2;
}

.box2 {
  grid-area: 1 / 3 / 3 / 4;
}

.box3 {
  grid-area: 1 / 2 / 2 / 3;
}

.box4 {
  grid-area: 3 / 2 / 4 / -1;
}

span

もちろん、「グリッド線の何番目まで」ではなく、「セルいくつ分」で幅を指定したいこともある。 その場合にはspanを使う。

例を見れば、すぐわかると思う。これまでのものは、こう書き換え可能だ。

.box1 {
  grid-column: 1;
  grid-row: 1 / span 3;
}

.box2 {
  grid-column: 3;
  grid-row: 1 / span 2;
}

.box3 {
  grid-column: 2;
  grid-row: 1;
}

.box4 {
  grid-column: 2 / span 2;
  grid-row: 3;
}

CSS GRIDを勉強してみる(10) - display: contentsとの関係

今度はdisplay: contentsとの関係の話。また知らない子の登場である。

ちょっと調べてみたところ、display: contentsを指定すると起きることは あたかもその内容がタグで囲われていないように扱われるということのようだ。ほっほーう。 DOMツリー上、レベルがひとつ上がるようなことになるわけ。 グリッドの場合に何が起きるかだけど、グリッドのアイテムに対してdisplay: contentsを指定すると、 その中のブロック要素がグリッドアイテムとして扱われるようになる。

というわけで、box1の中に複数のアイテムを持っているような例からスタートしよう。 HTML とCSSはこんなかんじ。

<div class="wrapper">
  <div class="box1">
    <div class="nest">a</div>
    <div class="nest">b</div>
    <div class="nest">c</div>
  </div>
  <div class="box2">Two</div>
  <div class="box3">Three</div>
  <div class="box4">Four</div>
  <div class="box5">Five</div>
</div>
.wrapper {
  color: white;

  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-auto-rows: minmax(60px, auto);
}

.wrapper div{
  padding: 10px;
}

.box1 {
  background-color: #660000;

  grid-column-start: 1;
  grid-column-end: 5;
}

.nest {
  background-color: ivory;
  color: black;
  border-color: tomato;
  border-style: solid;
}

.box2 {
  background-color: #006600;
}

.box3 {
  background-color: #000066;
}

.box4 {
  background-color: #666600;
}

.box5 {
  background-color: #660066;
}

レンダリング結果はこう

f:id:Tambourine:20181023124656p:plain

ここで、box1にdisplay: contentsをセットすると、

f:id:Tambourine:20181023131254p:plain

のように、box1の内側の要素がボックスコンテナの直近の子孫のようにグリッドに並ぶことになる。