+
RUBY ON RAILS 3 Tutorial を日本語訳してみたChapter 2
2011-10-12
目次
Chapter1 Rails 導入からデプロイ
Chapter2 デモアプリ (scaffold 使用 )
Chapter3 Web アプリケーション
Chapter4 Rails 風 Ruby
Chapter5 スタイルを追加する
Chapter6 User Model と View その 1
Chapter7 User Model と View その 2
2
目次
Chapter8 ユーザ登録
Chapter9 ログイン・ログアウト
Chapter10 ユーザデータの更新・編集・追加
Chapter11 ミニブログ ( ツイート )
Chapter12 ユーザのフォロー
3
+Chapter 2 A demo app
scaffold を使ってデモアプリを作る
バージョン管理は Git で
※Tutorial に書いてあるバージョン指定ではなく 3.1 で試しています
4
+2.1 Planning the Application
Rails のアプリ作成
bundle install コマンド実行
Tutorial では 3.0.9 ですが 3.1 でやってみます
※3.0.9 の場合 Gemfile 修正が必要↓
5
$ rails new demo_app$ cd demo_app
$ bundle install
+2.1 Planning the Application
Git に commit する
github で作成したリポジトリに push する
6
$ git init$ git add .$ git commit –m “Initial commit”
$ git remote add origin [email protected]:<username>/demo_app.git$ git push origin master
+2.1 Planning the Application
今回考えられるモデル
7
users
id integer
name string
email string
microposts
id integer
content string
user_id integer
+2.1 Planning the Application
scaffold で User 作成
DB の初期化
起動 (rails s)
8
$ rails generate scaffold User name:string email:string invoke active_record create db/migrate/20111011172415_create_users.rb create app/models/user.rb invoke test_unit …省略…
$ rake db:migrate== CreateUsers: migrating =============-- create_table(:users) -> 0.0023s== CreateUsers: migrated (0.0024s) =================================
+Box 2.1 Rake
Rake コマンドの一覧出力 DB に関連したコマンド (rake –T db)
すべて (rake –T)
9
$ rake -T db… 省略…rake db:schema:load # Load a schema.rb file into the databaserake db:seed # Load the seed data from db/seeds.rbrake db:structure:dump # Dump the database structure to an SQL filerake db:version # Retrieves the current schema version number
$ rake –T… 省略…rake db:seed # Load the seed data from db/seeds.rbrake assets:clean # Remove compiled assets… 省略…
+2.2.1 A User Tour
http://localhost:3000 にアクセス Rails の index ページが見える
config/routes.rb にはresources :users が追加されている
10
URL Action 説明
/users index 全ユーザ表示
/users/1 show ユーザ id が 1 のユーザ表示
/users/new new 新しいユーザを作るページ
/users/1/edit edit ユーザ id が 1 のユーザの編集ページ
+2.2.2 MVC in Action
MVC モデルで /users が表示されるまで
1. ブラウザから /users にリクエスト
2. このルーティングの場合、 /users は User コント
ローラの index アクション
3. index アクションは User モデルに全ユーザ取得
(User.all) のリクエストを送る
4. User モデルが DB からすべてのユーザを取り出す
11
+2.2.2 MVC in Action
5. User モデルがコントローラにすべてのユーザの list
を返す
6. コントローラが @users に list を格納して、 index
ビューに送る
7. ビューがユーザ一覧表示のため、 Ruby を埋込みで利
用している
8. コントローラが作成された HTML をブラウザに返す
12
+2.2.2 MVC in Action
@ から始まる変数は、 instance variablesと呼ばれる
自動的に view で利用出来る変数になる
13
+2.2.3 Weaknesses of This Users Recource
データのバリデーションをチェックしていない name の入力がない・不正なメールアドレスでも処理して
しまう
認証がない 他のユーザの登録情報が見えている
テストがない scaffold はテストを内部に持っている 自動生成されたテストは、汚く・柔軟性が無い バリデーション・認証等、他に必要な機能のテストが無い
14
+2.2.3 Weaknesses of This Users Recource
レイアウトがない サイトにあった CSS スタイルやナビゲーションがな
い
実際的な理解がない scaffold のコードは自動生成の為、勉強にならない
15
+2.3 The Microposts Resource
User 同様、 Micropost を作成する
16
$ rails generate scaffold Micropost content:string user_id:integer
$ rake db:migrate
+2.3 The Microposts Resource
17
HTTP request
URL Action 詳細
GET /microposts index すべての post 表示ページ
GET /microposts/1 show id が1の post を表示するページ
GET /microposts/new new 新しい post 作成するページ
POST /microposts create 新しい post 作成
GET /microposts/1/edit edit id が 1 の post を編集するページ
PUT /microposts/1 update id が 1 の post を更新する
DELETE /microposts/1 destroy id が 1 の post を削除する
+2.3 The Microposts Resource
app/models/micropost.rb にバリデーション追加
18
class Micropost < ActiveRecord::Base
validates :content, :length => { :maximum => 140}
end
+2.3 The Microposts Resource
“the default Rails error messages are not valid HTML” 正しい HTML 構成ではないらしい…?
19
+2.3.3 A User has_many Microposts
1 対多の関係
app/models/user.rb と micropost.rb に追加 has_many と belongs_to
20
class Micropost < ActiveRecord::Base belongs_to :user validates :content, :length => { :maximum => 140}end
class User < ActiveRecord::Base has_many :micropostsend
+2.3.3 A User has_many Microposts
21
+2.3.5 Deploying the Demo App
Microposts が完成したので、一度 push しておく
Heroku にデプロイ
22
$ git add .$ git commit –a –m “Done with the demo app”$ git push
$ heroku create$ git push heroku master$ heroku rake db:migrate
+2.3.5 Deploying the Demo App
Heroku にデプロイがうまくいかなかったとき
Listen 1.8 で挙げたようになっているか Gemfile確認
DB に migration をかけてみる
23
$ [ sudo ] gem install taps$ heroku db:push
+2.4 Conclusion
利点 Rails の概要をよく知ることが出来る
MVC モデルの導入になる
REST について学べる
データモデリングを始めることが出来る
DB を使った動的サービスが作れる
24
+2.4 Conclusion
欠点 レイアウトやスタイルがない Home や About のような静的ページがない ユーザ情報がない
パスワード 画像
登録がない セキュリティーがない 自動的に user と micropost が関連されていない フォローとフォロワーの関係がない test~driven development ではない 実際的な理解が何も無い
25
Top Related