139
Ruby on Rails 3 Tutorial Chapter 1 to 3 @yasulab 201193日土曜日

Rails Tutorial ch1-2

Embed Size (px)

DESCRIPTION

Ruby on Rails 3 Tutorial の chapter 1&2 の要約です。 by @yasulab http://flavors.me/yasulab

Citation preview

Page 1: Rails Tutorial ch1-2

Ruby on Rails 3 TutorialChapter 1 to 3

@yasulab

2011年9月3日土曜日

Page 2: Rails Tutorial ch1-2

Ruby on Rails 3 TutorialChapter 1 to 3

@yasulab

2011年9月3日土曜日

Page 3: Rails Tutorial ch1-2

Ruby on Rails 3 TutorialChapter 1 to 3

@yasulab

2

2011年9月3日土曜日

Page 4: Rails Tutorial ch1-2

内容• Ruby on Rails 3 Tutorial (Ch. 1to3)

• Web (Free)

• http://ruby.railstutorial.org/ruby-on-rails-tutorial-book

• 対象は初心者

• Ruby/Railsを知らないことが前提。

• 経験者は章末にあるExerciseをやってみるといいかも。

2011年9月3日土曜日

Page 5: Rails Tutorial ch1-2

本書の特徴• Rails 3にデフォルトで備わっていないものの、非常によく使われるツールを使った入門書。

• 例:Rspec, Git, GitHub, and Heroku

• Microblogの開発を題材としてRailsを解説。

• Test-Driven Development(TDD)で開発。

• Rspecがredからgreenになるまでテストコードと睨めっこします。

2011年9月3日土曜日

Page 6: Rails Tutorial ch1-2

進め方

• コードをひたすら追っていきます。

• 口頭でコードの動きを説明

• タイトルの横にある括弧内はセクション番号

• もしコードの動きがよく理解出来ない場合は、セクション番号から該当する章を読むと良い。

2011年9月3日土曜日

Page 7: Rails Tutorial ch1-2

進め方

• コードをひたすら追っていきます。

• 口頭でコードの動きを説明

• タイトルの横にある括弧内はセクション番号

• もしコードの動きがよく理解出来ない場合は、セクション番号から該当する章を読むと良い。

←タイトル

2011年9月3日土曜日

Page 8: Rails Tutorial ch1-2

基本方針

• 60minで全部理解するのは無理。諦めましょう。

• 1. 重要そうなところだけカバー

• 2. 放置したところは、あとで調べる

• 3. ただし、 質問はいつでもokです。

2011年9月3日土曜日

Page 9: Rails Tutorial ch1-2

今日の目標

2011年9月3日土曜日

Page 10: Rails Tutorial ch1-2

Railsを知った気になること

2011年9月3日土曜日

Page 11: Rails Tutorial ch1-2

“もうRails使えるお!”

2011年9月3日土曜日

Page 12: Rails Tutorial ch1-2

モチベーションはとても大事です

2011年9月3日土曜日

Page 13: Rails Tutorial ch1-2

堂々と分かった気になりましょう

2011年9月3日土曜日

Page 14: Rails Tutorial ch1-2

堂々と分かった気になりましょう

(心の中で)

2011年9月3日土曜日

Page 15: Rails Tutorial ch1-2

ではボチボチ始めます

2011年9月3日土曜日

Page 16: Rails Tutorial ch1-2

1.1 イントロ• 参考書の紹介(必要に応じて適宜参照する)

• HTML/CSS周りがワカラン:

• Head First HTML

• CSS: The Missing Manual

• Rubyがワカラン:

• The Well-Grounded Rubyist, David A. Black

• The Ruby Way, Hal Fulton

• The Rails 3 Way2011年9月3日土曜日

Page 17: Rails Tutorial ch1-2

1.1 イントロ

• 他、使えそうなモノ:

• Railscasts

• PeeoCode, Pragmatic.tv, EnvyCasts

• Rails Guides

• Rails blogs

2011年9月3日土曜日

Page 18: Rails Tutorial ch1-2

TIPS

• Ruby Weekly

