ii
iii
iv
v
vi
4 get "lesson/:action(/:name)" => "lesson" 5 6 resources :members 7 end Sato <%= link_to "Home", root_path, class: "menu" %> mac:rails taro$ cd asagao mac:asagao taro$ bin/rails s mac:asagao taro$ C: rails asagao> Windows / chapter3/app/controllers chapter3 app controllers \ vii
Mac \ o \ http://www.oiax.jp/rails4book chapter4/app/models/member.rb chapter4 app models member.rb chapter1 chapter7 chapter6a chapter6b chapter6a chapter6b section8-2 bundle install bin/rails s viii
mac:chapter1 taro$ bundle install mac:chapter1 taro$ bin/rails s bin/rails s bin/rake db:reset mac:chapter4 taro$ bundle install mac:chapter4 taro$ bin/rake db:reset mac:chapter4 taro$ bin/rails s bin/ ruby C: rails chapter4> bundle install C: rails chapter4> ruby bin/rake db:reset C: rails chapter4> ruby bin/rails s ix
...iii...v Chapter1... 2 1.1 Ruby on Rails...4 Ruby on Rails... 4 MVC... 8 Rails... 9... 11 1.2 Rails...14... 14 Mac... 15 Windows... 21... 25 Bundler... 27 1.3 Rails...31... 31... 33... 34... 37...41 Chapter2 Ruby... 42 2.1...44 Ruby... 44... 47... 54 2.2...59... 59... 66... 70... 74 2.3...77... 77... 78... 84... 88 x
2.4...92 Ruby... 92... 95... 98... 100... 103 Ruby... 107...111 Chapter3... 114 3.1 Rails HTTP...116 HTTP... 116 Rails... 120... 122 3.2...127... 127... 129... 132... 134 3.3...137... 137... 142... 145... 148 3.4...152... 152... 154... 156... 161...166 Chapter4... 168 4.1...170... 170 Rails... 171... 173... 175 Rake... 176 4.2...178... 178... 179 members... 181... 183 xi
4.3...188... 188... 191 4.4...195 find find_by... 195... 197...207 Chapter5... 210 5.1 REST...212... 212... 216 5.2 7...219 MembersController... 219... 220... 228...232 Chapter6... 234 6.1...236... 236... 237... 240... 244 6.2...250... 250... 251... 255 6.3...258... 258... 263 6.4...266 Rails... 266... 267 YAML... 273... 275...281 Chapter7... 284 7.1...286... 286... 287 xii
7.2...290... 290... 293 Factory Girl... 296 7.3...300 Article... 300... 302... 309 7.4...314... 314... 316 ArticlesController... 317 TopController... 329 7.5...331... 331...334 Chapter8... 336 8.1...338... 338 Sass... 342 CoffeeScript... 346 Turbolinks... 349 8.2...351... 351... 355... 359... 365... 371 8.3...376... 376... 380 8.4...383... 383 AccountsController... 384 8.5...389 Rails... 389... 391... 395 8.6...398 will_paginate... 398 will_paginate... 400...406 Chapter9... 408 9.1...410 xiii
... 410... 412 9.2 1...419... 419... 422 Entry... 425... 429... 435 9.3 1 1...439... 439 MemberImage... 441... 446... 450 9.4...457... 457... 461... 464 9.5...467... 467... 472... 477... 482...488 A... 490 B MySQL... 492 C Ruby... 496 D validates... 500 E... 504 F... 509...514... 74...109...194 REST... 218 scaffold...249...278...420 xiv
1
1 1.1 Ruby on Rails Ruby on Rails MVC Rails 1.2 Rails Mac Windows Bundler 1.3 Rails 2
1 3
1.1 Ruby on Rails Ruby on Rails Rails 4
1 1 Ruby on Rails Rails URL http://rubyonrails.org/ Rails Riding Rails Ruby on Rails http://weblog.rubyonrails.org/ 5
http://guides.rubyonrails.org/ http://railsguides.jp/ Ruby on Rails API http://api.rubyonrails.org/ Ruby on Rails Rails David Heinemeier Hansson DHH 37signals Basecamp Basecamp Rails http://david.heinemeierhansson.com/ http://itpro.nikkeibp.co.jp/article/news/20060620/241346/ Ruby Rails 6
1 1 Ruby Ruby Matz 1995 Ruby http://www.rubyist.net/~matz/ Ruby Rails 1.0 2005/12/13 3.0 2010/08/29 1.1 2006/03/28 3.1 2011/04/31 1.2 2007/01/19 3.2 2012/01/20 2.0 2007/12/07 4.0 2013/06/25 2.1 2008/06/01 4.1 2014/04/08 2.2 2008/11/21 4.2 2014/12/19 2.3 2009/03/16 5 2015? 7
MVC Rails MVC 8
1 1 members Member member.rb Rails Rails 9
rails & = /members /1 < %... % > rake birthday @member.birthday 10
1 1 @member.entries 11
12
1 1 chapter3 section9-5 13
1.2 Rails Mac/Windows Mac 14
1 1 Windows http://www.oiax.jp/rails4book Mac Mac Ruby Rails Mac Ruby RubyGems Mac Xcode 15
MacPorts Homebrew MacPorts Homebrew Mac Linux Homebrew Ruby rbenv MacPorts Homebrew MacPorts Homebrew MacPorts /opt/local Ruby Homebrew rbenv /Users/taro /.rbenv/ Ruby MacPorts Ruby gem sudo bundle Homebrew rbenv Ruby MacPorts Homebrew 16
1 1 mac:~ taro$ xcode-select --install MacPorts Ruby http://www.macports.org/ 17
mac:~ taro$ sudo port install ruby22 ruby22 ruby mac:~ taro$ sudo port select ruby ruby22 mac:~ taro$ ruby -v ruby 2.2.1p85 (2015-02-26 revision 49769) [x86_64-darwin14] sudo port install ruby23 sudo port select ruby ruby23 Homebrew rbenv Ruby http://brew.sh/ 18
1 1 mac:~ taro$ ruby -e "$(curl -fssl https://raw.githubusercontent.com/homebrew/ install/master/install)" mac:~ taro$ brew install rbenv ruby-build.bash_profile eval "$(rbenv init -)" mac:~ taro$ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile.bash_profile mac:~ taro$ source.bash_profile mac:~ taro$ rbenv install 2.2.1 rbenv versions mac:~ taro$ rbenv versions * system (set by /Users/taro/.rbenv/version) 2.2.1 19
mac:~ taro$ rbenv local 2.2.1 mac:~ taro$ ruby -v ruby 2.2.1p85 (2015-02-26 revision 49769) [x86_64-darwin14] rbenv install 2.3.0 rbenv local 2.3.0 Rails gem --version "4.2.1" sudo mac:~ taro$ sudo gem install rails --version "4.2.1" --no-rdoc --no-ri sudo mac:~ taro$ gem install rails --version "4.2.1" --no-rdoc --no-ri --no-ri --no-rdoc rails mac:~ taro$ rails -v Rails 4.2.1 20
1 Rails SQLite3 SQLite3 Mac SQLite3 Windows SQLite3 Bundler Gem sqlite3 SQLite3 Ruby SQLite3 Rails SQLite3 MySQL B MySQL 1 Windows Ruby http://rubyinstaller.org/ 21
Windows Ruby 2015 3 Windows Ruby 2.2 Rails Ruby 2.2 Rails Windows C: Users taro> ruby -v ruby 2.1.5p273 (2014-11-13 revision 48405) [i386-mingw32] 22
1 DevKit 1 C: devkit Rails C: devkit devkitvars C: Users taro> C: devkit devkitvars gem bundle cacert.pem C: Users taro http://curl.haxx.se/docs/caextract.html cacert.pem cacert.pem.txt cacert.pem C: Users taro 23
C: Users taro> set SSL_CERT_FILE=C: Users taro cacert.pem gem C: Users taro> gem install rails --version "4.2.1" --no-rdoc --no-ri gem bin/rails s Windows Ruby C: Users taro> rails -v Rails 4.2.1 24
1 Mac Windows Rails Gem Ruby gems 2.2.0 gems Active Record activerecord- Mac MacPorts /opt/local/ lib/ruby2.2 Homebrew rbenv.rbenv/ versions/2.2.0/lib/ruby Windows C: Ruby21 lib ruby 1 /Users/taro rails mkdir cd mac:~ taro$ mkdir rails mac:~ taro$ cd rails C: Users rails C: Users > cd C: > mkdir rails C: > cd rails 25
Mac Mac pwd mac:~ taro$ pwd /Users/taro/rails/asagao 1.bash_profile taro@mac~/rails/asagao$ export PS1=" u@ h w$ ".bash_profile nano.bash_profile rails rails new --skip-bundle bundle install bundle mac:rails taro$ rails new asagao --skip-bundle create create README.rdoc create Rakefile create config.ru create vendor/assets/stylesheets create vendor/assets/stylesheets/.keep rails new asagao asagao bundle install 26
1 1 mac:rails taro$ cd asagao mac:asagao taro$ bundle install Fetching gem metadata from https://rubygems.org/... Resolving dependencies... Using rake 10.4.2 Using i18n 0.7.0 Using json 1.8.2 Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed. Windows bundle Windows gem bundle DevKit SSL C: rails asagao> C: devkit devkitvars C: rails asagao> set SSL_CERT_FILE=C: Users taro cacert.pem rails new Rails rails new Rails Rails rails _ _ mac:rails taro$ rails _4.1.9_ new asagao --skip-bundle Bundler 27
bundle install rails new Gemfile Gemfile gem ' ' 1 source 'https://rubygems.org' 2 3 4 # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' 5 gem 'rails', '4.2.1' 6 # Use sqlite3 as the database for Active Record 7 gem 'sqlite3' 8 # Use SCSS for stylesheets 9 gem 'sass-rails', '~> 5.0' 10 # Use Uglifier as compressor for JavaScript assets 11 gem 'uglifier', '>= 1.3.0' 12 # Use CoffeeScript for.coffee assets and views 13 gem 'coffee-rails', '~> 4.1.0' 28
1 Gemfile Gemfile Gem 1 gem 'rails', '4.2.1' # 4.2.1 gem 'sqlite3' # OK gem 'uglifier', '>= 1.3.0' # 1.3.0 gem 'coffee-rails', '~> 4.1.0' # 4.1.0 4.2 rails-i18n Chapter 6 factory_girl Chapter 7 bcrypt-ruby Chapter 8 will_paginate Chapter 8 Gemfile bundle install Gemfile Gemfile.lock Gemfile.lock Gemfile.lock 1 GEM 2 remote: https://rubygems.org/ 3 specs: 4 actionmailer (4.2.1) 5 actionpack (= 4.2.1) 6 actionview (= 4.2.1) 7 activejob (= 4.2.1) 8 mail (~> 2.5, >= 2.5.4) 9 rails-dom-testing (~> 1.0, >= 1.0.5) Gemfile.lock bundle install Gemfile.lock bundle install chapter1 chapter1 bundle install 29
bundle bundle install bundle list Gem bundle check Gem The Gemfile's dependencies are satisfied bundle update Gemfile.lock Gemfile Gem Gemfile.lock Gem 30
1 1.3 Rails 1 asagao bin/rails s bin/rails server mac:asagao taro$ bin/rails s => Booting WEBrick => Rails 4.2.1 application starting in development on http://localhost:3000 => Run `rails server -h` for more startup options => Ctrl-C to shutdown server [2015-02-11 16:45:13] INFO WEBrick 1.3.1 [2015-02-11 16:45:13] INFO ruby 2.2.1 (2015-02-26) [x86_64-darwin14] [2015-02-11 16:45:13] INFO WEBrick::HTTPServer#start: pid=4577 port=3000 ruby bin/rails s ruby bin/rails server ruby C: rails asagao> ruby bin/rails s http://localhost:3000/ http://127.0.0.1:3000/ 31
c C WEBrick WEBrick Ruby Ruby Rails WEBrick 1 Rails Rails Ruby on Rails MySQL SQLite3 MySQL bin/rails s bin/rake db:create Chapter 4 B Rails SQLite3 32
1 1 app config db app config app Chapter bin HINT config Chapter db Chapter 4 doc lib rake log public test Chapter 7 tmp vendor bin bin/ rails 1.2 Rails rails bin/rails bin 33
Rails asagao Rails 4.2.1 Rails bin/rails Rails config/initializers generators.rb 1 Rails.application.config.generators do g 2 g.helper false # 3 g.assets false # CSS, JavaScript 4 g.skip_routes true # config/routes.rb 5 g.test_framework false # 6 end bin/rails g bin/rails g rails g controller TopController index bin/ rails g bin/rails generate ruby mac:asagao taro$ bin/rails g controller top index create app/controllers/top_controller.rb route get 'top/index' invoke erb create app/views/top create app/views/top/index.html.erb 34
1 app/controllers top_controller.rb 1 class TopController < ApplicationController def index end end TopController TopController ApplicationController TopController index index Rails UTF-8 Mac Atom https://atom.io/ Windows Atom http://www5f.biglobe.ne.jp/~t-susumu/ http://hide.maruo.co.jp/ 35
UTF-8 LF UNIX Windows UNIX HINT config routes.rb Rails.application.routes.draw do end 1 Rails.application.routes.draw do 2 root "top#index" 3 end http://www.oiax.jp/ / TopController index bin/rails s http:// localhost:3000/ TopController index 36
1 Windows 2015 3 Windows TypeError: Gemfile 1 gem 'coffee-script-source', '1.8.0' Gem coffee-script-source C: rails asagao> bundle update coffee-script-source index app/ views/top index.html.erb.erb index.html.erb <h1> </h1> <p> Rails </p> http://localhost:3000/ 37
UTF-8 Rails UTF-8 Rails Ruby.rb.erb UTF-8 Windows TeraPad UTF-8 UTF-8N Unicode(UTF-8) BOM bin/rails g bin/rails g controller top TopController index index. html.erb Rails app/controllers top_controller.rb index @message @message " " 1 class TopController < ApplicationController 2 def index 3 @message = " " 4 end 5 end app/views/top index.html.erb <%= %> @message 1 <h1><%= @message %></h1> 2 <p> Rails </p> 38
1 http://localhost:3000/ @message 1 Rails WEBrick WEBrick bin/rails g 1 Rails Ruby on Rails 2015 5 Rails 5 Rails 4.2 39