Rails 的超小型筆記 (5)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 部署到 Heroku
- 安裝 Heroku Gem
gem install heroku

- 第一次使用時提交 ssh public key 和帳號資料
heroku keys:add

- 將目前的專案加進 Git 版本管理
git init
git add .
git commit -m "first version"

- 進立 App (自動生成一個 App 網址)
heroku create

- 第一次提交到 Heroku App 上
git push heroku master

- 建立 DB 資料
heroku rake db:migrate

- 下次更新時就提交
git push heroku
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# 將 SQLite 換成 Postgresql
- 進入終端
heroku console

- 找出 Database DSN String
ENV['DATABASE_URL']

- 格式解說

"postgres://username:password@hostname/database"

- 修改 config/database.yml

production:
adapter: postgresql
encoding: unicode
username: username
port: 5432
host: hostname
database: database
password: password

- 修改 Gemfile 檔案
(Production: postgresql, Development: sqlite)

group :production do
gem 'pg'
end

group :development, :test do
gem 'sqlite3'
end

- 重新執行 bundler
RAILS_ENV=production bundle

- 提交
git add Gemfile
git add Gemfile.lock
git add config/database.yml
git commit -m 'updated production to postgresql'
git push heroku
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# Assets 預處理 (錯誤: isn't precompiled)
- 修改 config/environment/production.rb

config.assets.compile = true

- 修改 config/application.rb

config.assets.enabled = true

- 重新預編譯
RAILS_ENV=production bundle exec rake assets:clean
RAILS_ENV=production bundle exec rake assets:precompile

- 提到交 Heroku
git add public/assets
git commit -m 'vendor compiled assets'

git add config/environment/production.rb
git commit -m 'updated config.assets.compile to true on production env'

git add config/application.rb
git commit -m 'updated config.assets.enabled to true'

git push heroku
1
2
# 如果在發現 Apps 沒有正常出現,可以通過以下指令查看
heroku log
1
2
3
4
5
6
7
# Heroku 中更名
- 登入後台 General Info 輸入新名字並送出
- 又或者 heroku rename [newname]
- 手動更新 Git Repo

git remote rm heroku
git remote add heroku [email protected]:[newname].git
1
2
3
4
# Heroku 更改 Database 資料
heroku console
> User.find(1)
> User.find(1).update_attribute(:gender, 1)
1
2
3
4
5
6
#參考資料
http://devcenter.heroku.com/articles/quickstart
http://devcenter.heroku.com/articles/database
http://devcenter.heroku.com/articles/rails31_heroku_cedar
http://devcenter.heroku.com/articles/renaming-apps
http://stackoverflow.com/questions/7300532/blueprint-css-rails-3-1-help