• 毎週Ruby/Railsな情報を教えてくれるML

• Minituku

• Matzさんが直々に教えてくれるサイト

2011年9月3日土曜日

Page 19: Rails Tutorial ch1-2

1.2.1 開発環境• IDE(Cross-platform): RadRails, RubyMine, 3rd Rail

• OS別オススメ環境

• Mac OS X: TextMate, Emacs, MacVim

• Linux: Vim, gedit+GMate, Kate

• Win: Vim, E Text Editor, Komodo Edit, Sublime Text

• Browswers:

• Firefox + Firebug

• Firebug Lite

2011年9月3日土曜日

Page 20: Rails Tutorial ch1-2

1.2.2 必要なモノ• Ruby 1.9.x, RubyGems, Rails 3.0.x, Git

• 最近Rails3.1が出たので注意。

• Win: Rails Installerで一発らしい(?)

• http://railsinstaller.org

• Mac&Linux:

• http://ruby.railstutorial.org/ruby-on-rails-tutorial-book#sec:rubygems

2011年9月3日土曜日

Page 21: Rails Tutorial ch1-2

発表者の開発環境

2011年9月3日土曜日

Page 22: Rails Tutorial ch1-2

1.2.3 使ってみよう• mkdir rails_projecs

• cd rails_projects

• rails new first_app

2011年9月3日土曜日

Page 23: Rails Tutorial ch1-2

2011年9月3日土曜日

Page 24: Rails Tutorial ch1-2

よく使うフォルダ• app: コアな部分。MVCなコードとか

• config: アプリケーション

• db: DB周りをいじるためのコード

• public: CSSやJSとか画像などの置き場

• test: テストコード置き場

• Gemfile: プラグインを書き込むファイル

2011年9月3日土曜日

Page 25: Rails Tutorial ch1-2

1.2.4 使ってみよう• Gemfileを書き換え:

• bundle install

2011年9月3日土曜日

Page 26: Rails Tutorial ch1-2

1.2.5 使ってみよう• rails server

2011年9月3日土曜日

Page 27: Rails Tutorial ch1-2

TIPS

• 「“rails”とか”git”とかよく使いそうなコマンドは さっさとエイリアス!」

• rails server → rails s → r s

• git → g

2011年9月3日土曜日

Page 28: Rails Tutorial ch1-2

1.2.6 MVC

• app/の中の話。

• models/

• どんなデータか

• views/

• どう描画するか

• controllers/

• どう処理するか2011年9月3日土曜日

Page 29: Rails Tutorial ch1-2

1.3 Gitの使い方• Git = (Distributed) Version Controll Systems

2011年9月3日土曜日

Page 30: Rails Tutorial ch1-2

1.3 Gitの使い方• git config --global user.name “Your Name”

• git config --global user.email [email protected]

• Alias

• git config --global alias.$(エイリアス) コマンド名

• git config --global alias.a add

• git config --global alias.c commit

• git config --global alias.r remote

• エディタの指定: git config --global core.editor “emacs”2011年9月3日土曜日

Page 31: Rails Tutorial ch1-2

• git init

• emacs .gitignore

• 中身はこんな感じ→

1.3.1 Gitを使ってみる

2011年9月3日土曜日

Page 32: Rails Tutorial ch1-2

1.3.2 Gitを使ってみる

• git add .

• git status

• git commit -m “Initial commit”

• git log

• “q”をタイプすることで元の画面へ。

2011年9月3日土曜日

Page 33: Rails Tutorial ch1-2

1.3.4 GitHubを使ってみる

2011年9月3日土曜日

Page 34: Rails Tutorial ch1-2

1.3.4 GitHubを使ってみる

2011年9月3日土曜日

Page 35: Rails Tutorial ch1-2

• git remote add origin [email protected]:<username>/first_app.git

• git push origin master

1.3.4 GitHubを使ってみる

2011年9月3日土曜日

Page 36: Rails Tutorial ch1-2

1.3.5 Gitの他コマンド• Branch

• git checkout -b modify-README

• git branch

• Edit

• git mv README README.markdown

• emacs README.markdown

