SlideShare a Scribd company logo
RUBY ON RAILS 3 Tutorial  を日本語訳してみた Chapter 3 の途中 2011-10-12
目次 Chapter1 Rails 導入からデプロイ Chapter2  デモアプリ (scaffold 使用 ) Chapter3 Web アプリケーション Chapter4 Rails 風 Ruby Chapter5  スタイルを追加する Chapter6 User Model と View  その 1 Chapter7 User Model と View  その 2
目次 Chapter8  ユーザ登録 Chapter9  ログイン・ログアウト Chapter10  ユーザデータの更新・編集・追加 Chapter11  ミニブログ ( ツイート ) Chapter12  ユーザのフォロー
Chapter3 Mostly Static Pages scaffold を用いない、 Web サービスの作成 デフォルトでは Test::Unit が使われる為、 -T をつけて test ディレクトリ自動生成させないようにする Rspec を使ってテストを書くため、現段階では test ディレクトリは必要ない $ rails new sample_app  –T
Chapter3 Mostly Static Pages Gemfile 書き換え gem インストール rspec 用のコマンド実行 $ 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
Chapter3 Mostly Static Pages Git にリポジトリを登録して、 Readme を作成、 push $ 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 git@github.com:<name>/sample_app.git $ git push origin master
3.1.2. Static Pages with Rails Pages controller 作成  rails generate controller Pages home contact config/routes.rb に追加 get “pages/home” get “pages/contact”
3.2.1 Testing tools Rspec を利用する Autotest OS X なら Growl を入れたほうがいい gem の autotest-fsevent と autotest-growl を入れる $ 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
3.2.1 Testing tools Mac OS X の場合 アプリケーションのルートに .autotest 作成 それ以外  .autotest 私の環境 (OS X Snow Leopard) では autotest コマンドでエラーがでたため、下の方にした require ‘autotest/growl’ require ‘autotest/fsevent’ require ‘ autotest-growl ’ require ‘ autotest-fsevent ’
3.2.1 Testing tools Windows ユーザは Windows 向け Growl いれてみるといいかも Linux ・ Windows で Growl のような通知させたい場合は参考に http://fredschoeneman.posterous.com/pimp-your-autotest-notification autotest が実行できて、 Growl での通知があると便利! (Ruby マークが信号機みたいに見える… )
3.2.2 TDD: Red, Green, Refactor TDD においては、まずテストを失敗させてから ( 赤 )   ->それが通るよう ( 青 ) にコードを書く すでに書いてしまったコードに対してテストを書くのではない
3.2.2 TDD: Red, Green, Refactor Pages コントローラ作成 自動で spec ファイルも作られる テスト作成  spec/controllers/pages_controller_spec.rb $ rails generate controller Pages require   ' spec_helper ' describe   PagesController   do    describe   &quot; GET 'home' &quot;   do      it   &quot; should be successful &quot;   do        get   &quot; home &quot;        response .should be_success      end    end end
Box 3.2 HTTP response codes HTTP リクエストを送ると、 HTTP status が返って来る 200:  成功 301:  リソースが別の場所に移動してしまっている状態 curl がインストール済みならコマンドラインから見れる RSpec で response.should be_success と書くときは、 status code が 200 のレスポンスであるということ $ curl --head www.google.com HTTP/1.1 302 Found … 省略…
3.2.2 TDD: Red, Green, Refactor rspec spec/ でテストを実行出来る この段階でテストが失敗するなら rake db:migrate bundle exec rspec spec/ それでもだめなら再インストール gem uninstall rspec rspec-rails bundle install
3.2.2 TDD: Red, Green, Refactor RSpec の実行は rake spec でもいい rspec spec/  ->stack trace を表示してくれる rspec spec  -> してくれない RVM がおかしくなったら 一旦使っていた gemset 削除して、 gem の再インストールしてみる
3.2.2 TDD: Red, Green, Refactor Spork(https://github.com/timcharper/spork) RSpec は毎回 Rails 環境を読み込んでいるため遅い Spork は 1 度環境を読み込むだけ Autotest と一緒に使いやすい
3.2.2 TDD: Red, Green, Refactor Gemfile 変更〜 bundle install
3.2.2 TDD: Red, Green, Refactor spork の設定 spec/spec_helper.rb 変更 require “spork” と Spork 節が 2 つ増えている $ spork --bootstrap
3.2.2 TDD: Red, Green, Refactor require   ' spork ' Spork .prefork   do     ENV [ &quot; RAILS_ENV &quot; ] ||=   ' test '    require   File .expand_path( &quot; ../../config/environment &quot; ,   __FILE__ )    require   ' rspec/rails '    Dir [ Rails .root.join( &quot; spec/support/**/*.rb &quot; )].each {| f |  require  f}    RSpec .configure   do  | config |      config.mock_with   :rspec     config.fixture_path =   &quot;#{ :: Rails .root } /spec/fixtures &quot;     config.use_transactional_fixtures =   true    end end Spork .each_run   do end  
3.2.2 TDD: Red, Green, Refactor bundle exec spork で起動 spork を使わない場合と使う場合比較 合っているはずのテストが通らないときは、 Spork を ctrl + c で止めて再起動すべし $ time rspec spec/  … Finished in 0.29592 seconds 5 examples, 0 failures, 3 pending   real 0m11.099s user 0m7.833s sys 0m1.632s   time rspec --drb spec/ ..*** Finished in 0.21522 seconds 5 examples, 0 failures, 3 pending real 0m1.075s user 0m0.368s sys 0m0.125s
この後の 3 章の内容 view の title が正しいかテストする application.html.erb に <%= @title %> を埋め込んでしまう アクション内に @title=hoge とページ毎のタイトルを代入しておく @title で埋め込んだ文字列と想定する文字列を比較テスト
Chapter4 Rails-Flavored Ruby 4章は大体こんな話 Rails で Ruby のコードを使う 主に Ruby の文法の話 rails console で irb のように色々やってみる 数字の計算・文字列代入等々
次回予告 3 章はかなりキツイ… しかし残り時間的に… もうちょっと内容すっ飛ばすか、発表しない章があるかも…

More Related Content

PPT
Ruby on Rails3 Tutorial Chapter2
PPTX
Spring Rooで作るGWTアプリケーション
PDF
REST with Spring Boot #jqfk
PPTX
Appsody でnodejsのアプリを立ち上げよう!
KEY
Rails and twitter #twtr_hack
 
PPTX
Zabbix による ms sql監視 ~データベースモニタリング~ odbc
PDF
Niigata.pm #1
PPTX
【 Zabbix 2.0 】zabbix 2.0による簡単 MySQL 監視 #Zabbix
Ruby on Rails3 Tutorial Chapter2
Spring Rooで作るGWTアプリケーション
REST with Spring Boot #jqfk
Appsody でnodejsのアプリを立ち上げよう!
Rails and twitter #twtr_hack
 
Zabbix による ms sql監視 ~データベースモニタリング~ odbc
Niigata.pm #1
【 Zabbix 2.0 】zabbix 2.0による簡単 MySQL 監視 #Zabbix

What's hot (20)

PPTX
120517 revert tomcat7
PPTX
【 Zabbix 2.2 】zabbix update 2.0 to 2.2
PPTX
【 Zabbix 2.1 】 zabbix 2.2のVM監視機能評価 #Zabbix #自宅ラック勉強会
PDF
Laravel5を使って開発してみた
PDF
scala-kaigi1-sbt
PDF
Spring bootでweb 基本編
ODP
Nseg20120929
PDF
laravel x モバイルアプリ
PDF
AlibabaCloudではじめるKubernetes
KEY
Webサーバ勉強会 LT資料
PDF
最近のRails事情 - 4.1!
PDF
Capistranoで自動デプロイ
PDF
NDS#31
PDF
Apache Auroraの始めかた
PPTX
【Zabbix 2.1】zabbix2.1.6→2.1.7 の変更点
PPTX
開発環境をVagrantからdockerに移行してみた
PPT
Ruby on Rails Tutorial Chapter5-7
PDF
おれおれブログシステムにServiceWorkerを導入してみた #serviceworker
PPTX
明日から始める Chef 入門 #bpstudy
KEY
Bundler kanazawa.rb meetup #2 2012/09/19
120517 revert tomcat7
【 Zabbix 2.2 】zabbix update 2.0 to 2.2
【 Zabbix 2.1 】 zabbix 2.2のVM監視機能評価 #Zabbix #自宅ラック勉強会
Laravel5を使って開発してみた
scala-kaigi1-sbt
Spring bootでweb 基本編
Nseg20120929
laravel x モバイルアプリ
AlibabaCloudではじめるKubernetes
Webサーバ勉強会 LT資料
最近のRails事情 - 4.1!
Capistranoで自動デプロイ
NDS#31
Apache Auroraの始めかた
【Zabbix 2.1】zabbix2.1.6→2.1.7 の変更点
開発環境をVagrantからdockerに移行してみた
Ruby on Rails Tutorial Chapter5-7
おれおれブログシステムにServiceWorkerを導入してみた #serviceworker
明日から始める Chef 入門 #bpstudy
Bundler kanazawa.rb meetup #2 2012/09/19
Ad

Similar to Ruby on Rails3 Tutorial Chapter3 (20)

PDF
Nseg20120825
PPTX
実は怖くないDevOps
PDF
RでつくるWebアプリ~rApache編~
PDF
React Native GUIDE
PDF
Rails解説セミナー: リリースノート解説編
PPTX
13016 n分で作るtype scriptでnodejs
PDF
Ansible2.0と実用例
PDF
Web技術勉強会23回目
PPT
いまさら聞けないRake入門
PDF
Rails3.1rc4を試してみた
PDF
SDLoader SeasarCon 2009 Whire
PPT
Struts2を始めよう!
PDF
どこよりも早い Spring Boot 1.2 解説 #渋谷Java
PDF
第20回CloudStackユーザ会_ApacheCloudStack4.4新機能紹介
PDF
JavaScript.Next
PDF
Tokyo.R#16 wdkz
PDF
OpenStack Grizzly構築手順書
PPTX
AWSとAnsibleで実践!プロビジョニング入門‐Lamp+Laravel-
PDF
TDD勉強会キックオフ for Java
PDF
今だからこそ知りたい Docker Compose/Swarm 入門
Nseg20120825
実は怖くないDevOps
RでつくるWebアプリ~rApache編~
React Native GUIDE
Rails解説セミナー: リリースノート解説編
13016 n分で作るtype scriptでnodejs
Ansible2.0と実用例
Web技術勉強会23回目
いまさら聞けないRake入門
Rails3.1rc4を試してみた
SDLoader SeasarCon 2009 Whire
Struts2を始めよう!
どこよりも早い Spring Boot 1.2 解説 #渋谷Java
第20回CloudStackユーザ会_ApacheCloudStack4.4新機能紹介
JavaScript.Next
Tokyo.R#16 wdkz
OpenStack Grizzly構築手順書
AWSとAnsibleで実践!プロビジョニング入門‐Lamp+Laravel-
TDD勉強会キックオフ for Java
今だからこそ知りたい Docker Compose/Swarm 入門
Ad

Ruby on Rails3 Tutorial Chapter3

  • 1. RUBY ON RAILS 3 Tutorial を日本語訳してみた Chapter 3 の途中 2011-10-12
  • 2. 目次 Chapter1 Rails 導入からデプロイ Chapter2 デモアプリ (scaffold 使用 ) Chapter3 Web アプリケーション Chapter4 Rails 風 Ruby Chapter5 スタイルを追加する Chapter6 User Model と View その 1 Chapter7 User Model と View その 2
  • 3. 目次 Chapter8 ユーザ登録 Chapter9 ログイン・ログアウト Chapter10 ユーザデータの更新・編集・追加 Chapter11 ミニブログ ( ツイート ) Chapter12 ユーザのフォロー
  • 4. Chapter3 Mostly Static Pages scaffold を用いない、 Web サービスの作成 デフォルトでは Test::Unit が使われる為、 -T をつけて test ディレクトリ自動生成させないようにする Rspec を使ってテストを書くため、現段階では test ディレクトリは必要ない $ rails new sample_app –T
  • 5. Chapter3 Mostly Static Pages Gemfile 書き換え gem インストール rspec 用のコマンド実行 $ 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
  • 6. Chapter3 Mostly Static Pages Git にリポジトリを登録して、 Readme を作成、 push $ 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 git@github.com:<name>/sample_app.git $ git push origin master
  • 7. 3.1.2. Static Pages with Rails Pages controller 作成 rails generate controller Pages home contact config/routes.rb に追加 get “pages/home” get “pages/contact”
  • 8. 3.2.1 Testing tools Rspec を利用する Autotest OS X なら Growl を入れたほうがいい gem の autotest-fsevent と autotest-growl を入れる $ 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
  • 9. 3.2.1 Testing tools Mac OS X の場合 アプリケーションのルートに .autotest 作成 それ以外 .autotest 私の環境 (OS X Snow Leopard) では autotest コマンドでエラーがでたため、下の方にした require ‘autotest/growl’ require ‘autotest/fsevent’ require ‘ autotest-growl ’ require ‘ autotest-fsevent ’
  • 10. 3.2.1 Testing tools Windows ユーザは Windows 向け Growl いれてみるといいかも Linux ・ Windows で Growl のような通知させたい場合は参考に http://fredschoeneman.posterous.com/pimp-your-autotest-notification autotest が実行できて、 Growl での通知があると便利! (Ruby マークが信号機みたいに見える… )
  • 11. 3.2.2 TDD: Red, Green, Refactor TDD においては、まずテストを失敗させてから ( 赤 )   ->それが通るよう ( 青 ) にコードを書く すでに書いてしまったコードに対してテストを書くのではない
  • 12. 3.2.2 TDD: Red, Green, Refactor Pages コントローラ作成 自動で spec ファイルも作られる テスト作成 spec/controllers/pages_controller_spec.rb $ rails generate controller Pages require   ' spec_helper ' describe   PagesController   do    describe   &quot; GET 'home' &quot;   do      it   &quot; should be successful &quot;   do        get   &quot; home &quot;        response .should be_success      end    end end
  • 13. Box 3.2 HTTP response codes HTTP リクエストを送ると、 HTTP status が返って来る 200: 成功 301: リソースが別の場所に移動してしまっている状態 curl がインストール済みならコマンドラインから見れる RSpec で response.should be_success と書くときは、 status code が 200 のレスポンスであるということ $ curl --head www.google.com HTTP/1.1 302 Found … 省略…
  • 14. 3.2.2 TDD: Red, Green, Refactor rspec spec/ でテストを実行出来る この段階でテストが失敗するなら rake db:migrate bundle exec rspec spec/ それでもだめなら再インストール gem uninstall rspec rspec-rails bundle install
  • 15. 3.2.2 TDD: Red, Green, Refactor RSpec の実行は rake spec でもいい rspec spec/ ->stack trace を表示してくれる rspec spec -> してくれない RVM がおかしくなったら 一旦使っていた gemset 削除して、 gem の再インストールしてみる
  • 16. 3.2.2 TDD: Red, Green, Refactor Spork(https://github.com/timcharper/spork) RSpec は毎回 Rails 環境を読み込んでいるため遅い Spork は 1 度環境を読み込むだけ Autotest と一緒に使いやすい
  • 17. 3.2.2 TDD: Red, Green, Refactor Gemfile 変更〜 bundle install
  • 18. 3.2.2 TDD: Red, Green, Refactor spork の設定 spec/spec_helper.rb 変更 require “spork” と Spork 節が 2 つ増えている $ spork --bootstrap
  • 19. 3.2.2 TDD: Red, Green, Refactor require   ' spork ' Spork .prefork   do     ENV [ &quot; RAILS_ENV &quot; ] ||=   ' test '    require   File .expand_path( &quot; ../../config/environment &quot; ,   __FILE__ )    require   ' rspec/rails '    Dir [ Rails .root.join( &quot; spec/support/**/*.rb &quot; )].each {| f |  require  f}    RSpec .configure   do  | config |      config.mock_with   :rspec     config.fixture_path =   &quot;#{ :: Rails .root } /spec/fixtures &quot;     config.use_transactional_fixtures =   true    end end Spork .each_run   do end  
  • 20. 3.2.2 TDD: Red, Green, Refactor bundle exec spork で起動 spork を使わない場合と使う場合比較 合っているはずのテストが通らないときは、 Spork を ctrl + c で止めて再起動すべし $ time rspec spec/  … Finished in 0.29592 seconds 5 examples, 0 failures, 3 pending   real 0m11.099s user 0m7.833s sys 0m1.632s   time rspec --drb spec/ ..*** Finished in 0.21522 seconds 5 examples, 0 failures, 3 pending real 0m1.075s user 0m0.368s sys 0m0.125s
  • 21. この後の 3 章の内容 view の title が正しいかテストする application.html.erb に <%= @title %> を埋め込んでしまう アクション内に @title=hoge とページ毎のタイトルを代入しておく @title で埋め込んだ文字列と想定する文字列を比較テスト
  • 22. Chapter4 Rails-Flavored Ruby 4章は大体こんな話 Rails で Ruby のコードを使う 主に Ruby の文法の話 rails console で irb のように色々やってみる 数字の計算・文字列代入等々
  • 23. 次回予告 3 章はかなりキツイ… しかし残り時間的に… もうちょっと内容すっ飛ばすか、発表しない章があるかも…

Editor's Notes

  • #7: この後 3.1 では static なページに付いての説明があり、 index.html を自分の好きな物の表示に変えるには (route) など有りますが、省略
  • #14: http://w3g.jp/others/htaccess/redirect 301 Moved Permanently  とは、そのリソースが恒久的に別の場所へ移動したことを伝え、今後は移動先のリソースへと要求するように促します。移動先の URI は絶対 URI ( http://  からはじまる)で記述します。
  • #20: 画面の関係上、コメント削除 require を増やして、 Spork の部分を 2 箇所増えてる