blog.textfile.org結城浩のブログ

blog.textfile.org

Ruby on Rails Tutorial 自分用メモ

Ruby on Rails Tutorialの要点を短時間で振り返るための「自分用メモ」です。

Install RVM.

$ curl -L https://get.rvm.io | bash -s
$ rvm get stable
$ rvm requirements
$ rvm list
$ rvm list known

Install Ruby 2.0.0.

$ rvm install 2.0.0 --with-openssl-dir=$HOME/.rvm/usr

Create gemset for this tutorial.

$ rvm use 2.0.0@railstutorial_rails_4_0 --create --default

Update gem.

$ gem update --system 2.0.3

Install Rails.

$ gem install rails --version 4.0.5

Create my first app.

$ rails new first_app
$ cd first_app
  • Gemfileでバージョンを明示的に指定するとよい。
  • gemsetを指定するコメントあり

Gemfile更新後

$ bundle update
$ bundle install

Rails server http://localhost:3000

$ rails s

.gitignore をすげ替える

$ open http://railstutorial.jp/book/ruby-on-rails-tutorial?version=4.0#code-gitignore

Git最初だけ

$ git config --global user.name "MY NAME"
$ git config --global user.email my.mail@example.com

Gitリポジトリ最初だけ

$ git init
$ git add .
$ git commit -m "Initial commit."

(GitHubにMYAPP用のリポジトリ作っておく)
$ git remote add origin https://github.com/MYUSERNAME/MYAPP.git
$ git push -u origin master

(BitBucketの場合、BitBucketでMYAPP用のリポジトリ作っておく)
$ git remote add origin git@bitbucket.org:MYUSERNAME/MYAPP.git
$ git push -u origin --all
$ git push -u origin --tags

Git日々の作業

$ git checkout -b BRANCH-NAME
$ git branch
$ vi something
$ git add .
$ git commit -m "Change something."
$ git checkout master
$ git merge BRANCH-NAME
$ git branch -d BRANCH-NAME
$ git push origin master

Herokuで使っているデータベースはPostgresSQL

$ open http://railstutorial.jp/book/ruby-on-rails-tutorial?version=4.0#code-gemfile_pg_gem
$ bundle install --without production
$ heroku login
$ heroku create
$ git push heroku master
$ heroku open

Herokuでカスタムドメイン使う場合

$ open https://devcenter.heroku.com/articles/custom-domains
$ heroku domains:add myapp.example.com
(DNS Providerで myapp.example.org のCNAMEを xxxx-xxxx-1234.herokuapp.com にする)
$ host myapp.example.com

メモ

$ cd rails_projects
$ rails new demo_app
$ cd demo_app
$ open http://railstutorial.jp/book/ruby-on-rails-tutorial?version=4.0#code-demo_gemfile_sqlite_version_redux
$ vi Gemfile
$ bundle install --without production
$ bundle update
$ bundle install
$ git init
$ git add .
$ git commit -m "Initial commit"
$ (リポジトリを新規作成)
$ git remote add origin https://github.com/MY-NAME/MY-APP.git
$ git push -u origin master

ScaffoldでUser, Micropost作る

$ rails generate scaffold User name:string email:string
$ rails generate scaffold Micropost content:string user_id:integer
$ bundle exec rake db:migrate

関連ファイル

  • config/routes.rb
  • app/controllers/users_controller.rb
  • app/views/users/index.html.erb
  • app/models/micropost.rb

データモデル同士の関連づけ

  • has_many :microposts
  • belongs_to :user

検証

  • validates :content, length: { maximum: 140 }

テスト

$ rails console

$ heroku create
$ git push heroku master
$ heroku run rake db:migrate

$ cd rails_projects
$ rails new sample_app --skip-test-unit
$ cd sample_app
$ git init
$ git add .
$ git commit -m "Initial commit."
$ open http://railstutorial.jp/book/ruby-on-rails-tutorial?version=4.0#code-final_gemfile
$ vi Gemfile
$ bundle install --without production
$ bundle update
$ bundle install

秘密トークンの動的生成

$ open http://railstutorial.jp/book/ruby-on-rails-tutorial?version=4.0#code-secret_token
$ vi config/initializers/secret_token.rb

RSpec

$ rails generate rspec:install
$ git mv README.rdoc README.md
$ git commit -am "Improve the README"
$ git remote add origin https://github.com/USERNAME/sample_app.git
$ git push -u origin master

$ heroku create
$ git push heroku master
$ heroku run rake db:migrate

$ git checkout -b static-pages
$ rails generate controller StaticPages home help --no-test-framework

ファイル

  • config/routes.rb
  • app/views/static_pages/home.html.erb
  • app/views/static_pages/help.html.erb

テスト

$ rails generate integration_test static_pages
  • spec/requests/staticpagesspec.rb

