Ruby - Ruby on Rails - Hello World!

 クラウディア
1. 概要
2. コントローラ追加
3. アクション追加
4. ビュー追加
5. ルーティング
6. 表示してみる(失敗)
7. ログを読む
8. 表示してみる

1. 概要

 さぁ、ここまで来たら「Hello World!」なわけです。  しかし、サーバの設定まで考えたら、Ruby on Rails って大変ですよ。  なんか「Ruby on Rails はお手軽!」的なことをおっしゃっているのは、サーバやデータベースの設定を考慮していなければ・・・ってことなんでしょうね。  本項は「Ruby on RailsでHelloWorld!を表示しました - Qiita」を参考にさせていただきました。  MVC を理解していないので、これからのことはいよいよちんぷんかんぷんなのですが・・・。

2. コントローラ追加

 『モデルの無いコントローラとビューだけの構成です。』だそうで『検索のコントローラということで search コントローラを追加します。』だそうです。

> cd /プロジェクトパス/
> bundle exec rails g controller search
Running via Spring preloader in process 2731
      create  app/controllers/search_controller.rb
      invoke  erb
      create    app/views/search
      invoke  test_unit
      create    test/controllers/search_controller_test.rb
      invoke  helper
      create    app/helpers/search_helper.rb
      invoke    test_unit
      invoke  assets
      invoke    coffee
      create      app/assets/javascripts/search.coffee
      invoke    scss
      create      app/assets/stylesheets/search.scss
 確かに

/プロジェクトパス/app/controllers/search_controller.rb
 が作成されました。

3. アクション追加

 『app/controllers/search_controller.rb にアクションを記述します。』ですって。

/プロジェクトパス/app/controllers/search_controller.rb
 を開いて見ると

class SearchController < ApplicationController
end
 ってなってます。まぁ、これだけなら意味は分かります。  上記を

class SearchController < ApplicationController
  def helloworld
  end
end
 と編集するのだそうで。間に2行加えています。

4. ビュー追加


/プロジェクトパス/app/views/search/helloworld.html.erb
 を作成して以下のように記述します。

<h1>Hello World!</h1>

5. ルーティング

 今度は

/プロジェクトパス/config/routes.rb
 を編集するのだそうで

Rails.application.routes.draw do
  get 'info/index'
  resources :posts
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
 となっておりますが・・・。  参考サイトとちょっと違うな・・・。

Rails.application.routes.draw do
  get 'info/index'
  resources :posts
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
  get 'helloworld' => 'search#helloworld'
end
 これでいいのかな?

6. 表示してみる(失敗)

 で

http://IPアドレス/project/helloworld
 にアクセスするのですがうまく表示されない。

We're sorry, but something went wrong.
If you are the application owner check the logs for more information
 と書いてあるのでログを見るわけです。  どうもひとつ前の節からそうするべきであったようですが。  ログに関しては別途・・・。  一度思い直して

> rails server
 で 3000 番ポートからアクセスしてみたらうまく表示できたので、編集した内容に誤りがあるわけではないのがわかりました。

7. ログを読む

 ログのファイルがわかったので表示してみると

