0
votes

I have implemented CarrierWave to upload profile pictures for users in my application and MiniMagick to resize multiple versions.

image_uploader.rb

class ImageUploader < CarrierWave::Uploader::Base
 include CarrierWave::MiniMagick
  storage :file

  def store_dir
   "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

  version :thumb_card do
   process :resize_to_fit => [190, 210]
  end

  version :thumb_profile, from_version: :thumb_card do
   process :resize_to_fit => [100, 100]
  end

  version :thumb, from_version: :thumb_card do
   process :resize_to_fit => [60, 60]
  end

  def extension_white_list
   %w(jpg jpeg gif png)
  end

  def content_type_whitelist
   /image\//
  end
end

user.rb

mount_uploader :image, ImageUploader

Also added :image in permitted user_params.

html.erb

<%= image_tag(current_user.image.thumb_profile.url, { :alt=> 'Image', 
    :id=>'avatar_img' ,:style=>'padding-top: 5px;' }) if current_user.image? %>
<%= user.file_field :image %>

All this is working perfectly on local server. But when I push to my EC2 instance, the images fail to upload and every transaction is rolled back

error

User Exists (1.5ms)  SELECT  1 AS one FROM `users` WHERE 
`users`.`email` = '[email protected]' AND (`users`.`id` != 4) LIMIT 1

User Exists (1.4ms)  SELECT  1 AS one FROM `users` WHERE 
`users`.`phone` = '431 532 5413' AND (`users`.`id` != 4) LIMIT 1

(1.3ms)  ROLLBACK

Can't find the public/assets/uploads folder on the EC2 server. I guess it won't generate until an upload is successful?

Would like someone to point out why the exact code is working locally, but not live? TIA.

1

1 Answers

1
votes

FIXED

After some debugging through rails console on the EC2 instance, found out why the image wasn't uploading.

Followed this link

https://chuanhesmile.wordpress.com/2014/12/13/issue-fixed-rollback-transaction-when-uploading-images-using-carrierwave/

It was a MiniMagick issue. Uninstalled it and reinstalled it without success. Guess there were some dependencies. Wasn't properly installed on EC2. Got this error in rails console.

original error imagemagick/graphicsmagick is not installed

This command did the trick.

sudo yum install ImageMagick