テスト

$ open http://railstutorial.jp/book/ruby-on-rails-tutorial?version=4.0#code-capybara_dsl
  • spec/spec_helper.rb

テスト

$ bundle exec rspec spec/requests/static_pages_spec.rb
  • app/views/layouts/application.html.erb
  • app/controllers/staticpagescontroller.rb
  • config/routes.rb
  • app/views/static_pages/help.html.erb
  • app/views/static_pages/home.html.erb
  • app/views/static_pages/about.html.erb
  • app/helpers/application_helper.rb

Asset Pipeline互換の行を追加する。

$ open http://railstutorial.jp/book/ruby-on-rails-tutorial?version=4.0#code-bootstrap_config
$ vi config/application.rb
$ open http://railstutorial.jp/book/ruby-on-rails-tutorial?version=4.0#code-bootstrap_css
$ vi app/assets/stylesheets/custom.css.scss

パーシャル

$ open http://railstutorial.jp/book/ruby-on-rails-tutorial?version=4.0#code-layout_with_partials
$ vi app/views/layouts/application.html.erb
$ open http://railstutorial.jp/book/ruby-on-rails-tutorial?version=4.0#code-stylesheets_partial
  • app/assets/images/rails.png

カスタムドメイン

$ heroku apps:rename sample-app-XXXX
$ heroku domains:add myapp.example.com
(DNS Providerで myapp.example.orgのCNAMEを sample-app-XXXX.herokuapp.com にする。
$ host myapp.example.com

新規にメモアプリを作る

cd rails_projects
rvm list
rvm use 2.0.0@railstutorial_rails_4_0 --create --default
rails new memo_app
cd memo_app
git init
git add .
git commit -m "Initial commit."
git remote add origin git@bitbucket.org:MYNAME/MYAPPNAME.git
git push -u origin --all
git push -u origin --tags

open http://railstutorial.jp/book/ruby-on-rails-tutorial?version=4.0#code-gitignore
vi .gitignore
git status
git add .
git commit -m "Ignore more files for git."
git push

open http://railstutorial.jp/book/ruby-on-rails-tutorial?version=4.0#code-final_gemfile
vi Gemfile
git diff
git add .
git commit -m "Include Bootstrap in Gemfile."
git status
git push

bundle install --without production
bundle update
bundle install

git checkout -b scaffold
git branch
rails generate scaffold Memopost memo_title:string memo_content:text
bundle exec rake db:migrate
rails s &
open http://localhost:3000/memoposts
git status
git add .
git commit -m "Create Memopost."
git checkout master
git merge scaffold
git branch
git push origin master
git branch -d scaffold

git checkout -b static-pages
rails generate controller StaticPages home help --no-test-framework
rails destroy controller StaticPages home help
rails generate controller StaticPages home about --no-test-framework
vi app/assets/stylesheets/custom.css.scss
open http://railstutorial.jp/book/ruby-on-rails-tutorial?version=4.0#code-bootstrap_config
vi config/application.rb
open http://railstutorial.jp/book/ruby-on-rails-tutorial?version=4.0#code-layout_with_footer
vi app/views/layouts/application.html.erb
open http://railstutorial.jp/book/ruby-on-rails-tutorial?version=4.0#code-stylesheets_partial
vi app/views/layouts/_shim.html.erb
open http://railstutorial.jp/book/ruby-on-rails-tutorial?version=4.0#code-header_partial
vi app/views/layouts/_header.html.erb
open http://railstutorial.jp/book/ruby-on-rails-tutorial?version=4.0#code-footer_partial
vi app/views/layouts/_footer.html.erb
rails s &
open http://railstutorial.jp/book/ruby-on-rails-tutorial?version=4.0#code-title_helper
vi app/helpers/application_helper.rb
git status
git add .
git commit -m "Add Custom CSS, Static Pages, header, footer."
git checkout master
git branch
git merge static-pages
git push
git branch -d static-pages

git checkout -b secret
open http://railstutorial.jp/book/ruby-on-rails-tutorial?version=4.0#code-secret_token
vi config/initializers/secret_token.rb
git status
git add .
git commit -m "Use dynamic secret token."
git checkout master
git branch
git merge secret
git branch -d secret

git checkout -b route
open http://railstutorial.jp/book/ruby-on-rails-tutorial?version=4.0#code-root_route
vi config/routes.rb
git status
git add .
git commit -m "Fix root."
git checkout master
git branch
git merge route
git branch -d route

heroku login
heroku create
git push heroku master
heroku run rake db:migrate
heroku open
heroku domains:add memo.textfile.org
# Setup CNAME at DNS provider.
heroku domains:remove memo.textfile.org
heroku domains:add memoapp.textfile.org
open http://memoapp.textfile.org/