2011年9月3日土曜日

Page 37: Rails Tutorial ch1-2

1.3.5 Gitの他コマンド• Commit

• git status

• git commit -am “Improved the README file”

• Merge

• git checkout master

• git merge modify-README

• Branch delete

• git branch -d modify-README

• Push

• git push2011年9月3日土曜日

Page 38: Rails Tutorial ch1-2

1.4 Webにupしよう• Herokuを使う

• sudo gem install heroku

• heroku keys:add

• heroku create

• git push heroku master

• heroku open

• (Option) Heroku上のAppのURLを変更する

• heroku rename okinawarb-railstutorial2011年9月3日土曜日

Page 39: Rails Tutorial ch1-2

webにupできた!

2011年9月3日土曜日

Page 40: Rails Tutorial ch1-2

夢が広がる!• できるようになったこと• 1. Railsでアプリ作成

• 2. GitHubでソースコードを公開

• 3. アプリをWeb上で公開

2011年9月3日土曜日

Page 41: Rails Tutorial ch1-2

ちょっと休憩何か質問あれば

2011年9月3日土曜日

Page 42: Rails Tutorial ch1-2

続きRailsをもっと使ってみる

2011年9月3日土曜日

Page 43: Rails Tutorial ch1-2

2.1 おさらい• cd ~/Dropbox/rails_projects

• rails new demo_app

• cd demo_app

• emacs Gemfile

• bundle install2011年9月3日土曜日

Page 44: Rails Tutorial ch1-2

2.1 おさらい• git init

• git add .

• git commit -m “Initial commit”

• GitHubでレポジトリ作成

2011年9月3日土曜日

Page 45: Rails Tutorial ch1-2

2.1 おさらい

• git remote add origin \[email protected]:<username>/demo_app.git

• git push origin master

2011年9月3日土曜日

Page 46: Rails Tutorial ch1-2

2.1.1 Modelを考える• ユーザって何?

2011年9月3日土曜日

Page 47: Rails Tutorial ch1-2

2.1.1 Modelを考える• ユーザって何?

2011年9月3日土曜日

Page 48: Rails Tutorial ch1-2

2.1.2 Modelを考える• micropostsって何?

• (i.e. twitterのtweet)

2011年9月3日土曜日

Page 49: Rails Tutorial ch1-2

2.1.2 Modelを考える• micropostsって何?

• (i.e. twitterのtweet)

2011年9月3日土曜日

Page 50: Rails Tutorial ch1-2

2.2 Userを作ってみる • rails generate scaffold User name:string email:string

• なんか色々と作られる

• 注)まだdbはいじってない!

• dbをいじるスクリプトを生成しただけ!

• cat db/migrate/<日付>_create_users.rb

• bundle exec rake db:migrate

• これでdbをいじるスクリプトが実行される。2011年9月3日土曜日

Page 51: Rails Tutorial ch1-2

TIPS

• “rails generate scaffold” は黒魔法です。

•何が起こっても気にしないこと!

2011年9月3日土曜日

Page 52: Rails Tutorial ch1-2

TIPS

• “rails generate scaffold” は黒魔法です。

•何が起こっても気にしないこと!と、Rails Tutorialの著者が言ってます.

2011年9月3日土曜日

Page 53: Rails Tutorial ch1-2

TIPS

• “rails generate scaffold” は黒魔法です。

•何が起こっても気にしないこと!と、Rails Tutorialの著者が言ってます.

実際、本書ではscaffoldは使いません.

2011年9月3日土曜日

Page 54: Rails Tutorial ch1-2

2.2 恐る恐る見てみる• rails s

2011年9月3日土曜日

Page 55: Rails Tutorial ch1-2

2.2 恐る恐る見てみる• rails s

2011年9月3日土曜日

Page 56: Rails Tutorial ch1-2

2.2 恐る恐る見てみる

2011年9月3日土曜日

Page 57: Rails Tutorial ch1-2

2.2 恐る恐る見てみる

2011年9月3日土曜日

Page 58: Rails Tutorial ch1-2

2.2 恐る恐る見てみる

