デプロイするための準備
Gemfileの設定
以下を記述します。使うGemの場合分けを行うためです。
Gemfile
gem 'sqlite3', group: :development
gem 'pg', group: :production
gem 'rails_12factor', group: :production
database.ymlの設定
データベースの設定を行います。
config/database.yml
production:
<<: *default
adapter: postgresql
encoding: unicode
pool: 5
SSLを使う場合は
config/environments/production.rb
config.force_ssl = true
エラーログにあったので
一度デプロイしようとしたらエラーにbundlerを入れろ的なことが英語で書いてあったので入れます。
$ gem install bundler
Heroku側の操作
Herokuへのアカウント登録や、Heroku Toolbeltのインストールは軽く調べればすぐできるので省略します。Toolbeltとは、Herokuコマンドをターミナルで使えるツールのことです。
Herokuにログイン
$ heroku login
ここでemailとパスワード入力を指示されるので入力します。
Heroku側でアプリを作成する
$ heroku create 自分でつけるアプリの名前
デプロイする。
$ git init
$ git add .
$ git commit -m "first commit"
$ git push heroku master
データベース作成の際には
$heroku run rake db:migrate
デプロイできたか確認
heroku open
これでデプロイ完了です。
メール機能を利用したい場合
$ heroku addons:create sendgrid:starter
$ heroku addons:add sendgrid:starter
config/environments/production.rb
Rails.application.configure do
...
# 追加
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
host = '<Herokuアプリ名>.herokuapp.com'
config.action_mailer.default_url_options = { host: host }
ActionMailer::Base.smtp_settings = {
:address => 'smtp.sendgrid.net',
:port => '587',
:authentication => :plain,
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:domain => 'heroku.com',
:enable_starttls_auto => true
}
...
end
おまけ
デプロイしてレイアウトが崩れてブチキレそうになった(実際キレた)時はこうしたらいけます。cssのロードがうまく行われない場合があるようです。
$ rake assets:precompile