【Ruby on Railsチュートリアル】第3章 ほぼ静的なページの作成

静的なページ

第3章はまぁ基本的なことのようだ。

テストの方法が書かれている。でも、最初にテストを考えることの重要性は理解出来るが、なかなか出来ない。

セットアップ

サンプルアプリケーションの生成

$ cd ~/environment
$ rails _5.1.4_ new sample_app
$ cd sample_app/

サンプルアプリケーション用のGemfile

source 'https://rubygems.org'

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

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

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

group :test do
  gem 'rails-controller-testing', '1.0.2'
  gem 'minitest-reporters',       '1.1.14'
  gem 'guard',                    '2.13.0'
  gem 'guard-minitest',           '2.4.4'
end

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

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

このオプションを指定することで、PostgreSQL用のpg gemをdevelopment環境にインストールせず、代わりにSQLiteがdevelopment環境とtest環境で使われるようになる。Herokuでは、development環境とproduction環境とで異なるデータベースを使うことを非推奨としているが、このサンプルアプリケーションでは両者の違いは生じない。また、SQLiteの方がPostgreSQLよりもローカルでのインストールや設定がずっと楽なので、今回は異なるデータベースを使うことにする。もしGemfileで指定されているのと異なるバージョンのgem (Rails自身のgemなど) をインストールしていた場合は、bundle updateを実行してgemを更新 (update) し、gemのバージョンを合わせておく。
 >$ bundle update

Gitリポジトリの初期化

$ git init
$ git add -A
$ git commit -m "Initialize repository"

サンプルアプリケーション向けにREADMEを書き換える

書き終わったら

$ git commit -am "Improve the README"

Bitbucket上にリポジトリを作成してpush

$ git remote add origin git@bitbucket.org:ユーザー名/sample_app.git
$ git push -u origin --all     # リポジトリやその参照先もすべてプッシュする

Herokuにデプロイ

デプロイ前にhello,world!を追加しておくと便利。じゃないと何も表示されなくて意味不明になるから。

helloアクションをApplicationコントローラーに追加する app/controllers/application_controller.rb

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception

  def hello
    render html: "hello, world!"
  end
end

ルートルーティングを設定する config/routes.rb

Rails.application.routes.draw do
  root 'application#hello'
end

ここまでをしてから

$ git commit -am "Add hello"
$ git push
$ heroku create
$ git push heroku master

私はherokuの名前を変更しておくことにした。

$ heroku rename XXX-test

確認しようと思ったときにはcreateがかなり先の方でわからないから

$ heroku open

素敵、URLが表示される。

ここでデプロイの成功を確認すると困らない。ちゃんと

hello,world!

になってるから。

静的ページ

ブランチを作る

トピックブランチを作って作業をしていく。

$ git checkout -b static-pages

静的ページの生成

generateスクリプトでコントローラを生成する。コントローラ名は静的なページを扱うことにしか使わないことで

  • コントローラ名:StaticPages

表記をキャメルケースにした。

  • アクション名:home、help、about

ここではabout以外をコマンドラインで作成。

$ rails generate controller StaticPages home help

rails generateコマンドの短縮形

完全なコマンド 短縮形
$ rails server $ rails s
$ rails console $ rails c
$ rails generate $ rails g
$ rails test $ rails t
$ bundle install $ bundle

StaticPagesコントローラをGitにプッシュ

$ git add -A
$ git commit -m "Add a Static Pages controller"
$ git push -u origin static-pages

元に戻す方法

そう、これはとても必要。まだ第3章だけど、第1章と第2章でどのくらいゼロからしたか。「戻し方」を知ってれば、戻せばいいだけだけど、知らないとゼロからよ。

自動生成とそれに対応する取り消し処理

  $ rails generate controller StaticPages home help
  $ rails destroy  controller StaticPages home help

モデルの自動生成と取り消し

$ rails generate model User name:string email:string
$ rails destroy model User

db:migrateの場合

$ rails db:migrate

# ひとつ前に戻したい
$ rails db:rollback

#最初の状態に戻したい
$ rails db:migrate VERSION=0

StaticPagesコントローラの生成

StaticPagesコントローラを生成すると

**config/routes.rb

ファイルが自動的に更新される。

StaticPagesコントローラ内のhomeアクションとhelpアクションで使うルーティング config/routes.rb

Rails.application.routes.draw do
  get  'static_pages/home'
  get  'static_pages/help'
  root 'application#hello'
end

結果を確認する Railsのdevelopmentサーバーを起動する。

$ rails server

/static_pages/homeにアクセスして結果を表示

生成されたStaticPagesコントローラ

app/controllers/static_pages_controller.rb

class StaticPagesController < ApplicationController
  def home
  end

  def help
  end
end

Homeページ用に生成されたビュー app/views/static_pages/home.html.erb

<h1>StaticPages#home</h1>
<p>Find me in app/views/static_pages/home.html.erb</p>

Helpページ用に生成されたビュー app/views/static_pages/help.html.erb

<h1>StaticPages#help</h1>
<p>Find me in app/views/static_pages/help.html.erb</p>

動的なベージの調整

Homeページ用に生成されたビュー app/views/static_pages/home.html.erb

<h1>Sample App</h1>
<p>
  This is the home page for the
  <a href="https://railstutorial.jp/">Ruby on Rails Tutorial</a>
  sample application.
</p>

Helpページ用に生成されたビュー app/views/static_pages/help.html.erb

<h1>Help</h1>
<p>
  Get help on the Ruby on Rails Tutorial at the
  <a href="https://railstutorial.jp/help">Rails Tutorial help page</a>.
  To get help on this sample app, see the
  <a href="https://railstutorial.jp/#ebook"><em>Ruby on Rails Tutorial</em>
  book</a>.
</p>

ここまではまぁ大丈夫かなと。