LoginSignup
20
22

More than 5 years have passed since last update.

Heroku + Devise 3.1 でエラー

Last updated at Posted at 2013-10-21

この記事は古い情報です

「解決法」のuser-env-compileは廃止されたので使用できなくなっています。

未確認ですが、Devise 3.2.3以上とRails 4.1以上の組み合わせで、以下の記事のようにすると解決できるかもしれません。

Deviseの新しい設定

Devise 3.1で新しくsecret_keyという設定ができました。
コメントに書いてある通り、パスワードリセットやアンロック時のトークンを暗号化するためのキーです。

config/initializers/devise.rb
Devise.setup do |config|
  # The secret key used by Devise. Devise uses this key to generate
  # random tokens. Changing this key will render invalid all existing
  # confirmation, reset password and unlock tokens in the database.
  config.secret_key = '6ed69597b149896749aafb6ab11afa38f9fb6ea7b09c0d7a2f96655d9fc702e4bfeff9d915f889528c1b0080600964a144cc27ce195265a62c788754f7a70aab'

もっと詳しくはこちら
Devise 3.1: Now with more secure defaults | Plataformatec Blog

例のごとく、GitHubでpublicにするにはまずいかな、と思ってこうしたわけです。

config/initializers/devise.rb
  config.secret_key = ENV['DEVISE_SECRET_KEY']

Herokuで動かすため、configでENVの設定。

$ heroku config:set DEVISE_SECRET_KEY=6ed69597b149896749aafb6ab11afa38f9fb6ea7b09c0d7a2f96655d9fc702e4bfeff9d915f889528c1b0080600964a144cc27ce195265a62c788754f7a70aab

そしてHerokuにpushするとこんなエラーが…。

-----> Preparing app for Rails asset pipeline
       Running: rake assets:precompile
       rake aborted!
       Devise.secret_key was not set. Please add the following to your Devise initializer:
       config.secret_key = '92af6d16d7e27c5df9403064b7876f39a832ad35fb3dfb47501a5981fd7058380d2d7951bbdedc360bb8e53206b597d78ad433952040932d0f7e465f21c511a2'
       Please ensure you restarted your application after installing Devise or setting the key.

なぜ

Environment variables are not available to your app at compile time. This means if you need them for your asset compilation, the task may not run properly.

ということで、Herokuのconfig設定によるENVは、アプリケーションの実行時だけ有効で、asset compile時には効かないのです。

解決法

上記のDev Centerのページにもリンクがありますが、user-env-compileという機能があります。

This Heroku Labs feature adds experimental support for having an app’s config vars be present in the environment during slug compilation.

For example, because Rails 3.1 couples configuration with initialization, the Rails 3.1 assets:precompile task performs much more reliably in the presence of the runtime config.

有効にするにはこのようにするだけ。

$ heroku labs:enable user-env-compile

ただし、heroku configでENVを変更しても再コンパイルされるわけではないので、ちゃんと設定してからpushするように注意しましょう。

余談

stacktraceによると、config/routes.rb内でdevise_forがあるのでそこで引っかかっているようす。
ならasset precompileでroutesを見なきゃいいんでは、と思ってそれを無効にする方法を最初調べていたんだけど見つからず。
asset pathにroutesが関係してくるから、それはたぶん無理なんでしょうね。

20
22
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
20
22