LoginSignup
9
8

More than 5 years have passed since last update.

Heroku に Rails 4.1.7 アプリをデプロイする

Last updated at Posted at 2014-11-10

環境

  • Ruby 2.1.4p265
  • Rails 4.1.7

サンプル

参照

Rails 4 on Heroku | Heroku Dev Center
Getting Started with Rails 4.x on Heroku | Heroku Dev Center
Creating a 'Deploy to Heroku' Button | Heroku Dev Center

仕込み

アプリの作成

サンプルのRailsアプリを作成します。

rails _4.1.7_ new blog_heroku -B # bundle install しない
cd blog_heroku

Heroku(production)環境用にGemfileの「sqlite3」行を書き換えます。

group :development do
  gem 'sqlite3'
end
group :production do
  gem 'pg'
  gem 'rails_12factor'
end
bundle install --without production
bundle exec rails g scaffold Post title body:text
db/seeds.rb
1.upto(5) do |i|
  Post.create(title: "title #{i}", body: "body #{i}")
end
bundle exec rake db:migrate db:seed
config/routes.rb
Rails.application.routes.draw do
  resources :posts
  root 'posts#index'
end
bundle exec rails s # ローカル環境で動作確認

Herokuへデプロイ

git init
git add .
git commit -m 'Initial commit'
heroku create
heroku config:add SECRET_TOKEN="$(bundle exec rake secret)"
heroku config # コンフィグの確認
git push heroku master
heroku run rake db:migrate db:seed
heroku open
heroku logs -t # ログの確認
heroku run rails c
irb(main):002:0> Post.count
=> 5
irb(main):002:0> Post.first
=> #<Post id: 1, title: "title 1", ...

Herokuアプリの削除

heroku info # アプリ名の確認
heroku destroy -a APP

Herokuボタン

mv README.rdoc README.md
README.md
[![Deploy](https://www.herokucdn.com/deploy/button.png)](https://heroku.com/deploy)
app.json
{
  "scripts": {
    "postdeploy": "bundle exec rake db:migrate db:seed"
  },
  "env": {
    "RAILS_ENV": "production",
    "SECRET_TOKEN": {
      "description": "A secret key for verifying the integrity of signed cookies.",
      "generator": "secret"
    }
  },
  "addons": ["heroku-postgresql"]
}
git add .
git commit -m 'Heroku button'
git push origin master

このページにボタンを置く

GitHub外にボタンを配置するのでtemplateでリポジトリを明示的に指定します。

[![Deploy](https://www.herokucdn.com/deploy/button.png)](https://heroku.com/deploy?template=https://github.com/usutani/blog_heroku)

Deploy

9
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
8