I, [2018-11-22T12:05:49.189569 #7238]  INFO -- : [5da14123-754f-4e94-9a80-5e056d03291b] Started GET "/project/helloworld/" for 172.20.10.2 at 2018-11-22 12:05:49 +0900
I, [2018-11-22T12:05:49.191216 #7238]  INFO -- : [5da14123-754f-4e94-9a80-5e056d03291b] Processing by SearchController#helloworld as HTML
I, [2018-11-22T12:05:49.193979 #7238]  INFO -- : [5da14123-754f-4e94-9a80-5e056d03291b]   Rendering search/helloworld.html.erb within layouts/application
I, [2018-11-22T12:05:49.195026 #7238]  INFO -- : [5da14123-754f-4e94-9a80-5e056d03291b]   Rendered search/helloworld.html.erb within layouts/application (0.6ms)
I, [2018-11-22T12:05:49.197794 #7238]  INFO -- : [5da14123-754f-4e94-9a80-5e056d03291b] Completed 500 Internal Server Error in 6ms
F, [2018-11-22T12:05:49.202197 #7238] FATAL -- : [5da14123-754f-4e94-9a80-5e056d03291b]
F, [2018-11-22T12:05:49.203294 #7238] FATAL -- : [5da14123-754f-4e94-9a80-5e056d03291b] ActionView::Template::Error (The asset "application.css" is not present in the asset pipeline.):
F, [2018-11-22T12:05:49.203728 #7238] FATAL -- : [5da14123-754f-4e94-9a80-5e056d03291b]      5:     <%= csrf_meta_tags %>
[5da14123-754f-4e94-9a80-5e056d03291b]      6:     <%= csp_meta_tag %>
[5da14123-754f-4e94-9a80-5e056d03291b]      7:
[5da14123-754f-4e94-9a80-5e056d03291b]      8:     <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track': 'reload' %>
[5da14123-754f-4e94-9a80-5e056d03291b]      9:     <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
[5da14123-754f-4e94-9a80-5e056d03291b]     10:   </head>
[5da14123-754f-4e94-9a80-5e056d03291b]     11:
F, [2018-11-22T12:05:49.203919 #7238] FATAL -- : [5da14123-754f-4e94-9a80-5e056d03291b]
F, [2018-11-22T12:05:49.204074 #7238] FATAL -- : [5da14123-754f-4e94-9a80-5e056d03291b] app/views/layouts/application.html.erb:8:in `_app_views_layouts_application_html_erb___116861900126376519_17309652120'
 となっております。

ActionView::Template::Error (The asset "application.css" is not present in the asset pipeline.):
 がミソのようです。

/プロジェクトパス/app/assets/stylesheets/application.css
 てのは最初からあるけど、どうやらこれは違うものらしい。  「プリコンパイル済みのアセットを作成する - tetsuyai’s blog」が検索にかかりました。  なんか『Rake タスクでプリコンパイル済みのアセットを作成する』というのでやってみますが

> cd /プロジェクトパス/
> bundle exec rake assets:precompile RAILS_ENV=production
Yarn executable was not detected in the system.
Download Yarn at https://yarnpkg.com/en/docs/install
I, [2018-11-22T12:08:28.179791 #7365]  INFO -- : Writing /usr/local/www/project/public/assets/application-6a36cc7720e52e2810c3f0244c15fd2ade486b7602991517f9af783e84c4e63d.js
I, [2018-11-22T12:08:28.185196 #7365]  INFO -- : Writing /usr/local/www/project/public/assets/application-6a36cc7720e52e2810c3f0244c15fd2ade486b7602991517f9af783e84c4e63d.js.gz
I, [2018-11-22T12:08:28.297820 #7365]  INFO -- : Writing /usr/local/www/project/public/assets/application-35729bfbaf9967f119234595ed222f7ab14859f304ab0acc5451afb387f637fa.css
I, [2018-11-22T12:08:28.303095 #7365]  INFO -- : Writing /usr/local/www/project/public/assets/application-35729bfbaf9967f119234595ed222f7ab14859f304ab0acc5451afb387f637fa.css.gz

Yarn executable was not detected in the system.
 ちゅうてうまく作成されません・・・。  で、上記のサイトの最終手段的なことをやってみました。

/プロジェクトパス/config/environments/production.rb
 の

# Do not fallback to assets pipeline if a precompiled asset is missed.
config.assets.compile = false
 を

# Do not fallback to assets pipeline if a precompiled asset is missed.
config.assets.compile = true
 に変えて、apache を再起動

> service apache24 configtest
Performing sanity check on apache24 configuration:
Syntax OK
> service apache24 graceful
Performing sanity check on apache24 configuration:
Syntax OK
Performing a graceful restart

8. 表示してみる

 再度表示してみますと、おお出ましたよ。
「Ruby on Rails」-「apache」「Hello World!」

 今回はログがこんなんなってました。


I, [2018-11-22T12:55:32.111436 #8781]  INFO -- : [f9110145-a4ff-4bf0-8345-dd7176a16ccc] Started GET "/project/helloworld/" for 172.20.10.2 at 2018-11-22 12:55:32 +0900
I, [2018-11-22T12:55:32.115577 #8781]  INFO -- : [f9110145-a4ff-4bf0-8345-dd7176a16ccc] Processing by SearchController#helloworld as HTML
I, [2018-11-22T12:55:32.136207 #8781]  INFO -- : [f9110145-a4ff-4bf0-8345-dd7176a16ccc]   Rendering search/helloworld.html.erb within layouts/application
I, [2018-11-22T12:55:32.137259 #8781]  INFO -- : [f9110145-a4ff-4bf0-8345-dd7176a16ccc]   Rendered search/helloworld.html.erb within layouts/application (0.5ms)
I, [2018-11-22T12:55:32.139979 #8781]  INFO -- : [f9110145-a4ff-4bf0-8345-dd7176a16ccc] Completed 200 OK in 24ms (Views: 5.9ms)
ハイスピードプラン