LoginSignup
4
7

More than 5 years have passed since last update.

RailsアプリをHerokuに公開するまでの流れとエラー対処

Last updated at Posted at 2018-05-04

ドットインストールのHeroku入門を学習していて、ひっかかったところがいくつかあったので
公開までの流れと行った解決方法を記録します。

SPEC

macOS Sierra 10.12.6

$ brew -v
Homebrew 1.6.3
$ gem -v
2.7.6
$ ruby -v
ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-darwin16]
$ rails -v
Rails 5.2.0
$ git --version
git version 2.14.3 (Apple Git-98)

Macのbrewを使って、Herokuをインストール&ログイン

$ brew install heroku/brew/heroku
$ heroku --version
heroku/7.0.25 darwin-x64 node-v10.0.0
$ heroku login
Enter your Heroku credentials:

ログインはHerokuにアカウント登録したときのものです。

mymemoアプリを作成&webサーバー起動

HerokuはSQliteが使えないのでpostgreSQL指定します。

$ rails new mymemo -d postgresql
$ cd mymemo
$ rails server -b 192.168.3.107 -d

ちなみに、Gemfileファイルの中に「therubyracer」という行はなかったので修正しませんでした。

ブラウザ確認

http://192.168.3.107:3000/を見ると下記のようなエラー
could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
postgreSQLが動いていないようです。

postgreSQLをインストール&起動

$ brew install postgresql
$ postgres --version
postgres (PostgreSQL) 10.3
$ postgres -D /usr/local/var/postgres

再度ブラウザを確認

すると今度はこのようなエラーが。
FATAL: database "mymemo_development" does not exist
DBがないと。

mymemoディレクトリ直下に作成されたdbを見ると確かにseeds.rbしかないので、
dbディレクトリに移動してDBを作成。
$ postgresは起動したままだと他のコマンドが打てないので別ウィンドウでこれだけ起動させてた方が良さそうです。)

$ cd db
$ createdb mymemo_development

3度目の正直

もう一度ブラウザを確認するとやっとRailsマークが表示されてました。

アプリの中身を作成

mymemoディレクトリに戻ってアプリを作っていきます。

$cd ..
$ rails g scaffold Memo title:string body:text
$ rake db:migrate
$ rails s

gitを設定してコミット

$ git init
$ git add .
$ git commit -m "initial commit"

Heroku用にいくつかファイルを修正します。

$ vi Gemfile
#[最下行に
gem 'rails_12factor' , group: :production
を追記]
$ vi Procfile
#[このファイルは新規作成されるので、
web: bundle exec rails server -p $PORT
を追記]
$ git add .
$ git commit -m "Gemfile, Procfile update"

Herokuにアップロード

$ heroku create
$ git push heroku master
Counting objects: 116, done.

Delta compression using up to 4 threads.
Compressing objects: 100% (101/101), done.
Writing objects: 100% (116/116), 27.94 KiB | 2.15 MiB/s, done.
Total 116 (delta 4), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote:
remote:  !     Warning: Multiple default buildpacks reported the ability to handle this app. The first buildpack in the list below will be used.
remote:                         Detected buildpacks: Ruby,Node.js
remote:                         See https://devcenter.heroku.com/articles/buildpacks#buildpack-detect-order
remote: -----> Ruby app detected
remote: -----> Compiling Ruby/Rails
remote: -----> Using Ruby version: ruby-2.5.1
remote: -----> Installing dependencies using bundler 1.15.2
remote:        Running: bundle install --without development:test --path vendor/bundle --binstubs vendor/bundle/bin -j4 --deployment
remote:        Warning: the running version of Bundler (1.15.2) is older than the version that created the lockfile (1.16.1). We suggest you upg
rade to the latest version of Bundler by running `gem install bundler`.
remote:        You are trying to install in deployment mode after changing
remote:        your Gemfile. Run `bundle install` elsewhere and add the
remote:        updated Gemfile.lock to version control.
remote:
remote:        The dependencies in your gemfile changed
remote:
remote:        You have added to the Gemfile:
remote:        * rails_12factor

remote:        Bundler Output: Warning: the running version of Bundler (1.15.2) is older than the version that created the lockfile (1.16.1). We
 suggest you upgrade to the latest version of Bundler by running `gem install bundler`.
remote:        You are trying to install in deployment mode after changing
remote:        your Gemfile. Run `bundle install` elsewhere and add the
remote:        updated Gemfile.lock to version control.
remote:
remote:        The dependencies in your gemfile changed
remote:
remote:        You have added to the Gemfile:
remote:        * rails_12factor
remote:  !
remote:  !     Failed to install gems via Bundler.
remote:  !
remote:  !     Push rejected, failed to compile Ruby app.
remote:
remote:  !     Push failed
remote: Verifying deploy...
remote:
remote: !       Push rejected to tranquil-temple-90931.
remote:
To https://git.heroku.com/tranquil-temple-90931.git
 ! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'https://git.heroku.com/tranquil-temple-90931.git'

失敗。途中に、
remote: ! Failed to install gems via Bundler.
とあるので、bundle installが必要そう。

bundle install

mymemoディレクトリで実行してコミット。

$ bundle install --without production
$ git add .
$ git commit -a -m "Update gemfile.lock for heroku"

再度、Herokuにプッシュ

$ git push heroku master
・
・
・
 * [new branch]      master -> master

最後の[new branch]が出てきたらプッシュ完了です。

DB機能をアドオン

初期状態のHerokuにはDBが付いていないので、アドオンします

$ heroku addons:add heroku-postgresql

Herokuにテーブルを作成

$ heroku run rake db:migrate

最終確認

ブラウザで公開したアプリを確認します。

Heroku管理画面から[Open app]。
The page you were looking for doesn't exist.
トップページでは何も表示するものがないのでエラーが出てます。
/memos/ディレクトリを指定するとプッシュしたアプリがちゃんと見れました。

HEROKUに公開したアプリの情報を見たいとき

$ heroku apps:info
4
7
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
4
7