Ruby on Rails Tutorial (2)
現実トウヒ対応で残りを云々。
3.2 Our first tests
続きです。
help 向けの試験も追加らしい。以下を追加して
describe "Help page" do it "should have the content 'Help'" do visit '/static_pages/help' page.should have_content('Help') 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 Help page should have the content 'Help' Failure/Error: page.should have_content('Help') expected there to be content "Help" in "SampleApp\n\nStaticPages#help\nFind me in app/views/static_pages/help.html.erb\n\n\n" # ./spec/requests/static_pages_spec.rb:16:in `block (3 levels) in <top (required)>' Finished in 0.7617 seconds 2 examples, 1 failure Failed examples: rspec ./spec/requests/static_pages_spec.rb:14 # StaticPages Help page should have the content 'Help' Randomized with seed 13477
失敗確認。app/views/static_pages/help.html.erb を以下に修正。
<h1>Help</h1> <p> Get help on the Ruby on Rails Tutorial at the <a href="http://railstutorial.org/help">Rails Tutorial help page</a>. To get help on this sample app, see the <a href="http://railstutorial.org/book">Rails Tutorial book</a>. </p>
で、試験再度実行。
$ bundle exec rspec spec/requests/static_pages_spec.rb Rack::File headers parameter replaces cache_control after Rack 1.5. .. Finished in 0.49262 seconds 2 examples, 0 failures Randomized with seed 53364
合格。つうかこんな動作確認も可能なんですね。知らなんだ。
次は About というページを追加する模様。まず試験から追加。
describe "About page" do it "should have the content 'About Us'" do visit '/static_pages/about' page.should have_content('About Us') end end
試験は当り前なんですが red になります。以下。
1) StaticPages About page should have the content 'About Us' Failure/Error: visit '/static_pages/about' ActionController::RoutingError: No route matches [GET] "/static_pages/about" # ./spec/requests/static_pages_spec.rb:23:in `block (3 levels) in <top (required)>'
まず routing させてくれ、と。config/routes.rb に以下を追加して
get "static_pages/about"
再試験。今度は以下とのこと。
1) StaticPages About page should have the content 'About Us' Failure/Error: visit '/static_pages/about' AbstractController::ActionNotFound: The action 'about' could not be found for StaticPagesController # ./spec/requests/static_pages_spec.rb:23:in `block (3 levels) in <top (required)>'
追加してないので action は無いですわな。順にやってるのでテキストに沿ってみます。app/controllers/static_pages_controller.rb にアクション追加。
def about end
再試験。view が無いので叱られるはず。
1) StaticPages About page should have the content 'About Us' Failure/Error: visit '/static_pages/about' ActionView::MissingTemplate: Missing template static_pages/about, application/about with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}. Searched in: * "/home/rms/OLDHome/rms/Documents/rails_proj/sample_app/app/views" # ./spec/requests/static_pages_spec.rb:23:in `block (3 levels) in <top (required)>'
はいその通りで。app/views/static_pages/about.html.erb を追加。
<h1>About Us</h1> <p> The <a href="http://railstutorial.org/">Ruby on Rails Tutorial</a> is a project to make a book and screencasts to teach web development with <a href="http://rubyonrails.org/">Ruby on Rails</a>. This is the sample application for the tutorial. </p>
これで試験 green なのかな。
3.3 Slightly dynamic pages
真面目にトレイスするのは面倒なのでズルします (を
とりあえず試験を以下に変更する模様。
require 'spec_helper' describe "Static pages" do describe "Home page" do it "should have the h1 'Sample App'" do visit '/static_pages/home' page.should have_selector('h1', :text => 'Sample App') end it "should have the title 'Home'" do visit '/static_pages/home' page.should have_selector('title', :text => "Ruby on Rails Tutorial Sample App | Home") end end describe "Help page" do it "should have the h1 'Help'" do visit '/static_pages/help' page.should have_selector('h1', :text => 'Help') end it "should have the title 'Help'" do visit '/static_pages/help' page.should have_selector('title', :text => "Ruby on Rails Tutorial Sample App | Help") end end describe "About page" do it "should have the h1 'About Us'" do visit '/static_pages/about' page.should have_selector('h1', :text => 'About Us') end it "should have the title 'About Us'" do visit '/static_pages/about' page.should have_selector('title', :text => "Ruby on Rails Tutorial Sample App | About Us") end end end
h1 なソレおよびタイトルが微妙に異なる、という形らしい。最初は app/views/layouts/application.html.erb をリネイムしてそれぞれがヘッダから記述という形を取っています。盛り込んで試験実行は当り前に red ですね。
解としては title の一部に yield な記述を付けて云々らしい。
まず、app/views/layouts/application.html.erb が以下。
<!DOCTYPE html> <html> <head> <title>Ruby on Rails Tutorial Sample App | <%= yield(:title) %></title> <%= stylesheet_link_tag "application", :media => "all" %> <%= javascript_include_tag "application" %> <%= csrf_meta_tags %> </head> <body> <%= yield %> </body> </html>
で、title なソレを云々する形の static_pages な view が以下。
<% provide(:title, 'Home') %> <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>
home.html.erb のみ、ってことで。これで試験は green になるはず。なりました。
最後に
git add して終わってますね。ちょっと github とか heroku 云々もごにょごにょしておこうかな。
$ git add . $ git commit -m "Finish static pages" $ git checkout master $ git merge --no-ff static-pages
とりあえず Github 方面から。リポジトリを作って以下。
$ git remote add origin https://github.com/yamanetoshi/RubyonRailsTutorial3.git $ git push -u origin master
パスワード忘れてて数度リトライするなどorz
heroku
アカウントはロックされてなかった (ロックアウトされたことあり)。
gem 入れます。
$ gem install heroku $ rbenv rehash
で、heroku login ですか。あら
! The 'heroku' gem has been deprecated and replaced with the Heroku Toolbelt, download and install from https://toolbelt.heroku.com.
って何でしょ。開いてみたら
wget -qO- https://toolbelt.heroku.com/install-ubuntu.sh | sh
てのがありますね。スクリプト取得してみます。中身が以下。
echo "This script requires superuser access to install apt packages." echo "You will be prompted for your password by sudo." # clear any previous sudo permission sudo -k # run inside sudo sudo sh <<SCRIPT # add heroku repository to apt echo "deb http://toolbelt.heroku.com/ubuntu ./" > /etc/apt/sources.list.d/heroku.list # install heroku's release key for package verification wget -O- https://toolbelt.heroku.com/apt/release.key | apt-key add - # update your sources apt-get update # install the toolbelt apt-get install -y heroku-toolbelt SCRIPT
なんか面倒いのでこのままヤります。
$ heroku create Creating hidden-refuge-5467... done, stack is cedar http://hidden-refuge-5467.herokuapp.com/ | git@heroku.com:hidden-refuge-5467.git Git remote heroku added
あるいは .git/config に heroku な remote 追加されてますね。push してみます。
$ git push heroku master
あ、鍵の更新してなさげorz
heroku keys:add しなさい、とのこと。むむ。で、リトライしてみると push できました。pg も導入している様子です。どうやら launch できたみたい。よく考えてみるに RoR を heroku で云々って初めてかも。
一応 static_pages 配下なコンテンツも出力されること、確認できてます。