LoginSignup

This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

More than 5 years have passed since last update.

13.4.4 本番環境での画像アップロードのエラー対処法

Last updated at Posted at 2018-09-02

13章で画像のアップロード機能を追加した際にherokuにデプロイ出来なくなる事があります。
その対処法について説明していきます。

リスト 13.69: 本番環境での画像アップロードを調整する

app/uploaders/picture_uploader.rb
 class PictureUploader < CarrierWave::Uploader::Base
  include CarrierWave::MiniMagick
  process resize_to_limit: [400, 400]

  if Rails.env.production?
    storage :fog
  else
    storage :file
  end

  # アップロードファイルの保存先ディレクトリは上書き可能
  # 下記はデフォルトの保存先  
  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

  # アップロード可能な拡張子のリスト
  def extension_whitelist
    %w(jpg jpeg gif png)
  end
end

チュートリアル本文より

production?という論理値を返すメソッドは環境毎に保存先を切り替えるメソッドです
storage :fogは開発環境に保存先を設定している
storage :fileは本番環境に保存先を設定している

上のファイルを下のようにコメントアウトするとherokuにpushできるようになる

app/uploaders/picture_uploader.rb
 ## if Rails.env.production?
   ## storage :fog
  ##else
    storage :file
  ##end
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