RubyOnRailsで作成したアプリをHerokuにデプロイする方法をここでは紹介します。
本記事はHeroku上にデプロイすることを目的としているので、作成するアプリは超簡単に
トップ画面に「Hello world」を表示したものになります。
なお、RubyOnRails・Heroku・Gitの準備は終わっているものとします。
目次
1.RubyOnRailsアプリ作成
2.Herokuにデプロイ
1.RubyOnRailsでのアプリ作成
(1)アプリ新規作成
terminal
$ rails new sample
(2)コントローラーとindexアクションの作成
terminal
$ rails g controller top index
(3)ルーティングの設定
(Path) config/route.rb
Rails.application.routes.draw do root 'top#index' end
↑トップページに対応するコントローラとアクションがTopControllerのindexアクションになります。
(4)Viewの作成
(Path) views/top/index.html.erb
<h1>Hello World</h1> <p>Find me in app/views/top/index.html.erb</p>
これで「Hello World」がトップ画面に表示されるようになります
2.Herokuにデプロイ
(1)Heroku側でデプロイの受け入れ先を確保
terminal
$ heroku create sample
↑sampleの箇所に作成したアプリ名を書いて下さい。
(2) デプロイしたリポジトリを初期化
terminal
$ git init
(3) コミットするファイルを追加
terminal
$ git add .
(4) コミット
terminal
$ git commit -m "first commit"
(5) remote herokuの設定
削除
terminal
$ git remote rm heroku
登録
terminal
$ git remote add heroku git@heroku.com:sample.git
(4) デプロイ
terminal
$ git push heroku master
(5) ブラウザで見る
terminal
$ heroku open
※ 今回はdbを用意していないですがherokuで動かす場合は下記のコマンドで
マイグレートする必要があります
terminal
$ heroku run rake db:migrate
以上、簡単ではありますがHerokuにデプロイする方法をまとめてみました。