2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

railsチュートリアル3章 メモ書き

Posted at

3章チュートリアルをやったメモ書きです

$ mkdir environment
$ cd ~/environment
$ rails -v
Rails 5.2.2
$ rails _5.2.2_ new sample_app
Errno::EACCES: Permission denied @ dir_s_mkdir -
/Users/自分の名前/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/extensions/x86_64-darwin-17/2.5.0-static/bindex-0.5.0
An error occurred while installing bindex (0.5.0), and Bundler cannot continue.
Make sure that `gem install bindex -v '0.5.0' --source 'https://rubygems.org/'` succeeds before bundling.

In Gemfile:
  web-console was resolved to 3.7.0, which depends on
    bindex
         run  bundle exec spring binstub --all
bundler: command not found: spring
Install missing gem executables with `bundle install`

権限がないから作ることができないということらしい
うそ。よくわからなかったから取り敢えず

$ sudo rails _5.2.2_ new sample_app

railsのバージョンをチュートリアルと合わせてないことに気づいたけどもういいや
テストの流れを知りたいだけだからです

$ cd sample_app/

Gemgailの中身

Gemfile.
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '2.5.1'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.2.2'
# Use sqlite3 as the database for Active Record
# gem 'sqlite3'
# Use Puma as the app server
gem 'puma', '~> 3.11'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'mini_racer', platforms: :ruby

# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.2'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.5'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 4.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use ActiveStorage variant
# gem 'mini_magick', '~> 4.8'

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.1.0', require: false

group :development, :test do
  gem 'sqlite3', '1.3.13'
  gem 'byebug', '~> 9.0', '>= 9.0.6', platform: [:mri, :mingw, :x64_mingw]
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',                 '5.10.3'
  gem 'minitest-reporters',       '1.1.14'
  gem 'guard',                    '2.13.0'
  gem 'guard-minitest',           '2.4.4'
end

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

gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

エラー=>minitestを最新のものにしなさい

gem 'byebug', '~> 9.0', '>= 9.0.6', platform: [:mri, :mingw, :x64_mingw]

$ sudo bundle update minitest

Githubで作業ファイルを移動させるリポジトリを作成する

ブラウザのgithubのRepositoriesメニューに移動して緑色のNEWを押してRepositorieを作る。
READMEをチュートリアルで作った場合は
Initialize this repository with a README
にチェックは入れないで良い

$ git remote add origin https://github.com/gitでの自分の名前/自分が打ち込んだリポジトリの名前.git
$ git push -u origin --all     # リポジトリやその参照先もすべてプッシュする

3.2 静的ページ

チュートリアルそのまま
https://railstutorial.jp/chapters/static_pages?version=4.2#sec-static_pages

$ bundle exec rails s

/static_pages/homeをurlの末尾に追加してアクセス
成功

スクリーンショット 2019-01-21 9.12.14.png

成功です

演習

$ rails g controller Foo bar baz
$ rails destroy controller Foo bar baz

クリア!

tatic_pages_controller_test.rb
test "should get home" do   # テストの名前
  get static_pages_home_url # GETリクエストをhomeアクションに対して発行 (=送信) した時
  assert_response :success  # (期待)レスポンスに200が帰ってくる
end

チュートリアル通りです

3.3.2 Red

  test "should get about" do
    get static_pages_about_url
    assert_response :success
  end

書けた☺️

$ rails test
3 tests, 2 assertions, 0 failures, 1 errors, 0 skips

エラー😱

3.3.3 Green

テストがめでたく失敗した (red) ので、今度はこのテストのエラーメッセージを頼りにテストがパスする (green) ようにコードを書くことで、Aboutページを実装します。

😆

失敗したテストのエラーメッセージを詳しく見ていく

チュートリアルのまま
https://railstutorial.jp/chapters/static_pages?version=4.2#sec-green

 1) Error:
StaticPagesControllerTest#test_should_get_about:
NameError: undefined local variable or method `static_pages_about_url'
 for #<StaticPagesControllerTest:0x00007fa3b0218760>
    test/controllers/static_pages_controller_test.rb:15:in `block in <class:StaticPagesControllerTest>'
NameError: undefined local variable or method `static_pages_about_url'

直訳「`static_pages_about_url'ってなんやそれ? そんなもの知らないよ」
チュートリアル訳「AboutページへのURLが見つからない」

root書き方

こういう書き方の方が自分にはわかりやすいんだけど初学者用の書き方なのか?

gets 'static/pages' => 'about'

about.html.erbを作成する

app/views/static_pagesで「新しいファイル」をクリックしようとしましたがチュートリアルではもっとかっこいい方法をとるそうです

$ touch app/views/static_pages/about.html.erb

3.4 少しだけ動的なページ

チュートリアルそのまま
https://railstutorial.jp/chapters/static_pages?version=4.2#sec-slightly_dynamic_pages

3.4.2 タイトルを追加する (Green)

<title>ページ名 | Ruby on Rails Tutorial Sample App</title>

titleが追加されたのでテストコードでのエラーがなくなる予定が、エラーが出る

1) Failure:
StaticPagesControllerTest#test_should_get_about 
[/Users/kuzunoryou/environment/sample_app/test/controllers/static_pages_controller_test.rb:19]:
<Home | Ruby on Rails Tutorial Sample App> expected but was
<About | Ruby on Rails Tutorial Sample App>..
Expected 0 to be >= 1.

  2) Failure:
StaticPagesControllerTest#test_should_get_help [/Users/kuzunoryou/environment/sample_app/test/controllers/static_pages_controller_test.rb:13]:
<Home | Ruby on Rails Tutorial Sample App> expected but was
<Help | Ruby on Rails Tutorial Sample App>..
Expected 0 to be >= 1.

テストコードを間違えていました
<Home | Ruby on Rails Tutorial Sample App>Homeの部分をそのまま貼り付けていました
それぞれAbout,Helpにテストコードの中身を書き換えて

3 runs, 6 assertions, 0 failures, 0 errors, 0 skips

チュートリアル演習

  def setup
    @base_title = "Ruby on Rails Tutorial Sample App"
  end

setupとは「各テストが実行される直前で実行されるメソッド」とのこと
なので、メソッド名を変えてsetとかにするとエラーになる

3.4.3 レイアウトと埋め込みRuby (Refactor)

チュートリアルそのまま
https://railstutorial.jp/chapters/static_pages?version=5.1#sec-layouts_and_embedded_ruby

$ mv layout_file app/views/layouts/application.html.erb

なるほど。このファイルがあるとこっち側のheadの中身が参照されるから前に移動させたんだな、と今更ながら理解した

home, help, aboutのファイルの

の中身と一番上の<% ~ %>だけを残して消す

3.4.4 ルーティングの設定 演習

static_pages_controller_test.rb
  test "should get root" do
    get root_url
    assert_response :success
  end
routes.rb
    root 'static_pages#home'

rootルーティングを追加したことで、root_urlというRailsヘルパーが使えるようになりました
FILL_INと記された部分を置き換えて、rootルーティングのテストを書いてみてください

何書いてあるかよくわからなかったがそのままの意味で「root_urlが使えるよ!」という意味だった

3.5 最後に

チュートリアルそのまま
https://railstutorial.jp/chapters/static_pages?version=5.1#sec-static_pages_conclusion

テストの流れを知るためにやったのだけれど色々ためになった気がします

読んでくれてありがとう!

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?