2011年9月3日土曜日

Page 59: Rails Tutorial ch1-2

2.2 恐る恐る見てみる

2011年9月3日土曜日

Page 60: Rails Tutorial ch1-2

2.2 恐る恐る見てみる

2011年9月3日土曜日

Page 61: Rails Tutorial ch1-2

2.2 恐る恐る見てみる

2011年9月3日土曜日

Page 62: Rails Tutorial ch1-2

2.2 恐る恐る見てみる

2011年9月3日土曜日

Page 63: Rails Tutorial ch1-2

2.2 恐る恐る見てみる

2011年9月3日土曜日

Page 64: Rails Tutorial ch1-2

2.2 恐る恐る見てみる

2011年9月3日土曜日

Page 65: Rails Tutorial ch1-2

2.2 恐る恐る見てみる

2011年9月3日土曜日

Page 66: Rails Tutorial ch1-2

2.2 恐る恐る見てみる

2011年9月3日土曜日

Page 67: Rails Tutorial ch1-2

2.2 恐る恐る見てみる

2011年9月3日土曜日

Page 68: Rails Tutorial ch1-2

rails generate scaffoldを実行しただけなのに...

2011年9月3日土曜日

Page 69: Rails Tutorial ch1-2

┐(´-`)┌

2011年9月3日土曜日

Page 70: Rails Tutorial ch1-2

ただサンプルコードとしては中々の優れもの

2011年9月3日土曜日

Page 71: Rails Tutorial ch1-2

ちょっとコードを読んでみる

2011年9月3日土曜日

Page 72: Rails Tutorial ch1-2

2.2.2 MVCの動き

2011年9月3日土曜日

Page 73: Rails Tutorial ch1-2

2.2.2 MVCの動き例:localhost:3000/users

1. ブラウザからreq.

2. /usersのindexを実行3. indexがUser.allを実行4. Mがdbからusersを取得5. Cにusersを渡す6. @usersにusersを代入7. Vが@usersを描画8. Cが結果を返す2011年9月3日土曜日

Page 74: Rails Tutorial ch1-2

1. ブラウザからreq.

+Enter

2011年9月3日土曜日

Page 75: Rails Tutorial ch1-2

2. /usersのindexを実行

app/controllers/users_controller.rb

2011年9月3日土曜日

Page 76: Rails Tutorial ch1-2

2. /usersのindexを実行

app/controllers/users_controller.rb

2011年9月3日土曜日

Page 77: Rails Tutorial ch1-2

3. indexがUser.allを実行

app/controllers/users_controller.rb

2011年9月3日土曜日

Page 78: Rails Tutorial ch1-2

4. Mがdbからusersを取得5. Cにusersを渡す

app/models/user.rb

2011年9月3日土曜日

Page 79: Rails Tutorial ch1-2

4. Mがdbからusersを取得5. Cにusersを渡す

app/models/user.rb

こいつが何かやってるっぽい...

2011年9月3日土曜日

Page 80: Rails Tutorial ch1-2

6. @usersにusersを代入

app/controllers/users_controller.rb

2011年9月3日土曜日

Page 81: Rails Tutorial ch1-2

7. Vが@usersを描画app/views/users/index.html.erb

2011年9月3日土曜日

Page 82: Rails Tutorial ch1-2

7. Vが@usersを描画app/views/users/index.html.erb

2011年9月3日土曜日

Page 83: Rails Tutorial ch1-2

7. Vが@usersを描画app/views/users/index.html.erb

2011年9月3日土曜日

Page 84: Rails Tutorial ch1-2

8. Cが結果を返す

app/controllers/users_controller.rb

2011年9月3日土曜日

Page 85: Rails Tutorial ch1-2

2.2.2 MVCの動き例:localhost:3000/users

1. ブラウザからreq.

2. /usersのindexを実行3. indexがUser.allを実行4. Mがdbからusersを取得5. Cにusersを渡す6. @usersにusersを代入7. Vが@usersを描画8. Cが結果を返す2011年9月3日土曜日

Page 86: Rails Tutorial ch1-2

ところで...

2011年9月3日土曜日

Page 87: Rails Tutorial ch1-2

app/controllers/users_controller.rb

2011年9月3日土曜日

Page 88: Rails Tutorial ch1-2

localhost:3000/users.xml

なん...だと...!?2011年9月3日土曜日

Page 89: Rails Tutorial ch1-2

話を戻して

2011年9月3日土曜日

Page 90: Rails Tutorial ch1-2

index以外もいろいろ

2011年9月3日土曜日

Page 91: Rails Tutorial ch1-2

他にも色々

2011年9月3日土曜日

Page 92: Rails Tutorial ch1-2

他にも色々

2011年9月3日土曜日

Page 93: Rails Tutorial ch1-2

注目!

2011年9月3日土曜日

Page 94: Rails Tutorial ch1-2

同じURLでも

2011年9月3日土曜日

Page 95: Rails Tutorial ch1-2

他にも色々

2011年9月3日土曜日

Page 96: Rails Tutorial ch1-2

実行内容が違う

2011年9月3日土曜日

Page 97: Rails Tutorial ch1-2

他にも色々

2011年9月3日土曜日

Page 98: Rails Tutorial ch1-2

URL直打ちだと

2011年9月3日土曜日

Page 99: Rails Tutorial ch1-2

2011年9月3日土曜日

Page 100: Rails Tutorial ch1-2

showになるっぽい

2011年9月3日土曜日

Page 101: Rails Tutorial ch1-2

2011年9月3日土曜日

Page 102: Rails Tutorial ch1-2

どうやってdelete/updateをするのか?

2011年9月3日土曜日

Page 103: Rails Tutorial ch1-2

2011年9月3日土曜日

Page 104: Rails Tutorial ch1-2

何が違うのか?

2011年9月3日土曜日

Page 105: Rails Tutorial ch1-2

HTTP methodが違う

2011年9月3日土曜日

Page 106: Rails Tutorial ch1-2

同じURLでもHTTP methodが違うと振る舞いも異なる

2011年9月3日土曜日

Page 107: Rails Tutorial ch1-2

こんな設計を“RESTfulなリソース設計”

と呼びます

2011年9月3日土曜日

Page 108: Rails Tutorial ch1-2

TIPS• RESTfulの恩恵:

• URLが直感的になる

• “GET users/1” = userのNo.1の情報を取得

• “PUT users/1” = userのNo.1を更新する

• “DELETE users/1” = userのNo.1を削除する

• 使いやすいAPIを提供出来る.

• リソースに対してHTTP methodを変えるだけ!

詳細はchapter 12で解説

2011年9月3日土曜日

Page 109: Rails Tutorial ch1-2

もっとRESTfulを知りたい!

2011年9月3日土曜日

Page 110: Rails Tutorial ch1-2

話を戻して

2011年9月3日土曜日

Page 111: Rails Tutorial ch1-2

同様にMicropostsを作ってみる

2011年9月3日土曜日

Page 112: Rails Tutorial ch1-2

黒魔法再び

┐(´-`)┌2011年9月3日土曜日

