Ruby on Rails Tutorial (1)
朝練。Chapter 2 はトバして 3 から。今回は以下なカンジでプロジェクト作成。
$ rails new sample_app --skip-test-unit $ cd sample_app
Test::Unit は使わないとのこと。Rspec を云々とありますね。つうか rails new で bundle install までヤッちゃうのってどうなのだろうか。
で、Gemfile を以下に、とのこと。
source 'https://rubygems.org' gem 'rails', '3.2.11' group :development, :test do gem 'sqlite3', '1.3.5' gem 'rspec-rails', '2.11.0' end # Gems used only for assets and not required # in production environments by default. group :assets do gem 'sass-rails', '3.2.5' gem 'coffee-rails', '3.2.2' gem 'uglifier', '1.2.3' end gem 'jquery-rails', '2.0.2' group :test do gem 'capybara', '1.1.2' end group :production do gem 'pg', '0.12.2' end
で、再度 bundle install 実行。
$ bundle install --without production
そして rspec な処理をして git init せよ、とのこと。
$ rails generate rspec:install
あら、なんかエラーになりました。
Could not find a JavaScript runtime. See https://github.com/sstephenson/execjs for a list of available runtimes. (ExecJS::RuntimeUnavailable)
これ、Node.js 入れれってことなのかな。Gemfile に以下を追加して
gem 'execjs'
再度 bundle install して、も駄目だったので gem install しちゃえ。
$ gem install execjs Successfully installed execjs-1.4.0 1 gem installed $
でリトライするも再現。ええと
によると
- gem i therubyracer
- Gemfile に以下を追加
- gem 'execjs'
- gem 'therubyracer'
とのことで盛り込んでリトライ。
$ rails generate rspec:install create .rspec create spec create spec/spec_helper.rb $
すばらです。現時点での Gemfile を以下に。
source 'https://rubygems.org' gem 'rails', '3.2.11' # Bundle edge Rails instead: # gem 'rails', :git => 'git://github.com/rails/rails.git' group :development, :test do gem 'sqlite3', '1.3.5' gem 'rspec-rails', '2.11.0' end # Gems used only for assets and not required # in production environments by default. group :assets do gem 'sass-rails', '3.2.5' gem 'coffee-rails', '3.2.2' gem 'uglifier', '1.2.3' end gem 'jquery-rails', '2.0.2' gem 'execjs' gem 'therubyracer' group :test do gem 'capybara', '1.1.2' end group :production do gem 'pg', '0.12.2' end
あ、ここで git init するのか。
$ git init $ git add . $ git commit -m 'initial commit'
.gitignore に情報追加が云々てどっかで見た気がするんですがとりあえずスルー。README.rdoc を開いて以下に書き換えて
# Ruby on Rails Tutorial: sample application This is the sample application for [*Ruby on Rails Tutorial: Learn Rails by Example*](http://railstutorial.org/) by [Michael Hartl](http://michaelhartl.com/).
git で云々
$ git mv README.rdoc README.md $ git commit -a -m "Improve the README"
Github が云々とか Heroku 云々は別途で。
3.1 Static Pages
public/ 配下ではなくて controller と view を使って静的なページを云々、なのか。この項はどちらかというと試験云々がアレなのかな。
とりあえず詳細スルーで実装のみ済ませておきます。
つうかブランチ作って云々してるけどいつマージするんかな。
$ git checkout -b static-pages
あるいは再度 rspec:install を generate してますな。
$ rails generate rspec:install identical .rspec exist spec identical spec/spec_helper.rb $
スルー。次で StaticPages なソレを作成。
$ rails generate controller StaticPages home help --no-test-framework
generate で作成したソレは destroy で rollback できますよ、とか出てるな。で、とりあえず config/routes.rb を云々、とあります。現時点で有効になっているのは以下。
SampleApp::Application.routes.draw do get "static_pages/home" get "static_pages/help"
これは今しがた実行した rails generate で作られたものですね。app/controller/static_pages_controller.rb もテキストの通り
class StaticPagesController < ApplicationController def home end def help end end
で、app/views/static_pages/{home,help}.html.erb も同じ状態ですね。この状態でブランチにコミットを作ります。
$ git add . $ git commit -m "Add a StaticPages controller"
3.2 Our first tests
時間切れになりそう。"Red, Green, Refactor" が云々とありますね。とりあえず試験を作る模様。あまり本文は読まずに進めます。時間無いし。
$ rails generate integration_test static_pages invoke rspec create spec/requests/static_pages_spec.rb
自動生成された中身はどうなっているかというと
$ cat spec/requests/static_pages_spec.rb require 'spec_helper' describe "StaticPages" do describe "GET /static_pages" do it "works! (now write some real specs)" do # Run the generator again with the --webrat flag if you want to use webrat methods/matchers get static_pages_index_path response.status.should be(200) end end end
これはテキストの通りに書き換えが必要らしい。
require 'spec_helper' describe "StaticPages" do describe "Home page" do it "should have the content 'Sample App'" do visit '/static_pages/home' page.should have_content('Sample App') end end end
で、試験開始。
$ bundle exec rspec spec/requests/static_pages_spec.rb Rack::File headers parameter replaces cache_control after Rack 1.5. F Failures: 1) StaticPages Home page should have the content 'Sample App' Failure/Error: page.should have_content('Sample App') expected there to be content "Sample App" in "SampleApp\n\nStaticPages#home\nFind me in app/views/static_pages/home.html.erb\n\n\n" # ./spec/requests/static_pages_spec.rb:8:in `block (3 levels) in <top (required)>' Finished in 0.7772 seconds 1 example, 1 failure Failed examples: rspec ./spec/requests/static_pages_spec.rb:6 # StaticPages Home page should have the content 'Sample App' Randomized with seed 40041
失敗。test first は失敗させないと、な模様。で、デフォなソレではなくて以下にせよ、とありますね。
<h1>Sample App</h1> <p> This is the home page for the <a href="http://railstutorial.org/">Ruby on Rails Tutorial</a> sample application. </p>
これで試験リトライ。
$ bundle exec rspec spec/requests/static_pages_spec.rb Rack::File headers parameter replaces cache_control after Rack 1.5. . Finished in 0.46838 seconds 1 example, 0 failures Randomized with seed 47545
ここでいったん止めます。時間切れ。