ruby on rails3 tutorial chapter3

23
+ RUBY ON RAILS 3 Tutorial ををををををををを Chapter 3 ををを 2011-10-12

Upload: sea-mountain

Post on 26-May-2015

1.712 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Ruby on Rails3 Tutorial Chapter3

+

RUBY ON RAILS 3 Tutorial を日本語訳してみたChapter 3の途中

2011-10-12

Page 2: Ruby on Rails3 Tutorial Chapter3

目次

Chapter1 Rails導入からデプロイ

Chapter2 デモアプリ (scaffold使用 )

Chapter3 Webアプリケーション

Chapter4 Rails風 Ruby

Chapter5 スタイルを追加する

Chapter6 User Modelと View その 1

Chapter7 User Modelと View その 2

2

Page 3: Ruby on Rails3 Tutorial Chapter3

目次

Chapter8 ユーザ登録

Chapter9 ログイン・ログアウト

Chapter10 ユーザデータの更新・編集・追加

Chapter11 ミニブログ ( ツイート )

Chapter12 ユーザのフォロー

3

Page 4: Ruby on Rails3 Tutorial Chapter3

+Chapter3 Mostly Static Pages

scaffoldを用いない、Webサービスの作成

デフォルトでは Test::Unitが使われる為、 -Tをつけて testディレクトリ自動生成させないようにする

Rspecを使ってテストを書くため、現段階では testディレクトリは必要ない

4

$ rails new sample_app –T

Page 5: Ruby on Rails3 Tutorial Chapter3

+Chapter3 Mostly Static Pages

Gemfile書き換え

gemインストール

rspec用のコマンド実行

5

$ bundle install

$ rails generate rspec:install

gem 'rails', '3.1.1’gem 'sqlite3'

group :development do  gem 'rspec-rails', '2.0.1'end

group :test do  gem 'rspec', '2.0.1'  gem 'webrat', '0.7.1'end

Page 6: Ruby on Rails3 Tutorial Chapter3

+Chapter3 Mostly Static Pages

Gitにリポジトリを登録して、 Readmeを作成、 push

6

$ git init$ git add .$ git commit –m “Initial commit”

$ git mv README README.markdown$ git commit –a –m “Improved the README”

$ git remote add origin [email protected]:<name>/sample_app.git$ git push origin master

Page 7: Ruby on Rails3 Tutorial Chapter3

+3.1.2. Static Pages with Rails

Pages controller作成 rails generate controller Pages home contact

config/routes.rbに追加get “pages/home”get “pages/contact”

7

Page 8: Ruby on Rails3 Tutorial Chapter3

+3.2.1 Testing tools

Rspecを利用する

Autotest

OS Xなら Growlを入れたほうがいい gemの autotest-fseventと autotest-growlを入れる

8

$ gem install autotest –v 4.4.6$ gem install autotest-rails-pure –v 4.1.2

$ gem install autotest-fsevent –v 0.2.4$ gem install autotest-growl –v 0.2.9

Page 9: Ruby on Rails3 Tutorial Chapter3

+3.2.1 Testing tools

Mac OS Xの場合 アプリケーションのルートに .autotest作成

それ以外 .autotest

私の環境 (OS X Snow Leopard)では autotestコマンドでエラーがでたため、下の方にした

9

require ‘autotest/growl’require ‘autotest/fsevent’

require ‘autotest-growl’require ‘autotest-fsevent’

Page 10: Ruby on Rails3 Tutorial Chapter3

+3.2.1 Testing tools

WindowsユーザはWindows向け Growlいれてみるといいかも

Linux・Windowsで Growlのような通知させたい場合は参考に http://fredschoeneman.posterous.com/pimp-your-autotest-

notification

autotestが実行できて、 Growlでの通知があると便利!

(Rubyマークが信号機みたいに見える… )

10

Page 11: Ruby on Rails3 Tutorial Chapter3

+3.2.2 TDD: Red, Green, Refactor

TDDにおいては、まずテストを失敗させてから ( 赤 )

  →それが通るよう ( 青 ) にコードを書く

すでに書いてしまったコードに対してテストを書くのではない

11

Page 12: Ruby on Rails3 Tutorial Chapter3

