11
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Rails本番環境(AWS)S3に画像投稿できなかった

Posted at

####はじめに
初学者のアウトプット備忘録記事です。
ご指摘アドバイスあればよろしくお願いします。

####今作ってるもの
現在記事投稿アプリを作ってます。
(タイトル、説明文章、カテゴリ、画像、日時が投稿されます。)
ローカル環境では問題なく投稿でき画像もS3に保存されています。

しかし、デプロイ後、本番環境で動かしてみたらcreateアクションにて値がsaveされずにリダイレクトしていまう。ということでハマりました。

本番環境AWS
ストレージS3
ruby2.5.1
rails5.2.3

###本番環境で画像投稿できない。

まず確認したのはサーバー側でproduction.log をcatコマンドで確認しました。

#var/www/アプリ名/current/log

コマンド入力
[ec2-user@ip-000-00-00-00 log]$ cat production.log

こう表示されました。

production.log
INFO -- :  Started POST "/products" 
INFO -- :  Processing by ProductsController#create as HTML
INFO -- :  Parameters: {"utf8"=>"✓", "authenticity_token"=>"yT4Vwpfnf+ARsYq1+ug==", "product"=>{"name"=>"ああああああ", "image"=>#<ActionDispatch::Http::UploadedFile:0x @tempfile=#<Tempfile:/tmp/RackMultipart201902-1824-l30mg9.jpg>, @original_filename="51DOPJPZH8L.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"product[image]\"; filename=\"51DOPJPZH8L.jpg\"\r\nContent-Type: image/jpeg\r\n">, "description"=>"ああああああ", "category_id"=>"1"}, "commit"=>"投稿する"}
DEBUG -- : User Load (0.3ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 2 ORDER BY `users`.`id` ASC LIMIT 1
DEBUG -- : (0.1ms)  BEGIN
DEBUG -- : (0.1ms)  ROLLBACK
DEBUG -- : (0.1ms)  BEGIN
DEBUG -- : (0.1ms)  ROLLBACK

下の方見るとrollbackしていました。
ローカル側で確認したらrollbackの記述はなくcreateしていました。
エラー文章もなく、値は受け取れてるのに何故保存されないのだろうと、色々ググったらこちらの記事が出てきました。

Railsでlogを出力しdebugする

参考にしてとりあえず、エラーが起きてないかlogで出力するようにしました。

####logでdebug

コントローラーにlogger.debug @product.errors.inspectを記述

products.controller.rb

  def create
    @product = Product.create(product_params)
    logger.debug @product.errors.inspect

    if @product.save
       redirect_to root_path ,notice: '記事が投稿されました'
    else
       render :new
    end    
  end

こちらも記述

produvtion.rb
  if ENV["RAILS_LOG_TO_STDOUT"].present?
    logger           = ActiveSupport::Logger.new(STDOUT)
    logger.formatter = config.log_formatter
    config.logger    = ActiveSupport::TaggedLogging.new(logger)
  end

####再びlog確認

エラーlogが出てきました。

#<ActiveModel::Errors: @base=#<Product id: nil, name: "aaaaaaaa", description: "てすと", user_id: 2, category_id: 1, likes_count: 0, created_at: nil, updated_at: nil, image: nil>, 
@messages={:image=>["Failed to manipulate with MiniMagick, maybe it is not an image? Original Error: You must have ImageMagick or GraphicsMagick installed"]}, @details={:image=>[{:error=>"Failed to manipulate with MiniMagick, maybe it is not an image? Original Error: You must have ImageMagick or GraphicsMagick installed"}]}>

こちらの文章そのままなんですが
:image=>["Failed to manipulate with MiniMagick, maybe it is not an image? Original Error: You must have ImageMagick or GraphicsMagick installed"

このエラー文でだいたい分かりました。
ImageMagickなくね?みたいな
下記を参考にしました。

Railsのmini_magickで、画像のアップロードに失敗する問題

サーバー側でコマンド入力してImageMagickインストールしましたら解決しました。

[ec2-user@ip-000-00-00-hoge ~]$ sudo yum install ImageMagick ImageMagick-devel

この後は本番環境で投稿しても問題なく値が保存され画像も表示されるようになりました。

今回はloggerを使うという発想がなくてハマりました。使ってみればエラー文で表示してくれるし内容もすぐ分かったので今後は活用していきたいと思います。

では

11
10
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
11
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?