LoginSignup
2
2

More than 3 years have passed since last update.

rails newしよう〜herokuでデプロイ〜

Last updated at Posted at 2019-09-22

バージョンを確認する

Rubyのバージョンを確認

$ ruby -v
ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-darwin18]

# コンソールで利用しているバージョン
$ rbenv versions
  system
  2.3.1
* 2.5.1 (set by /Users/ユーザーネーム/.rbenv/version)


# インストール可能なバージョンの確認
$ rbenv install --list

*rbenvって??・・・バージョンを管理してくれるツール

railsのバージョンを確認して、必要があればバージョンを変更する

# railsのバージョンを確認する
$ rails -v
Rails 5.2.3

# 希望のバージョンをインストール
$ cd ~ # ホームディレクトリで行う
$ gem install rails --version="5.0.7.2"

# gemの変更を反映させる
$ rbenv rehash

rails newのオプション

$ rails _5.0.7.2_ new appname -d mysql
  • railsのバージョン(5.0.7.2)を指定している(バージョンはお好きに)
  • newのあとにアプリケーション名(任意の名前)
  • データベースはmysqlで指定(決まっていなければ一旦なしでもOK)
  • 指定しないと、データベースのデフォルトはsqliteになる

今回は、Herokuでデプロイしたいので公式でバージョンなど確認を行う。
https://devcenter.heroku.com/articles/ruby-support#ruby-versions

今回はherokuでやるので以下コマンドで実行
rails _5.2.3_ new appname -d postgresql

*bundle skipつけるのか問題は軽く調べただけだとよくわからなかったので、また次に持ち越し

railsアプリの下準備

モデルの作成
$ rails g model map 

カラムを追加する

maps.rb
class CreateMaps < ActiveRecord::Migration[5.2]
  def change
    create_table :maps do |t|
      t.string :address, null :false
      t.timestamps
    end
  end
end

コントローラ作成で不要なファイルを生成したくないので、configに設定しておく。
(_test,_helper,_coffeeが作られない)

config/application.rb
module Appname
  class Application < Rails::Application
    config.load_defaults 5.2

    # 以下を追記
    config.generators do |g|
      g.javascripts false
      g.helper false
      g.test_framework false
    end

  end
end
コントローラの作成
rails g controller maps

ちなみにscaffoldだと
コントローラー、モデル、ビュー、ルーティングすべて用意してもらうこともできますが
初心者なので、完全にモデルなど作ることになれるまで使用は控えています。
モデルやコントローラーはコマンドでそれぞれ作っていきます。

routes.rb
Rails.application.routes.draw do
  root 'maps#index'
end

仮でindexを定義

maps_controller.rb
class MapsController < ApplicationController
  def index
  end
end

viewsには、index.html.erbのファイルを作成する。

DB初期設定
$ rails db:create db:migrate

Git管理

  • giithub desktopでレポジトリの追加を選択
  • add Existing Repository...を選択

image.png
*keep this code privateのチェックを外すのはお忘れなく。

postgreSQLをインストールする

HerokuのデータベースはPostgresを使うので入れます。
参考:https://qiita.com/matcham/items/e39bf9ac787b5d8d99ef
https://qiita.com/shi-ma-da/items/f3cab71bd22cee2b9f98
*homebrewを利用

入っているかの確認
$ psql --version
インストール
brew install postgresql
バージョンの確認
$ psql -V
psql (PostgreSQL) 11.5
インストール場所
$ which psql
/usr/local/bin/psql
起動
$ brew services start postgresql
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
データベースの起動状況確認
$ brew services list
Name       Status  User           Plist
mysql@5.6  started username/Users/username/Library/LaunchAgents/homebrew.mxcl.mysql@5.6.plist
postgresql started username /Users/username/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
停止
brew services stop postgresql
==> Successfully stopped `postgresql` (label: homebrew.mxcl.postgresql)
データベース一覧
psql -l

Heroku Toolbeltを導入する

herokuのパッケージ管理ツールを入れておきます。
*homebrew経由
heroku自体のインストールは公式から行なっておいてください。

