3
3 ............................................................................................................................................................
2
2 ........................................................................................................................................................................
.....................................................................................................................................................................................iviv
.....................................................................................................................................................................................
..................................................................................................................................................................................
.................................................................................................................................................................................. vv
..................................................................................................................................................................................................
..................................................................................................................................................................................................vivi
1 ......................................................................................................................................................................
1
...................................................................................................................................................................... 11
1.1 ........................................................................................................................................................................... 2
1.2 ................................................................................................................................................................... 6
1.3 git .................................................................................................................................................. 20
1.4 ......................................................................................................................................................................... 30
1.5 ......................................................................................................................................................................... 35
........................................................................................................................................................................ 3737
2.1 ................................................................................................................................................................. 37
2.2 Users users resource................................................................................................................................ 40
2.3 Microposts ...................................................................................................................................................... 52
2.4 ......................................................................................................................................................................... 61
............................................................................................................................................................ 6363
3.1 ................................................................................................................................................................. 66
3.2 ............................................................................................................................................................. 73
3.3 ............................................................................................................................................. 81
3.4 ......................................................................................................................................................................... 90
3.5 ......................................................................................................................................................................... 91
3.6 ................................................................................................................................................................. 93
..................................................................................................................................................... 104104
4.1 ....................................................................................................................................................................... 104
4.2 ....................................................................................................................................................... 108
4.3 ................................................................................................................................................... 114
4.4 Ruby ................................................................................................................................................................. 124
4.5 ....................................................................................................................................................................... 132
4.6 ....................................................................................................................................................................... 132
...................................................................................................................................................................... 134134
5.1 ....................................................................................................................................................... 135
5.2 Sass asset pipeline............................................................................................................................................ 150
5.3 ....................................................................................................................................................... 159
5.4 ............................................................................................................................................... 171
5.5 ....................................................................................................................................................................... 175
5.6 ....................................................................................................................................................................... 175
...................................................................................................................................................................... 179179
6.1 User .............................................................................................................................................................. 180
6
6 ......................................................................................................................................................................
ii
4 Rails Ruby
4 Rails Ruby .....................................................................................................................................................
5
5 ......................................................................................................................................................................
9
9 ..............................................................................................................................................
8
8 ..................................................................................................................................................................
7
7 ......................................................................................................................................................................
6.2 ....................................................................................................................................................... 189
6.3 ....................................................................................................................................................... 203
6.4 ....................................................................................................................................................................... 214
6.5 ....................................................................................................................................................................... 214
...................................................................................................................................................................... 216216
7.1 ....................................................................................................................................................... 216
7.2 ............................................................................................................................................................... 232
7.3 ............................................................................................................................................................... 244
7.4 ............................................................................................................................................................... 252
7.5 ....................................................................................................................................................................... 259
7.6 ....................................................................................................................................................................... 259
.................................................................................................................................................................. 262262
8.1 session .............................................................................................................................................. 262
8.2 ............................................................................................................................................................... 277
8.3 Cucumber ..................................................................................................................................... 295
8.4 ....................................................................................................................................................................... 302
8.5 ....................................................................................................................................................................... 303
.............................................................................................................................................. 305305
9.1 ............................................................................................................................................................... 305
9.2 ............................................................................................................................................................... 315
9.3 ....................................................................................................................................................... 326
9.4 ............................................................................................................................................................... 341
9.5 ....................................................................................................................................................................... 349
9.6 ....................................................................................................................................................................... 351
................................................................................................................................................................ 355355
10.1 Microposts .................................................................................................................................................. 355
10.2 ............................................................................................................................................................. 368
10.3 ................................................................................................................................................. 377
10.4 ..................................................................................................................................................................... 399
10.5 ..................................................................................................................................................................... 399
.................................................................................................................................................................... 403403
11.1 ............................................................................................................................................................. 403
11.2 ................................................................................................................................. 419
11.3 ............................................................................................................................................................. 443
11.4 ..................................................................................................................................................................... 450
11.5 ..................................................................................................................................................................... 453
iii
10
10 ................................................................................................................................................................
11
11 ....................................................................................................................................................................
Michael Hartl Ruby on Rails Web
RailsSoace Rails Ruby on Rails Insoshi
Ruby 2011 Ruby Hero
Y Combinator
Andor Chen
WordPress Ruby Rails Web
Rails
iv
Ruby “”David Heinemeier Hansson
Rails RubyRails Ruby on Rails Web
Ruby on Rails Tutorial Rails Web
Web Ruby
on Rails Andor
Chen Ruby on Rails Tutorial
Ruby on Rails Tutorial Michael Hartl
Ruby is a delightful computer language explicitly designed to make programmers happy. This philosophy influenced David
Heinemeier Hansson to pick Ruby when implementing the Rails web framework. Ruby on Rails, as it’s often called, makes
building custom web applications faster and easier than ever before. In the past few years, the Ruby on Rails Tutorial has
become the leading introduction to web development with Rails.
In our interconnected world, computer programming and web application development are rapidly rising in importance, and I
am excited to support Ruby on Rails in China. Although it is important to learn English, which is the international language
of programming, it’s often helpful at first to learn in your native language. It is for this reason that I am grateful to Andor
Chen for producing the Chinese-language edition of the Ruby on Rails Tutorial book.
I’ve never been to China, but I definitely plan to visit some day. I hope I’ll have the chance to meet some of you when I do!
Best wishes and good luck,
Michael Hartl
Author
The Ruby on Rails Tutorial
v
CD Baby Ruby on Rails
PHP Google Michael Hartl
Ruby on Rails Tutorial Rails
Rails “Rails ”
Rails
TDD GitGitHub
Heroku
Rails
Derek Sivers (sivers.org)
CD Baby
vi
1
1
Ruby on Rails “ Ruby on Rails Web
” Rails Web
Rails Rails 4
Web Ruby on Rails
Rails 4 Rails 4 Rails 4
Rails 3.2 1.1
“” 7.3.2 Rails
Turbolinks RSpec “”
1.1
“”
Rails 3.2
Rails 3.2
• Rails 3.2 Rails 4.01.2.2
• Capybara DSL 3.10
• RSpec .should expect().to3.2.1
• have_selector('title', ...) have_title(...)3.3.1
• HTTP PUT PATCH 3.3
• JavaScript Turbolinks 3.26
• root to: 'path' root 'path' 5.24
• find_by_thing(...) find_by(thing: ...)6.1.4
• rake db:test:prepare rake test:prepare6.2.1
• attr_accessible“”7.3.2
• 8.2.1
• before_filter before_action 9.21
• Capybara match: :first 9.43
• default_scope Hash lambda 10.11
• dup to_a 10.12
• XPath 11.2.4
Rails Rails Web
Ruby on Rails HTMLCSS
1
Rails Derek Sivers
TDD
Ruby on Rails 1.2 Rails
first_app
Git 1.3 1.4
2 Rails
1.1 demo_app 2
URI URL1
sample_app
“”Test-driven Development, TDD 3
Rails Ruby
10 11
Rails 2
Web
1.1
2004 Rails Web
Rails37signalsGithubShopifyScribdTwitterDisneyHulu Yellow Pages
Web Rails ENTPthoughtbotPivotal Labs Hashrocket
Rails Ruby on Rails MIT
Rails Ruby Rails
Web “”Domain-speific Language, DSL Web HTML
URL Rails
Rails Web Rails REST Web
Rails DHH
Rails Merb Rails Ruby Web
Rails Merb API
1. URI Uniform Resources Identifier URL Uniform Resource
LocatorURL
2. Twiiter
2