+3.2.2 TDD: Red, Green, Refactor

Pagesコントローラ作成

自動で specファイルも作られる

テスト作成 spec/controllers/pages_controller_spec.rb

12

$ rails generate controller Pages

require 'spec_helper'

describe PagesController do  describe "GET 'home'" do    it "should be successful" do      get "home"      response.should be_success    end  endend

Page 13: Ruby on Rails3 Tutorial Chapter3

+Box 3.2 HTTP response codes

HTTPリクエストを送ると、 HTTP statusが返って来る 200: 成功 301: リソースが別の場所に移動してしまっている状態

curlがインストール済みならコマンドラインから見れる

RSpecで response.should be_successと書くときは、 status codeが 200のレスポンスであるということ

13

$ curl --head www.google.comHTTP/1.1 302 Found…省略…

Page 14: Ruby on Rails3 Tutorial Chapter3

+3.2.2 TDD: Red, Green, Refactor

rspec spec/でテストを実行出来る

この段階でテストが失敗するなら

1.rake db:migrate

2.bundle exec rspec spec/

それでもだめなら再インストール

1.gem uninstall rspec rspec-rails

2.bundle install

14

Page 15: Ruby on Rails3 Tutorial Chapter3

+3.2.2 TDD: Red, Green, Refactor

RSpecの実行は rake specでもいい rspec spec/ →stack traceを表示してくれる rspec spec →してくれない

RVMがおかしくなったら一旦使っていた gemset削除して、 gemの再インストールしてみる

15

Page 16: Ruby on Rails3 Tutorial Chapter3

+3.2.2 TDD: Red, Green, Refactor

Spork(https://github.com/timcharper/spork) RSpecは毎回 Rails環境を読み込んでいるため遅い

Sporkは1 度環境を読み込むだけ

Autotestと一緒に使いやすい

16

Page 17: Ruby on Rails3 Tutorial Chapter3

+3.2.2 TDD: Red, Green, Refactor

Gemfile変更〜 bundle install

17

Page 18: Ruby on Rails3 Tutorial Chapter3

+3.2.2 TDD: Red, Green, Refactor

sporkの設定

spec/spec_helper.rb変更require “spork”と Spork節が2 つ増えている

18

$ spork --bootstrap

Page 19: Ruby on Rails3 Tutorial Chapter3

+3.2.2 TDD: Red, Green, Refactor

19

require 'spork'

Spork.prefork do   ENV["RAILS_ENV"] ||= 'test'  require File.expand_path("../../config/environment", __FILE__)  require 'rspec/rails'

  Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

  RSpec.configure do |config|     config.mock_with :rspec

    config.fixture_path = "#{::Rails.root}/spec/fixtures"

    config.use_transactional_fixtures = true  endend

Spork.each_run doend 

Page 20: Ruby on Rails3 Tutorial Chapter3

+3.2.2 TDD: Red, Green, Refactor

bundle exec sporkで起動

sporkを使わない場合と使う場合比較

合っているはずのテストが通らないときは、 Sporkを ctrl + cで止めて再起動すべし

20

$ time rspec spec/ …Finished in 0.29592 seconds5 examples, 0 failures, 3 pending real 0m11.099suser 0m7.833ssys 0m1.632s 

time rspec --drb spec/..***Finished in 0.21522 seconds5 examples, 0 failures, 3 pending

real 0m1.075suser 0m0.368ssys 0m0.125s

Page 21: Ruby on Rails3 Tutorial Chapter3

+この後の3 章の内容

viewの titleが正しいかテストする

application.html.erbに<%= @title %>を埋め込んでしまうアクション内に@title=hogeとページ毎のタイトルを代入しておく

@titleで埋め込んだ文字列と想定する文字列を比較テスト

21

Page 22: Ruby on Rails3 Tutorial Chapter3

+Chapter4 Rails-Flavored Ruby

4章は大体こんな話

Railsで Rubyのコードを使う主に Rubyの文法の話

rails consoleで irbのように色々やってみる数字の計算・文字列代入等々

22

Page 23: Ruby on Rails3 Tutorial Chapter3

+次回予告

3 章はかなりキツイ… しかし残り時間的に…

もうちょっと内容すっ飛ばすか、発表しない章があるかも…

23