Ruby on Rails
を仕事で使うかもなので、Ruby on Rails チュートリアルで勉強します。
環境
項目 | 値 |
---|---|
ホストOS | Windows 7 Enterprise 64bit |
ゲストOS | CentOS 6.5 64bit |
仮想化ソフト | VirtualBox 4.3.12 |
Ruby | 2.0.0 |
その他 | Vagrant 1.6.3 |
第1章 ゼロからデプロイまで
各種インストール
-
/etc/resolv.conf
にoptions single-request-reopen
を追加-
rails
のインストールが早くなる
-
yum update
-
epel
のリポジトリを追加 yum install git vim
curl -L https://get.rvm.io | bash -s
yum install libyaml-devel libxslt-devel libxml2-devel sqlite-devel nodejs
rvm install 2.0.0 --with-openssl-dir=$HOME/.rvm/usr
-
~/.gemrc
に下記記載を追加install: --no-rdoc --no-ri
update: --no-rdoc --no-ri
rvm use 2.0.0@railstutorial_rails_4_0 --create --default
gem update --system 2.0.3
gem install rails --version 4.0.4
最初のアプリケーションを作成
$ mkdir rails_projects
$ cd rails_projects
$ rails new first_app
Gemfile
Gemfileの見方
Gemfile
# Rubyのバージョンを指定(rvm利用時のみ)
ruby '2.0.0'
# 最新のバージョンをインストール
gem 'jquery-rails'
# 特定のバージョンをインストール
gem 'jquery-rails', '3.0.4'
# development環境のみインストール
group :development do
gem 'sqlite3', '1.3.8'
end
# 1.3.0以上で最新のバージョンをインストール
gem 'uglifier', '>= 1.3.0'
# 4.0.0でマイナーなアップグレードを行う
# 4.0.1にはアップグレードされるが、4.1にはアップグレードされない
gem 'coffee-rails', '~> 4.0.0'
# ただし、gemにおいてはマイナーバージョンアップでもバグることがよくあるので、
# チュートリアルにおいてはバージョンをピンポイントで指定
Gemfileの変更
Gemfile
source 'https://rubygems.org'
ruby '2.0.0'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.0.4'
# Use sqlite3 as the database for Active Record
group :development do
gem 'sqlite3', '1.3.8'
end
# Use SCSS for stylesheets
gem 'sass-rails', '4.0.2'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '1.3.0'
# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails', '4.0.1'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
gem 'therubyracer', platforms: :ruby
# Use jquery as the JavaScript library
gem 'jquery-rails', '3.0.4'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks', '1.1.1'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '1.0.2'
group :doc do
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '0.3.20', require: false
end
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
# Use unicorn as the app server
# gem 'unicorn'
# Use Capistrano for deployment
# gem 'capistrano', group: :development
# Use debugger
# gem 'debugger', group: [:development, :test]
Gemfileからgemをインストール
# Using ...:既にインストール済み
# Installed ...:新規にインストール (was ...で過去にインストールしていたバージョンが表示される)
$ bundle update
$ bundle install
ローカルWebサーバの起動
$ rails server
- エラーが発生する場合の対応
# 下記エラーが発生してしまう場合
/usr/local/rvm/gems/ruby-2.0.0-p481@railstutorial_rails_4.0/gems/execjs-2.2.1/lib/execjs/runtimes.rb:51:in `autodetect': Could not find a JavaScript runtime. See https://github.com/sstephenson/execjs for a list of available runtimes. (ExecJS::RuntimeUnavailable)
# その場合はtherubyracerをインストール(Gemfileに追加してもよい)
# https://github.com/sstephenson/execjs
$ gem install therubyracer
# それでも駄目な場合、Node.jsなどをインストールする(epelを使用)
$ sudo yum install -y nodejs
gitでコミット
gitの初期設定
# gitがインストール済みである前提
# ユーザ名とメールアドレスを設定
$ git config --global user.name "asam-3"
$ git config --global user.email hoge@hoge.com
# gitのエイリアスを設定(ここではチェックアウト)
$ git config --global alias.co checkout
# gitのコミットメッセージを入力するエディタを指定
$ git config --global core.editor "vim"
リポジトリのセットアップ
# リポジトリの作成
$ git init
# .gitignoreの編集
# Railsドキュメント、エディタのスワップファイル等を除外
$ vim .gitignore
# 変更内容
17,22d16
<
< # Ignore other unneeded files.
< doc/
< *.swp
< *~
< .project
追加とコミット
# 追加
$ git add .
# ステージングエリアのファイルを確認
$ git status
# コミット
$ git commit -m "Initialize repository"
# コミットログの確認
$ git log
GitHubにコミット
- 事前にGitHubのアカウントを作成し、first_appのリポジトリを作成しておく
SSHの鍵を作成し、GitHubアカウントに設定する
SSHの鍵作成手順
$ ssh-keygen -t rsa -C "hoge@hoge.com"
$ eval "$(ssh-agent -s)"
$ ssh-add ~/.ssh/id_rsa
# ~/.ssh/id_rsa.pub の内容をGitHubに登録
- GitHubにコミット
$ git remote add origin https://asam-3@github.com/asam-3/first_app.git
$ git push -u origin master
- READMEファイルの更新
# トピックブランチの作成
$ git co -b modify-README
# 現在のブランチの確認(modify-READMEが選択されている)
$ git branch
# README.rdoc→README.mdに変更(git mvにより、README.mdは新規ではなく変更とみなされる)
$ git mv README.rdoc README.md
# README.mdを編集
$ vim README.md
# ブランチの状態を確認
$ git status
# コミット
$ git commit -a -m "Improve the README file"
[modify-README a5be022] Improve the README file
2 files changed, 3 insertions(+), 28 deletions(-)
create mode 100644 README.md
delete mode 100644 README.rdoc
# マスターブランチに切り替え
$ git co master
# modify-READMEの変更内容をマスターブランチに反映
$ git merge modify-README
Updating 8c3ca40..a5be022
Fast-forward
README.md | 3 +++
README.rdoc | 28 ----------------------------
2 files changed, 3 insertions(+), 28 deletions(-)
create mode 100644 README.md
delete mode 100644 README.rdoc
# modify-READMEブランチの削除
$ git branch -d modify-README
# GitHubに変更を反映
$ git push
Herokuへのデプロイ
Herokuのアカウント作成と設定
- Herokuのユーザ登録(https://id.heroku.com/signup/www-home-top)
- Heroku Toolbeltのインストール
# Heroku Toolbeltのインストール
$ wget -qO- https://toolbelt.heroku.com/install.sh | sh
# Heroku Toolbeltのパスを通す
$ vim ~/.bash_profile
# 変更内容
10c10
< PATH=/usr/local/heroku/bin:$PATH:$HOME/bin
---
> PATH=$PATH:$HOME/bin
# 動作確認
$ heroku version
heroku-toolbelt/3.9.6 (x86_64-linux) ruby/2.0.0
本番環境用にgemをGemfileに追加
Gemfile
group :production do
gem 'pg', '0.15.1'
gem 'rails_12factor', '0.0.2'
end
Herokuへのデプロイ準備
# 本番用のgemをインストールしないようにオプションを追加してインストール
$ bundle install --without production
# Gemfile.lockをコミット
$ git commit -a -m "Update Gemfile.lock for Heroku"
# Herokuにログイン
$ heroku login
# Herokuサーバ上にサンプルアプリケーション用の場所を作成
$ heroku create
# Herokuにサンプルアプリケーションをデプロイ
$ git push heroku master
# アプリケーション名を変更
$ heroku rename railstutorial-hogehoge