Ruby on Rails + Heroku でWebサイト構築
dotcloudにRailsのプロジェクトをデプロイしようとするとtimeoutになるので(サポート問い合わせ中。。。)一旦Herokuを使う事に。
私はHerokuのアカウントは前から持っているので、登録は割愛。
Heroku | Cloud Application Platform
1. 環境の確認
Mac OSX 10.8.2 Mountain Lion
$ ruby -v
ruby 1.9.3p327 (2012-11-10 revision 37606) [x86_64-darwin12.2.0]
$ rvm -v
rvm 1.17.0 (master) by Wayne E. Seguin
Mountain LionのデフォルトのRubyは1.8.7 なので、
$ /usr/bin/ruby -v
ruby 1.8.7 (2012-02-08 patchlevel 358) [universal-darwin12.0]
1.9.3を使うために、RVMをインストール。
インストールしたら、~/.bash_profileに以下が記載されている事を確認(無ければ追記)
\curl -L https://get.rvm.io | bash -s stable --ruby
rvm get head
rvm reload
rvm use 1.9.3 --default # 1.9.3を使用してデフォルトに
-s "$HOME/.rvm/scripts/rvm" && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
eval "$(rbenv init -)"
eval "$(rbenv init -)"
eval "$(rbenv init -)"
eval "$(rbenv init -)"
eval "$(rbenv init -)"
eval "$(rbenv init -)"
eval "$(rbenv init -)"
eval "$(rbenv init -)"
eval "$(rbenv init -)"
eval "$(rbenv init -)"
or $ echo '[ [ -s "$HOME/.rvm/scripts/rvm" ] ] && . "$HOME/.rvm/scripts/rvm" # Load RVM function' >> ~/.bash_profile
# rvmの状態を確認
$ rvm listrvm rubies
# => - current
# =* - current && default
# * - default
2. ローカル環境にRailsアプリを作成
# Herokuで動かすために「Gemfile」のgem 'sqlite3'を変更
$ cd /Develop/Rails
$ rails new railsapp
$ cd railsapp
#ローカル環境の構築
$ vi Gemfile
source 'https://rubygems.org'
gem 'rails', '3.2.9'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
#gem 'sqlite3'
group :production do
gem 'pg'
end
group :development, :test do
gem 'sqlite3'
end
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.1'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', :platforms => :ruby
gem 'uglifier', '>= 1.0.3'
end
gem 'jquery-rails'
# To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'
# To use Jbuilder templates for JSON
# gem 'jbuilder'
# Use unicorn as the app server
# gem 'unicorn'
# Deploy with Capistrano
# gem 'capistrano'
# To use debugger
# gem 'debugger'
scaffoldを使ってテーブルを一つ作成してみておく
$ bundle install
4. scaffoldを利用した開発(1) | TECHSCORE(テックスコア)
こちらのサイトから、scaffoldの概要を確認して同様にitemテーブルを作ってみる
$ rails g scaffold item name:string price:integer description:text
(「rails g」は「generate」の「g」らしい)
そして、マイグレーションと呼ばれるコマンドを叩く事によって実際にテーブルが作成される、と。
$ rails g scaffold item name:string price:integer description:text
invoke active_record
create db/migrate/20121129190553_create_items.rb
create app/models/item.rb
invoke test_unit
create test/unit/item_test.rb
create test/fixtures/items.yml
invoke resource_route
route resources :items
invoke scaffold_controller
create app/controllers/items_controller.rb
invoke erb
create app/views/items
create app/views/items/index.html.erb
create app/views/items/edit.html.erb
create app/views/items/show.html.erb
create app/views/items/new.html.erb
create app/views/items/_form.html.erb
invoke test_unit
create test/functional/items_controller_test.rb
invoke helper
create app/helpers/items_helper.rb
invoke test_unit
create test/unit/helpers/items_helper_test.rb
invoke assets
invoke coffee
create app/assets/javascripts/items.js.coffee
invoke scss
create app/assets/stylesheets/items.css.scss
invoke scss
identical app/assets/stylesheets/scaffolds.css.scss
なるほど。
※以下参考サイトより抜粋
マイグレーションは、railsコマンドではなく、rakeコマンドを使います。
Rakeとは、Rubyで実装されたビルドツールです。「タスク」と呼ばれる実行単位で処理を定義しておき、rakeコマンドを使って実行します。
Ruby1.9をインストールすると、同時にインストールされていますので、再度インストールする必要はありません。
Railsには、rakeコマンドで実行できるタスクが、すでにいくつか用意されています。今回行うマイグレーションもその一つです。以下のコマンドを入力して、マイグレーションを実行します。
# マイグレーションの実行
$ rake db:migrate
== CreateItems: migrating ====================================================
-
- create_table(:items)
-> 0.0205s
== CreateItems: migrated (0.0206s) ===========================================# ローカルのRailsを起動
「s」は「server」の「s」だろう
$ rails s
http://localhost:3000/itemsをチェック
参考サイトと同じ見た目になっている事を確認
2. Herokuとの接続
# まずはHerokuコマンドのインストール
# Herokuにログイン
$ gem install heroku bundler
# パスワードを毎回聞かれなくするために、公開鍵/秘密鍵の設定
$ heroku login
Enter your Heroku credentials.
Email: ****@gmail.com
Password (typing will be hidden):
Authentication successful.
通常であれば、$ heroku keys:add だけで良いようだが、私の場合いくつものSSHの設定を持っている(社内LAN、AWS、GitHub)ので
~/.ssh/configに細工が必要(最初permission deniedになりました)
なので作り直しました。ファイル名:~/.ssh/heroku_id_rsa
$ ssh-keygen -t rsa -C "***@gmail.com"
$ ssh-add
$ ssh-add -l
Could not open a connection to your authentication agent.$ ssh-add id_dsa"
Enter passphrase for ~/.ssh/heroku_id_rsa:
Identity added: ~/.ssh/heroku_id_rsa
Host github.com
User git
Port 22
Hostname github.com
IdentityFile ~/.ssh/github_id_rsa
TCPKeepAlive yes
IdentitiesOnly yes
Host heroku.com
User git
port 22
Hostname heroku.com
IdentityFile ~/.ssh/heroku_id_rsa
TCPKeepAlive yes
IdentitiesOnly yes
# 登録されている事を確認します。
# Herokuへのキー登録
$ ssh-add -l
1024 b4:76:79:7c:7d:41:48:a4:d1:92:c5:07:7f:22:50:71 /Users/###/.ssh/heroku_id_dsa (DSA)
# Herokuに登録されているか確認する
$ heroku keys:add /Users/###/.ssh/heroku_id_rsa.pub
※フルパスが必要らしい
# プロジェクトをGitの管理下にする
$ heroku keys
ssh-rsa AIUFAxxNzaC...vBl##iVzc7 ***@gmail.com
# Heroku上にアプリを作成する
$ git init
$ git add .
$ git commit -m "Init Heroku Project"
# Herokuにリリースする
$ heroku create --stack cedar
$ git push heroku master
git push heroku master
Counting objects: 54, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (34/34), done.
Writing objects: 100% (35/35), 4.86 KiB, done.
Total 35 (delta 6), reused 0 (delta 0)
-
-
-
-
- > Heroku receiving push
-
-
-
# Heroku上でDBマイグレーション
# 実行
$ heroku run rake db:migrate
Running `rake db:migrate` attached to terminal... up, run.7963
DEPRECATION WARNING: You have Rails 2.3-style plugins in vendor/plugins! Support for these plugins will be removed in Rails 4.0. Move them out and bundle them in your Gemfile, or fold them in to your app as lib/myplugin/* and config/initializers/myplugin.rb. See the release notes for more on this: http://weblog.rubyonrails.org/2012/1/4/rails-3-2-0-rc2-has-been-released. (called fromat /app/Rakefile:7)
DEPRECATION WARNING: You have Rails 2.3-style plugins in vendor/plugins! Support for these plugins will be removed in Rails 4.0. Move them out and bundle them in your Gemfile, or fold them in to your app as lib/myplugin/* and config/initializers/myplugin.rb. See the release notes for more on this: http://weblog.rubyonrails.org/2012/1/4/rails-3-2-0-rc2-has-been-released. (called fromat /app/Rakefile:7)
Connecting to database specified by DATABASE_URL
Migrating to CreateNotes (20121129181155)
Migrating to CreateItems (20121129190553)
== CreateItems: migrating ====================================================
- create_table(:items)
NOTICE: CREATE TABLE will create implicit sequence "items_id_seq" for serial column "items.id"
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "items_pkey" for table "items"
-> 0.1416s
== CreateItems: migrated (0.1418s) ===========================================
最終的にターミナルに表示される「http://xxx.herokuapp.com/items」 でローカル環境(http://localhost:3000/items)と同じか確認する
$ heroku open
※HerokuのDashBoardからも確認可能