Page 113: Rails Tutorial ch1-2

2.3 Micropostsを作ってみる• rails generate scaffold Micropost \

content:string user_id:integer

• bundle exec rake db:migrate2011年9月3日土曜日

Page 114: Rails Tutorial ch1-2

2.3 Micropostsを作ってみる

2011年9月3日土曜日

Page 115: Rails Tutorial ch1-2

2.3 Micropostsを作ってみる

2011年9月3日土曜日

Page 116: Rails Tutorial ch1-2

2.3 Micropostsを使ってみる

2011年9月3日土曜日

Page 117: Rails Tutorial ch1-2

2.3 Micropostsを使ってみる

2011年9月3日土曜日

Page 118: Rails Tutorial ch1-2

2.3 Micropostsを使ってみる

2011年9月3日土曜日

Page 119: Rails Tutorial ch1-2

2.3 Micropostsを使ってみる

rails generate scaffold Micropost \content:string user_id:integer

2011年9月3日土曜日

Page 120: Rails Tutorial ch1-2

2.3 Micropostsを使ってみる

2011年9月3日土曜日

Page 121: Rails Tutorial ch1-2

2.3 Micropostsを使ってみる

2011年9月3日土曜日

Page 122: Rails Tutorial ch1-2

これでtwitter-like...?

