男女比はカレーと福神漬けと同じくらい

マサカリよろしくお願いします。

Rails+carrierwave+heroku+AWS S3で画像アップロードさせるときにハマったこと

# Gemfile

gem 'carrierwave'
gem 'fog-aws'
gem 'rmagick', require: 'RMagick'
# config/initializers/carrierwave.rb

if Rails.env.production?
  CarrierWave.configure do |config|
    config.storage = :fog
    config.fog_provider = 'fog/aws'
    config.fog_credentials = {
        :provider              => 'AWS',
        :aws_access_key_id     => ENV['S3_ACCESS_KEY_ID'],
        :aws_secret_access_key => ENV['S3_SECRET_KEY_ID'],
        :region                => ENV['S3_REGION'],
    }

    config.fog_directory = ENV['S3_BUCKET_NAME']
    config.fog_public = true
  end
else
  CarrierWave.configure do |config|
    config.storage = :file
  end
end

上記の Gemfilecarrierwave.rb を記述して git push heroku master すると以下のエラーが…

-----> Detecting rake tasks
-----> Preparing app for Rails asset pipeline
       Running: rake assets:precompile
       [DEPRECATION] requiring "RMagick" is deprecated. Use "rmagick" instead
       rake aborted!
       NameError: uninitialized constant CarrierWave::Storage::Fog
       /tmp/build_ef15e45299129328aea04600a8d0865a/vendor/bundle/ruby/2.2.0/gems/carrierwave-1.0.0/lib/carrierwave/uploader/configuration.rb:77:in `eval'
       /tmp/build_ef15e45299129328aea04600a8d0865a/vendor/bundle/ruby/2.2.0/gems/carrierwave-1.0.0/lib/carrierwave/uploader/configuration.rb:77:in `eval'
       /tmp/build_ef15e45299129328aea04600a8d0865a/vendor/bundle/ruby/2.2.0/gems/carrierwave-1.0.0/lib/carrierwave/uploader/configuration.rb:77:in `storage'
       /tmp/build_ef15e45299129328aea04600a8d0865a/config/initializers/carrierwave.rb:4:in `block in <top (required)>'
       /tmp/build_ef15e45299129328aea04600a8d0865a/vendor/bundle/ruby/2.2.0/gems/carrierwave-1.0.0/lib/carrierwave/uploader/configuration.rb:158:in `configure'
       /tmp/build_ef15e45299129328aea04600a8d0865a/vendor/bundle/ruby/2.2.0/gems/carrierwave-1.0.0/lib/carrierwave.rb:14:in `configure'
       /tmp/build_ef15e45299129328aea04600a8d0865a/config/initializers/carrierwave.rb:3:in `<top (required)>'
       /tmp/build_ef15e45299129328aea04600a8d0865a/vendor/bundle/ruby/2.2.0/gems/railties-5.0.1/lib/rails/engine.rb:648:in `block in load_config_initializer'
       /tmp/build_ef15e45299129328aea04600a8d0865a/vendor/bundle/ruby/2.2.0/gems/activesupport-5.0.1/lib/active_support/notifications.rb:166:in `instrument'
       /tmp/build_ef15e45299129328aea04600a8d0865a/vendor/bundle/ruby/2.2.0/gems/railties-5.0.1/lib/rails/engine.rb:647:in `load_config_initializer'
       /tmp/build_ef15e45299129328aea04600a8d0865a/vendor/bundle/ruby/2.2.0/gems/railties-5.0.1/lib/rails/engine.rb:612:in `block (2 levels) in <class:Engine>'
       /tmp/build_ef15e45299129328aea04600a8d0865a/vendor/bundle/ruby/2.2.0/gems/railties-5.0.1/lib/rails/engine.rb:611:in `each'
       /tmp/build_ef15e45299129328aea04600a8d0865a/vendor/bundle/ruby/2.2.0/gems/railties-5.0.1/lib/rails/engine.rb:611:in `block in <class:Engine>'
       /tmp/build_ef15e45299129328aea04600a8d0865a/vendor/bundle/ruby/2.2.0/gems/railties-5.0.1/lib/rails/initializable.rb:30:in `instance_exec'
       /tmp/build_ef15e45299129328aea04600a8d0865a/vendor/bundle/ruby/2.2.0/gems/railties-5.0.1/lib/rails/initializable.rb:30:in `run'
       /tmp/build_ef15e45299129328aea04600a8d0865a/vendor/bundle/ruby/2.2.0/gems/railties-5.0.1/lib/rails/initializable.rb:55:in `block in run_initializers'
       /tmp/build_ef15e45299129328aea04600a8d0865a/vendor/bundle/ruby/2.2.0/gems/railties-5.0.1/lib/rails/initializable.rb:44:in `each'
       /tmp/build_ef15e45299129328aea04600a8d0865a/vendor/bundle/ruby/2.2.0/gems/railties-5.0.1/lib/rails/initializable.rb:44:in `tsort_each_child'
       /tmp/build_ef15e45299129328aea04600a8d0865a/vendor/bundle/ruby/2.2.0/gems/railties-5.0.1/lib/rails/initializable.rb:54:in `run_initializers'
       /tmp/build_ef15e45299129328aea04600a8d0865a/vendor/bundle/ruby/2.2.0/gems/railties-5.0.1/lib/rails/application.rb:352:in `initialize!'
       /tmp/build_ef15e45299129328aea04600a8d0865a/config/environment.rb:5:in `<top (required)>'
       /tmp/build_ef15e45299129328aea04600a8d0865a/vendor/bundle/ruby/2.2.0/gems/railties-5.0.1/lib/rails/application.rb:328:in `require_environment!'
       /tmp/build_ef15e45299129328aea04600a8d0865a/vendor/bundle/ruby/2.2.0/gems/railties-5.0.1/lib/rails/application.rb:448:in `block in run_tasks_blocks'
       /tmp/build_ef15e45299129328aea04600a8d0865a/vendor/bundle/ruby/2.2.0/gems/sprockets-rails-3.2.0/lib/sprockets/rails/task.rb:62:in `block (2 levels) in define'
       /tmp/build_ef15e45299129328aea04600a8d0865a/vendor/bundle/ruby/2.2.0/gems/rake-12.0.0/exe/rake:27:in `<top (required)>'
       Tasks: TOP => environment
       (See full trace by running task with --trace)

解決法を探っているとfogのissueに同じような内容のものを発見。

Declaring only fog-aws in Gemfile: uninitialized constant CarrierWave::Storage::Fog · Issue #3429 · fog/fog · GitHub

 

carrierwave.rbの始めでいくつかのファイルをrequireしろと書いてあったので

# config/initializers/carrierwave.rb

require 'carrierwave/storage/abstract'
require 'carrierwave/storage/file'
require 'carrierwave/storage/fog'

if Rails.env.production?
  CarrierWave.configure do |config|
    …

書き足してからgit push heroku masterすると無事デプロイすることができました。

めでたしめでたし。