Ruby - Ruby on Rails - サーバ起動

クラウディア 
1. 概要
2. scaffold(???)
3. 起動してみる

1. 概要

 前項までのことを掲載しているサイトはたくさんあるのですが、ここからの手順を掲載しているサイトは極端に少ないのです。  やっと見つけた「Ruby on Rails のインストール方法(Mac, Window)(最新Ruby2.5/Rails5.2対応[2018年版]) - Qiita」を参考にさせていただきました。

2. scaffold(???)

 おまじないみたいなので、唱えてみます。

> cd プロジェクトのパス
> rails generate scaffold post title:string body:text published:boolean
Running via Spring preloader in process 690
      invoke  active_record
      create    db/migrate/20181114073803_create_posts.rb
      create    app/models/post.rb
      invoke    test_unit
      create      test/models/post_test.rb
      create      test/fixtures/posts.yml
      invoke  resource_route
       route    resources :posts
      invoke  scaffold_controller
      create    app/controllers/posts_controller.rb
      invoke    erb
      create      app/views/posts
      create      app/views/posts/index.html.erb
      create      app/views/posts/edit.html.erb
      create      app/views/posts/show.html.erb
      create      app/views/posts/new.html.erb
      create      app/views/posts/_form.html.erb
      invoke    test_unit
      create      test/controllers/posts_controller_test.rb
      create      test/system/posts_test.rb
      invoke    helper
      create      app/helpers/posts_helper.rb
      invoke      test_unit
      invoke    jbuilder
      create      app/views/posts/index.json.jbuilder
      create      app/views/posts/show.json.jbuilder
      create      app/views/posts/_post.json.jbuilder
      invoke  assets
      invoke    coffee
      create      app/assets/javascripts/posts.coffee
      invoke    scss
      create      app/assets/stylesheets/posts.scss
      invoke  scss
      create    app/assets/stylesheets/scaffolds.scss
 コマンドを入力したあと、で何やらディスクにアクセスしているが何も出力されないまま待たされるので不安になりましたが。  待たされたあと何やらうまくいっているようなメッセージが出力されました。  今回の参考サイトでは、ここでデータベースを作成していますが。まぁいいでしょう。

3. 起動してみる

 参考サイトの通りに起動してみます。

> rails server
=> Booting Puma
=> Rails 5.2.1 application starting in development
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.12.0 (ruby 2.4.5-p335), codename: Llamas in Pajamas
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://vm.sing.ne.jp:3000
Exiting
/usr/local/lib/ruby/gems/2.4/gems/puma-3.12.0/lib/puma/binder.rb:272:in `initialize': getaddrinfo: hostname nor servname provided, or not known (SocketError)
        from /usr/local/lib/ruby/gems/2.4/gems/puma-3.12.0/lib/puma/binder.rb:272:in `new'
        from /usr/local/lib/ruby/gems/2.4/gems/puma-3.12.0/lib/puma/binder.rb:272:in `add_tcp_listener'
        from /usr/local/lib/ruby/gems/2.4/gems/puma-3.12.0/lib/puma/binder.rb:105:in `block in parse'
 こけましたが、getaddrinfo でこけているので意味は分かります。  とりあえず

/etc/hosts
 で簡易的に名前解決して、再挑戦

> rails server
=> Booting Puma
=> Rails 5.2.1 application starting in development
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.12.0 (ruby 2.4.5-p335), codename: Llamas in Pajamas
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://vm.sing.ne.jp:3000
Use Ctrl-C to stop
 とりあえずは動いているのだな・・・。  ブラウザで

http://IPアドレス:3000/
 にアクセスしてみますと、サーバの端末にメッセージがわらわらわいてこんな表示になりました。
「Ruby on Rails」-「エラー画面」

 あ~ごめんなさい。create と migrate を読み間違えていました。


> rails db:migrate
== 20181114073803 CreatePosts: migrating ======================================
-- create_table(:posts)
   -> 0.0552s
== 20181114073803 CreatePosts: migrated (0.0561s) =============================
 をやってから

> rails server
=> Booting Puma
=> Rails 5.2.1 application starting in development
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.12.0 (ruby 2.4.5-p335), codename: Llamas in Pajamas
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://vm.sing.ne.jp:3000
Use Ctrl-C to stop
 再度、ブラウザからアクセスするとサーバの端末にこういう表示が出て

Started GET "/" for 172.20.10.2 at 2018-11-14 17:07:08 +0900
Cannot render console from 172.20.10.2! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
   (3.4ms)  SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
  ? /usr/local/lib/ruby/gems/2.4/gems/activerecord-5.2.1/lib/active_record/log_subscriber.rb:98
Processing by Rails::WelcomeController#index as HTML
  Rendering /usr/local/lib/ruby/gems/2.4/gems/railties-5.2.1/lib/rails/templates/rails/welcome/index.html.erb
  Rendered /usr/local/lib/ruby/gems/2.4/gems/railties-5.2.1/lib/rails/templates/rails/welcome/index.html.erb (12.4ms)
Completed 200 OK in 55ms (Views: 17.8ms | ActiveRecord: 0.0ms)
 ブラウザにはこんな画面が表示されました。
「Ruby on Rails」-「初期起動画面」

 うまくいったのかしら?

 サーバ側を Ctrl+C で止めると

 こんなん表示されました。


^C- Gracefully stopping, waiting for requests to finish
=== puma shutdown: 2018-11-14 17:17:57 +0900 ===
- Goodbye!
Exiting
 う~ん、ここから先は「Rails をはじめよう | Rails ガイド」を参考にしようかしら・・・。
ハイスピードプランU-NEXT