user984621
user984621

Reputation: 48503

Rails - trying to open a file, but getting MiniMagick::Invalid

I am trying load a file in controller, like

  Avatar.all.each do |avatar|
     if avatar.avatar_file_name
       file = "lib/data/#{avatar.avatar_file_name}"

       image = MiniMagick::Image.open("#{file}")
       ...
     end
   end 

But whenever I run this code, I get the error message

MiniMagick::Invalid

I already tried to reinstall the imagemagick as is mentioned here, but it didn't really help me.

Where could be a problem? Am I missing a component or something? Thank you

Upvotes: 1

Views: 1833

Answers (1)

Ch4rAss
Ch4rAss

Reputation: 754

Maybe you should check if file exists first

if File.exist?(file)
  image = MiniMagick::Image.open(file)
end

To get path to the file you should do something like this:

file = "#{Rails.root}/lib/data/#{avatar.avatar_file_name}"

btw lib is not the best place to save your images.

Upvotes: 2

Related Questions