LoginSignup
2
2

More than 5 years have passed since last update.

Rails チュートリアルを実際にやってみた

Posted at

第3章 ほぼ静的なページの作成 | Rails チュートリアル
http://railstutorial.jp/chapters/static-pages?version=4.0#top

こちらを参考に実装し、躓いた点を列挙してみる。
つまずかなかったところは普通に飛ばしてます。

環境

  • Ruby 2.2.2
  • Rails 4.2.1
sampleapp作成
$ rails new sample_app --skip-test-unit
$ cd sample_app
Gemfile
source 'https://rubygems.org'
ruby '2.0.0'
#ruby-gemset=railstutorial_rails_4_0

gem 'rails', '4.0.5'

group :development, :test do
  gem 'sqlite3', '1.3.8'
  gem 'rspec-rails', '2.13.1'
end

group :test do
  gem 'selenium-webdriver', '2.35.1'
  gem 'capybara', '2.1.0'
end

gem 'sass-rails', '4.0.5'
gem 'uglifier', '2.1.1'
gem 'coffee-rails', '4.0.1'
gem 'jquery-rails', '3.0.4'
gem 'turbolinks', '1.1.1'
gem 'jbuilder', '1.0.2'

group :doc do
  gem 'sdoc', '0.3.20', require: false
end

group :production do
  gem 'pg', '0.15.1'
  gem 'rails_12factor', '0.0.2'
end
bundle_install
$ bundle install --without production
$ bundle update

つまづき!

config/initializers/secret_token.rb なんてない。。と思ったので新規作成。。

secret_token.rb
require 'securerandom'

def secure_token
  token_file = Rails.root.join('.secret')
  if File.exist?(token_file)
    # Use the existing token.
    File.read(token_file).chomp
  else
    # Generate a new token and store it in token_file.
    token = SecureRandom.hex(64)
    File.write(token_file, token)
    token
  end
end

SampleApp::Application.config.secret_key_base = secure_token
rspecの初期化
$ rails generate rspec:install

ここから本題

ブランチ作成
$ git checkout -b static-pages
コントローラー作成
$ rails generate controller StaticPages home help --no-test-framework

テスト駆動開発

テストスクリプトファイルの作成
$ rails generate integration_test static_pages
spec/requests/static_pages_spec.rb
require 'spec_helper'

describe "Static pages" do

  describe "Home page" do

    it "should have the content 'Sample App'" do
      visit '/static_pages/home'
      expect(page).to have_content('Sample App')
    end
  end
end

Capybaraの設定追加

spec/spec_helper.rb
# This file is copied to spec/ when you run 'rails generate rspec:install'
.
.
.
RSpec.configure do |config|
  .
  .
  .
  config.include Capybara::DSL
end
テスト実行
$ bundle exec rspec spec/requests/static_pages_spec.rb

つまづき!

なんかエラーでた

/.../sample_app/spec/spec_helper.rb:87:in `block in <top (required)>': uninitialized constant Capybara (NameError)

rails_spec.rbに書くのがいいらしい

spec/spec_helper.rb
RSpec.configure do |config|
...
  .
  .
  .
  config.include Capybara::DSL
end

テスト実行は出来たけど、テストでエラーが発生
```bash
Failures:

1) StaticPages GET /static_pages should have the content 'Sample App'
Failure/Error: expect(page).to have_content('Sample App')
expected #has_content?("Sample App") to return true, got false
# ./spec/requests/static_pages_spec.rb:7:in `block (3 levels) in '

Finished in 0.32109 seconds (files took 6.05 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/requests/static_pages_spec.rb:5 # StaticPages GET /static_pages should have the content 'Sample App'
```
そもそも生成されたhtmlに'Sample App'が無いっていうエラー。

app/views/static_pages/home.html.erb
<h1>Sample App</h1>
<p>
    This is the home page for the
  <a href="http://railstutorial.jp/">Ruby on Rails Tutorial</a>
    sample application.
</p>

で解消。

ページの追加

チュートリアルでは直にコーディングしているが、あえてrails generateで作成してみる

$ rails generate controller StaticPa
ges about --no-test-framework
    conflict  app/controllers/static_pages_controller.rb
Overwrite /.../sample_app/app/controllers/static_pages_controller.rb? (enter "h" for help) [Ynaqdh] 

コンフリクトしたのでnをおしてみる

        skip  app/controllers/static_pages_controller.rb
       route  get 'static_pages/about'
      invoke  erb
       exist    app/views/static_pages
      create    app/views/static_pages/about.html.erb
      invoke  helper
   identical    app/helpers/static_pages_helper.rb
      invoke  assets
      invoke    coffee
   identical      app/assets/javascripts/static_pages.coffee
      invoke    scss
   identical      app/assets/stylesheets/static_pages.scss

コンフリクトしているファイル以外が生成されるみたい。

about.html.erb
<h1>About Us</h1>
<p>Find me in app/views/static_pages/about.html.erb</p>

に変更してテスト

$ bundle exec rspec spec/requests/static_pages_spec.rb
Finished in 0.34315 seconds (files took 6.03 seconds to load)
2 examples, 0 failures

成功!

2
2
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
2