インストール
$ brew tap heroku/brew && brew install heroku
herokuバージョン確認
$ heroku --version
heroku/7.30.0 darwin-x64 node-v11.14.0

これでターミナルからherokuの操作が可能に。

Herokuへデプロイ

https://devcenter.heroku.com/articles/heroku-cli
公式通りに進めていきます。

herokucliインストール
$ curl https://cli-assets.heroku.com/install.sh | sh

パスワードを入力するよう求められます。

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1892  100  1892    0     0  10395      0 --:--:-- --:--:-- --:--:-- 10453
This script requires superuser access.
You will be prompted for your password by sudo.
Password:
Installing CLI from https://cli-assets.heroku.com/heroku-darwin-x64.tar.xz
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 15.2M  100 15.2M    0     0  1683k      0  0:00:09  0:00:09 --:--:--  916k
v11.14.0
heroku installed to /usr/local/bin/heroku
heroku/7.30.0 darwin-x64 node-v11.14.0

アプリとherokuの紐付け

herokuへログイン
$ heroku login
heroku: Press any key to open up the browser to login or q to exit: 
Opening browser to https://cli-auth.heroku.com/auth/browser/9908f897-258d-4f1e-bd5d-5b5561a0798a
Logging in... done
Logged in as 登録したメアド

enterを押すと、ブラウザが開くのでログインをクリックするとターミナルに上記のような表示に切り替わる。

herokuでアプリcreate
$ heroku create appname

appnameは任意のものを入力します。urlとなります。

create成功の表示
Creating ⬢ appname... done
https://appname.herokuapp.com/ | https://git.heroku.com/appname.git
デプロイコマンド
$ git push heroku master
デプロイ成功
remote:        https://appname.herokuapp.com/ deployed to Heroku
remote: 
remote: Verifying deploy... done.
To https://git.heroku.com/mogulog.git
 * [new branch]      master -> master

最後にURLが生成されていたらOKです。
この部分がURLとなります。

https://appname.herokuapp.com/

本番環境もマイグレートする必要がありますので忘れずに。

本番環境のマイグレート
$ heroku run bundle exec rake db:migrate

URLにアクセスすると特にまだ何も書いていないので、白画面が表示されるはずです。

データベース

HerokuのデータベースURLを確認
$  heroku config --app appname
DATABASE_URL:     ~~~~~~これがURL~~~~~~

今まで、データベースの確認はSuquel Proを使っていましたが、PG Commanderを今回下記Webサイトを参考に導入しました。
https://dev.classmethod.jp/tool/pg-commander/

本番環境の検証データ削除
$ heroku pg:reset DATABASE_URL
$ heroku run rake db:migrate

データベースごと削除する場合は、herokuのアカウントページから行う。

migrateがaborteの時ためしたこと(DBごと削除してしまったとき)
$ heroku addons:create heroku-postgresql

Creating heroku-postgresql on ⬢ appname... free
Database has been created and is available
 ! This database is empty. If upgrading, you can transfer
 ! data from another database with pg:copy
Created postgresql-tetrahedral-02348 as DATABASE_URL
Use heroku addons:docs heroku-postgresql to view documentation

$ heroku run rails db:migrate

オプション操作

環境変数

環境変数の確認
$ heroku config
環境変数の追加
$ heroku config:set GOOGLE_API_KEY=文字列
環境変数の削除
$ heroku config:unset GOOGLE_API_KEY

よく使うコマンド

ログの表示
heroku logs -t 
Heroku再起動
heroku restart
デプロイコマンド
$ git push heroku master
本番環境のマイグレート
$ heroku run bundle exec rake db:migrate

参考

✳️heroku公式ドキュメント 
 https://devcenter.heroku.com/categories/reference

✴️heroku CLIコマンド
 https://e-tec-memo.herokuapp.com/article/3/

参考にさせていただいた記事
https://qiita.com/kazukimatsumoto/items/a0daa7281a3948701c39#heroku%E3%81%A8%E3%81%AF
http://vdeep.net/rubyonrails-heroku

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