HerokuでRuby on Railsを立ち上げてみる(Windows)
HerokuでRuby on Railsを立ち上げてみるお話
Herokuの導入に関しては前回のエントリを。
【解決編】Heroku Toolbeltのセットアップをしようとしたが失敗した(Windows8) - tkoyama1988の”なんか作ってみる”
RailsのWEBアプリケーションを用意する
RubyとRailsは導入済みとして、まずはRailsでアプリケーションの雛形を生成する。アプリ名はtestapp以外の別名(ユニークなもの)の方がいい。herokuでアプリ名を決めるときにほかの誰かと重複するとアプリ名とディレクトリ名を別名にしなければならなくなるため。
$ cd [管理しやすいフォルダパス]
$ rails new testapp
$ cd testapp
Railsアプリの雛形が生成されるので、アプリのディレテクトリ内に移動する。
今度はGemfileを書き換える。
$ vim Gemfile
(中略)
group :development, :test do
gem 'sqlite3'
end
group :production do
gem 'pg'
end
gem 'therubyracer', platforms :ruby
最初はsqliteとpgの設定とtherubyracerのコメント解除だけしておく。
$ bundle install
Gemfileを書き換えたのでbundle install。
次はproduction.rbを修正。
$ vim config/environments/production.rb
(中略)
config.assets.compile = true
assetsファイルをコンパイルする。これをやっておかないとHerokuなどの本番環境(production)で公開するときにエラーが出るらしい。
Rails 3.2のproduction環境でisn't precompiledと出る場合の対処 - Qiita [キータ]
というわけで、テストサーバー(port:3000)で起動してみる
$ rails s
でhttp://localhost:3000/にアクセス。うん、動いた。
HerokuでWEBアプリケーションを公開する
Herokuにtestappを公開する作業に取り掛かる。ログインしてない場合はログインする。ついでにブラウザのHerokuサイトも。
$ heroku login
Herokuに公開するということはアプリのプロジェクトディレクトリをHerokuにアップロードする必要がある。
そのため、SSHの公開キーの設定を行う。
$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/tkoyama1988/.ssh/id_rsa):
/c/Users/tkoyama1988/.ssh/id_rsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/tkoyama1988/.ssh/id_rsa.
Your public key has been saved in /c/Users/tkoyama1988/.ssh/id_rsa.pub.
The key fingerprint is:
3c:1a:64:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx
~/testapp (master)
$ heroku keys:add
Found existing public key: C:/Users/tkoyama_com/.ssh/id_rsa.pub
Uploading SSH public key C:/Users/tkoyama_com/.ssh/id_rsa.pub... done
$ heroku create testapp
次はGitの設定。
$ git config --global user.email "hoge@hege.hoge"
$ git config --global user.name "tkoyama1988"
$ git init
$ git add .
$ git commit -m "test"
Gitのmailと名前を設定し、ディレクトリ内をgitで登録する。git add .でディレクトリ内全体をgitに登録して、git commitでコミットする。
最後にherokuにアップロードするが、なんかエラーでた。
$ git push heroku master
Enter passphrase for key '/c/Users/tkoyama1988/.ssh/id_rsa':
Initializing repository, done.
Counting objects: 61, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (49/49), done.
Writing objects: 100% (61/61), 14.37 KiB, done.
Total 61 (delta 2), reused 0 (delta 0)
! Push rejected, no Cedar-supported app detected
To git@heroku.com:testapp.git
! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'git@heroku.com:testapp.git'
StackOverflowに解決策が載ってた。Django(Python)だけど、解決策は同じっぽい。
rm -rf .git git init git add . git commit -m "First commit" heroku create --stack cedar git push heroku master
というわけで、Herokuにアップロードできました。Herokuにログインして、アプリケーションが六角形マーク(中にマイナスマークがない状態)なら成功です。
がしかし。アップロードは成功したものの、アプリケーションの詳細ページに入って、アプリ名の横にある「Open Application」のアイコンを押すと「The page you were looking for doesn't exist.」。...お、おう
とりあえず、productionモードで問題が発生しているようなので、ローカルでproductionモードを動かしてみる。
$ rails s -e production
でlocalhost:3000にアクセス。当然ながら同じページ。
[2013-12-12T15:30:38.616534 #6604] FATAL -- :
ActionController::RoutingError (No route matches [GET] "/"):
routeがうまくいってないらしい。developmentモードなら普通に動くのに...
良いQAサイトがありました。
こちらのサイトを見つけるのに苦労した。多くの人はRails3で作られているのだろうか。Rails4でのproductionモードは/(ルート)に対するパスがないといけないらしい。public/配下にindex.htmlを置くとエラーにはならなくなる。
ということで、仮のindex.htmlを置いて、動かすことができた。今回の目標はあくまでHerokuで動かしてみることだから。
作り込んでいく際に、きちんと/(ルート)のパスを設定すれば問題ないだろう。
参考として、dotinstallのパス設定の説明がされている動画リンクを載せておく。
#09 rootの設定をしよう | Ruby on Rails 4入門 - プログラミングならドットインストール
関係ないが、dbがうまくいってない場合の解決策も見つかった。