LoginSignup
17

More than 5 years have passed since last update.

Rails4のアプリをHerokuで動かす

Posted at

Rails4のアプリをHerokuで動かすまでにしたことをメモします。

一年前に書いた記事ですが、先ほど試してみたら同じ方法でできたので転載します。

1. Rails4 のプロジェクトを作る

Getting Started with Railsを参考にして Rails4 のプロジェクトを作りました。

念の為にバージョンもメモしておきます。

ruby
: ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-darwin12.4.0]

rails
: Rails 4.0.0

Mac OS X でやりました。

2. Heroku に登録

heroku.com に 登録して、Heroku のコマンドラインツールをインストールして、公開鍵を登録したような記憶があります。

(あまり記憶に無いので、たぶん指示に従っていけばいい感じだったと思います。)

3. production用のDB を sqlite3 → pg に変更

Heroku では SQLite3 に対応してないので、production用のデータベースに PostgreSQL(pg) を使うために Gemfileconfig/databae.ymlを編集します。

Gemfile
gem 'rails', '4.0.0'

# Use sqlite3 as the database for Active Record
-gem 'sqlite3'
+gem 'sqlite3', groups: %w(test development), require: false
+gem 'pg', groups: %w(production), require: false

# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.0'
config/database.yml
  timeout: 5000

production:
-  adapter: sqlite3
-  database: db/production.sqlite3
+  adapter: pg
+  database: db/production.pg

最後に、bundle installをします。

補足

ちなみに、SQLite3 のままで、git push herokuすると、こんな感じのエラーが起こりました。

       Gem files will remain installed in /tmp/build_8njhwta846es/vendor/bundle/ruby/2.0.0/gems/sqlite3-1.3.8 for inspection.
       Results logged to /tmp/build_8njhwta846es/vendor/bundle/ruby/2.0.0/gems/sqlite3-1.3.8/ext/sqlite3/gem_make.out
       An error occurred while installing sqlite3 (1.3.8), and Bundler cannot continue.
       Make sure that `gem install sqlite3 -v '1.3.8'` succeeds before bundling.
 !
 !     Failed to install gems via Bundler.
 !
 !     Detected sqlite3 gem which is not supported on Heroku.
 !     https://devcenter.heroku.com/articles/sqlite3
 !

 !     Push rejected, failed to compile Ruby/Rails app

To git@heroku.com:pacific-waters-7608.git
 ! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'git@heroku.com:pacific-waters-7608.git'

4. Git リポジトリを作る

普通に Git リポジトリを作ります。

git init
git add .
git commit -m "first commit"

5. Heroku に deploy する

heroku create # このとき、`heroku create アプリ名` で、任意のアプリ名にもできる。
git push heroku # Heroku への deploy は git の commit で行うよう。時間がかかる。
heroku open # ブラウザで確認できる。

補足

git push heroku に失敗して、heroku create しているうちに、アプリが5個を越えてしまったので、
heroku dashbord からアプリを消していたら、
push 先の heroku(リモートリポジトリ) が無いとかいうエラーになってしまいました。

git push heroku

 !  No such app as pacific-waters-7608.

fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

こうなってしまったら、.git/config を書き換えて、[remote "heroku"]の部分を消してから、もう一度 heroku createすればいいです。

もしくは、url の部分を 存在するアプリ名に書き換えてもいい気がします。

6. Heroku のデータベースで rake db:migrate する

heroku openして、動作を確認すれば分かると思いますが、このままだと Heroku 側のDBが空なので、エラーになります。

なので、Heroku で rake db:migrate します。

heroku run rake db:migrate

7. ハマった点

localでは動くのに、Herokuだとそのままだと上手くいかない場合があります。

そんな場合は、次のようにconfig/environments/production.rbの設定を書き換えて、もう一度git push herokuしましょう。

CSSやJSが読み込めない場合

config/environments/production.rb 内で config.assets.compile = trueにします。

config/environments/production.rb
   # config.assets.css_compressor = :sass

   # Do not fallback to assets pipeline if a precompiled asset is missed.
-  config.assets.compile = false
+  config.assets.compile = true

   # Generate digests for assets URLs.
   config.assets.digest = true

public 以下のファイルが404になる場合

config/environments/production.rb 内で config.serve_static_assets = trueにします。

config/environments/production.rb
   # config.action_dispatch.rack_cache = true

   # Disable Rails's static asset server (Apache or nginx will already do this).
-  config.serve_static_assets = false
+  config.serve_static_assets = true

   # Compress JavaScripts and CSS.
   config.assets.js_compressor = :uglifier

これでやっと Heroku で Rails4 を動かせるようになりました。

heroku open で確認しましょう。

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
17