2011年9月3日土曜日

Page 123: Rails Tutorial ch1-2

問題点1. 文字数無制限2. Userが独立

2011年9月3日土曜日

Page 124: Rails Tutorial ch1-2

2.3.2 文字数制限の追加

2011年9月3日土曜日

Page 125: Rails Tutorial ch1-2

2.3.2 文字数制限の追加

2011年9月3日土曜日

Page 126: Rails Tutorial ch1-2

2.3.3 Userを繋ぐ

2011年9月3日土曜日

Page 127: Rails Tutorial ch1-2

2.3.3 Userを繋ぐ

2011年9月3日土曜日

Page 128: Rails Tutorial ch1-2

2.3.3 Userを繋ぐ

2011年9月3日土曜日

Page 129: Rails Tutorial ch1-2

2.3.3 Userを繋ぐ

• rails console

• >> User.first

• => #<User id: 1, name: “...”, email: “...” ...

• >> User.first.microposts

• => [#<Micropost id: 1, content: “...”, ...

2011年9月3日土曜日

Page 130: Rails Tutorial ch1-2

2.3.3 Userを繋ぐ

• rails console

• >> User.first

• => #<User id: 1, name: “...”, email: “...” ...

• >> User.first.microposts

• => [#<Micropost id: 1, content: “...”, ...

2011年9月3日土曜日

Page 131: Rails Tutorial ch1-2

2.3.3 Userを繋ぐ

• rails console

• >> User.first

• => #<User id: 1, name: “...”, email: “...” ...

• >> User.first.microposts

• => [#<Micropost id: 1, content: “...”, ...

2011年9月3日土曜日

Page 132: Rails Tutorial ch1-2

2.3.3 Userを繋ぐ

• rails console

• >> User.first

• => #<User id: 1, name: “...”, email: “...” ...

• >> User.first.microposts

• => [#<Micropost id: 1, content: “...”, ...

2011年9月3日土曜日

Page 133: Rails Tutorial ch1-2

2.3.3 Userを繋ぐ

• rails console

• >> User.first

• => #<User id: 1, name: “...”, email: “...” ...

• >> User.first.microposts

• => [#<Micropost id: 1, content: “...”, ...

2011年9月3日土曜日

Page 134: Rails Tutorial ch1-2

2.3.3 Userを繋ぐ

• rails console

• >> User.first

• => #<User id: 1, name: “...”, email: “...” ...

• >> User.first.microposts

• => [#<Micropost id: 1, content: “...”, ...

2011年9月3日土曜日

Page 135: Rails Tutorial ch1-2

2.3.5 webにupしてみる(再)• git add .

• git commit -am “Done with the demo app”

• git push

• heroku create

• git push heroku master

• heroku rake db:migrate

• heroku open

2011年9月3日土曜日

Page 136: Rails Tutorial ch1-2

まとめ

• 出来るようになったこと

• 1. rails generate scaffoldを使える

• 2. どこに何のコードがあるか分かる

• 3. modelに制限かけたり繋いだり出来る

2011年9月3日土曜日

Page 137: Rails Tutorial ch1-2

続きは• Chapter 3 - Mostly static pages

• http://ruby.railstutorial.org/chapters/static-pages#top

2011年9月3日土曜日

Page 138: Rails Tutorial ch1-2

Thanks!

@yasulab

2011年9月3日土曜日

Page 139: Rails Tutorial ch1-2

英文よむときのポイント• 1. マウスオーバー辞書 + 英辞郎

• MacならLiveDictionary+英辞郎

• 2. 英文の構成を知る (i.e. 5 paragraph essay)

• Intro→body 1→body 2→body 3→cncl. 

• 3. ソースコードや図・表で理解しようと試みる

• “ソースコードは口ほどに物を言う”

2011年9月3日土曜日