LoginSignup
0
2

More than 5 years have passed since last update.

[学習用]Rails5+Herokuで挫折した私でもRailsで作成したアプリをHerokuへ送って動かすまでの手順

Last updated at Posted at 2018-05-24

heroku04.png

何度もやり直して、挫折しそうになったHerokuがやっと、稼働できるようになりました。
新しく作り直して同じ手順で、WEB稼働できるようになったのでメモ。

用意しておくもの

Vagrant UbuntuにPostgresqlを入れておく
Vagrant UbuntuにHeroku toolbeltを入れておく
Vagrant UbuntuにGit を入れておく(入ってるかも?)

Postgresで準備

Postgresのユーザーを作成

$ psql --version
$ sudo -u postgres createuser -s pguser
$ sudo -u postgres psql

pguser というユーザーを作成しました。

Postgresのパスワードを作成

postgres=# \password pguser
postgres=# \du
postgres=# \q

2回パスワードを入力。
例えば pasword って2回入力すると パスワードが password になります。
\du でユーザーの一覧を表示できますので、pguserがあるか確認しておきます。
\q で脱出。

Railsでアプリを作成

アプリを作成

$ rails new myherokuapp -d postgresql
$ cd myherokuapp

Postgresqlを指定しておく。

Postgresqlにテーブルを作成

$ rake db:create

$ sudo -u postgres psql
postgres=# CREATE DATABASE myherokuapp_development;
postgres=# CREATE DATABASE myherokuapp_test;
postgres=# \q
開発用のサーバーでテーブルを作成しておくとRailsサーバー起動したときにエラーでない。

rails に専用コマンドあった。

Gemfileを修正

# gem 'sqlite3'
gem 'pg'

pgへ修正
(Rails作成時に postgresqlを指定しておけば初めからpgだった)

$ bundle install

database.ymlに追記

config/database.yml
default: &default
  adapter: postgresql
  encoding: unicode
  # For details on connection pooling, see Rails configuration guide
  # http://guides.rubyonrails.org/configuring.html#database-pooling
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  # この3つを追加する。
  # ただし先にPostgresにpguserとパスワードを作成する必要がある
  host: localhost
  username: pguser
  password: password

コントローラーを作成

$ rails g controller welcome index

viewのindexを編集

app/views/welcome/index.html.erb
<h2>Hello World</h2>
<p>
  The time is now: <%= Time.now %>
</p>

rails g controller welcome を
rails g controller welcome index
にすると view に index.html.erb も作成された。
たぶんindex.html.erb が無いので作る。

routesの編集

config/routes.rb
  root 'welcome#index'

サーバー起動で動作チェック

$ rails s

動作チェックします。

Git設定

Gitの初期化と登録

$ git init
$ git add .
$ git commit -am "make it better"

Heroku設定

Herokuにアプリを作成

$ heroku create

ランダムな名前のアプリ用URLが発行されます。
createの後にアプリ名を入れるとアプリ名になりますが
必ずほかに無いユニークな名前で作成する必要があります。

Herokuへアップロード

$ git push heroku master

データベースを作成

$ heroku run rake db:migrate

これを行うとHerokuでPostgresのデータベースを作成してくれる様です。
add-onで入れなくてPostgresが入っていました。

これでHerokuから発行されたURLをブラウザで表示させれば
無事にHerokuで表示されることでしょう。

heroku02.png

もしHerokuにPostgresが入ってなかったら
https://dashboard.heroku.com/apps > dashboard > 該当アプリのURL > Configure Add-ons > Postgres でインストール

ファイルを更新してみる

$ git add .
$ git commit -am "make it better"
$ git push heroku master

heroku03.png

無事に反映されました。やったよ!

前に作ったサンプルアプリをPostgresに変更

  • Gemfileの変更
  • config/database.ymlの変更

この変更後に

$ rake db:migrate

これでデータベースがPostgresに変わる。
前のデータはSQLiteに入れてるのでPostgresは空の状態。

さて無事にHerokuへアップできるだろうか。

無事アプリをアップ

heroku04.png

無事にアプリをアップすることが出来ました。
ありがとうございます。

◆追記
学習用に作成した記事作成から、コメント登録まで。
Bootstrap4とFontawesam5入れたサンプルアプリもアップ出来ました。

次は、ログイン機能を実装してみたい。

参考サイト

Heroku>Deploy>Deploy using Heroku Git
 ┗こっちの方が簡単で確実だった。ただProcfileっての作ってない。
参考:#05 Railsアプリケーションを作ろう
 ┗こっちの手順はうまく出来なかった。Procfileを作った。
RubyMineとHerokuのPostgresを繋げる方法
 ┗これはうまくHerokuのデータベースに接続できた。
Getting Started with Rails 5.x on Heroku
 ┗途中まで試した。

0
2
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
0
2