Images not showing - Rails 4 (using Paperclip gem) - ruby-on-rails

I am trying to get images to upload as flyers for events. I do have the parameters configured in the controller. No error is being shown, but the actual image is not rendering in the view (all you see is the filename).
When I upload an image file in production (hosting with Heroku) it saves properly and I can see the file name and size in the console. I can even go to my Amazon S3 console and look at the actual image from there.
This makes it hard for me to tell what's wrong.
I have a hunch that I'm about to try out (among many other things). The only thing I think it could be is that I haven't configured the url to the location of the file in my S3 bucket, but so far I haven't found out exactly how to do that...
In case you're wondering, I do believe I have the code for the view, controller, and model written properly. But the model does make me curious. Here it is just in case:
event.rb
has_attached_file :flyer,
styles: { medium: '300x430#', thumb: '123x170#' }
validates_attachment_content_type :flyer, :content_type => /\Aimage\/.*\Z/
private
def flyer_size
if flyer.size > 5.megabytes
errors.add(:flyer, 'must be less than 5MB')
end
end
Any ideas on what's going on?
EDIT:
When I inspect element this is what it shows.
<img src= "https://s3.amazonaws.com/[bucketname]/events/flyers/000/000/008/medium/JAM.jpg?1436400492" alt="Jam" >
::before
</img>

Related

Ruby On Rails: Get md5 hash of paperclip image

I am uploading an image to a ruby on rails server using a gem called paperclip. The image is uploaded correctly and is working. I am working on using md5 hashes so that a single request to the rails server can be made, returning to my script exactly what images need to be uploaded (I do not want duplicates).
In the script I am calculating the md5 successfully with
require 'digest/md5' and md5 = Digest::MD5.file(filename).hexdigest
On the server in order to check if the md5s are the same (image to be uploaded vs image on the server) I need to calculate the md5 of each paperclip image on the server.
Images are called TestImages and the model looks like this
class TestImage < ActiveRecord::Base
has_attached_file :image, styles: {thumbnail: '100x100', small: '350x350'}
validates_attachment :image, content_type: {content_type: ["application/octet-stream", "multipart/form-data", "image/jpg", "image/jpeg", "image/png", "image/gif"]}
belongs_to :build
belongs_to :test
end
People have said that paperclip has added the functionality of a fingerprint (md5 representation) but I am unsure as to how to set that up. It seems it is done automatically but needs to be stored as a column in the database? Here is one other post I was looking at Rails: How does MD5 checksum work in paperclip?
If it doesn't work with using paperclip (paperclip fingerprint) I could do the same digest/md5 method as in the script but I can't seem to find the full image path of the paperclip image.
Using this did not work
image.md5 = Digest::MD5.file(test_image.image.path).hexdigest
This gave the following error:
Errno::ENOENT (No such file or directory # rb_sysopen - /Users/scott.bishop/Code/visual-automation/public/system/test_images/images//original/testBasic_2x.png):
app/controllers/test_images_controller.rb:37:in `create'
I'm not sure what path it wants. Any help would be much appreciated.
It turns out that the paperclip image path works.
require 'digest/md5'
image.md5 = Digest::MD5.file(PAPER_CLIP_IMAGE.path).hexdigest

Broken images when seeding database on heroku

I have a Rails 4 application that uses paperclip to attach photos. My db/seeds.rb file adds some photos for my Person model with lines like this:
Person.create(:first_name => 'Jon', :last_name => 'Snow',
:photo => File.open("#{Rails.root}/app/assets/images/jon-snow.png))
In my app/models/person.rb file I have the :photo as a paperclip attachment where it is cropped and resized:
class Person < ActiveRecord::Base
has_attached_file :photo,
:styles => { :medium => "256x256#", :small => "64x64#", :tiny => "24x24#" },
:default_url => :set_default_avatar,
:url => "/assets/photos/:id/:style/:basename.:extension",
:path => ":rails_root/public/assets/photos/:id/:style/:basename.:extension"
validates_attachment_size :photo, :less_than => 5.megabytes
validates_attachment_content_type :photo, :content_type => ['image/jpeg', 'image/png']
My problem is that when I try to deploy this to heroku (with the Cedar stack) and then seed the database, the images all come up as broken. However, on my local computer everything comes up completely fine.
The broken image tag that is generated on heroku might look something like:
<img alt="Jon Snow" src="/assets/photos/21/small/jon-snow.png?1386825683">
Does anyone know why this link would break on heroku but not on my computer? I know that heroku is generating the resized photos because the heroku console outputs things similar to what is in this post: Seed images in heroku with paperclip.
However, it is putting them into public/assets instead of in public/assets/photos/:id/:style/:basename.:extension as the controller specifies.
I've tried doing what that linked post mentions, as well as running:
heroku run rake assets:precompile
and a number of other things, but nothing works. Any help would be greatly appreciated.
The issue is the same I described in this answer How to use paperclip with rails and how does it work in deployment?
The legacy Bamboo stack had a read-only file system so you were unable to write on the file-system.
In the new Cedar stack, the file system is no longer read-only, so the upload will not fail. However, you should keep using AWS or any other external storage because Heroku distributes your compiled application across several machines, and it's not guaranteed that the image will be visible from another request. They call it ephemeral filesystem.
In other words, you should keep using AWS or any other storage outside Heroku file-system.
From heroku-dev
The filesystem for the slug is read-only, which means you cannot
dynamically write to the filesystem for semi-permanent storage. The
following types of behaviors are not supported: Caching pages in the
public directory Saving uploaded assets to local disk (e.g. with
attachment_fu or paperclip)
Use AWS S3

rails paperclip - error processing thumbnail

I am running Rails 3.2.8 with Paperclip 3.3.0 on a Windows 7 machine. I am getting the following error when I try to upload an image named "2012-10-26_17.49.13.png": Image There was an error processing the thumbnail for 2012-10-26_17.49.1320121027-1196-l5ejgs
The filename may not be important, but I want to point out that most of the questions I find related to this say error processing the thumbnail for stream instead. No 'stream' on my error. The end of the filename changes randomly each time I try to upload (Paperclip doing some collision avoidance I assume).
The image field is set up in the model like this:
has_attached_file :image, :path => ":rails_root/public/images/:hash_:style.:extension", :url => "/images/:hash_:style.:extension", :hash_secret => "sometext", :default_url => "", :styles => { :thumb => "100x100#" }
validates_attachment_content_type :image, :content_type => ['image/jpeg','image/jpg','image/pjpeg','image/png','image/x-png','image/gif']
The has_attached_file is using :hash at the moment, but I also tried with :basefilename or whatever it was and that didn't affect the results. The :thumb style has been tried as "100x100>" as well.
The field is like this in _form.html.erb:
<div class="field">
<%= f.label :image %><br />
<%= f.file_field :image %>
</div>
And the controller has nothing special in it regarding :image because Paperclip seems to take care of everything for me. At least, that's what I gathered from looking at example code.
I think it might be a problem with ImageMagick. Some of the solutions I found for this error involved the installation location of ImageMagick. I moved mine to C:/ImageMagick6, updated my system path, and added the following lines to development.rb:
Paperclip.options[:command_path] = "C:/ImageMagick6"
Paperclip.options[:swallow_stderr] = false
Paperclip.options[:whiny_thumbnails] = true
I'm not entirely sure if the latter two are needed, but I saw them suggested. Didn't notice any changes from including them. If I open a command prompt, I can access the ImageMagick commands, so the path should be correct.
I also tried to run this with the :styles removed, which allowed me to upload the original image file. I really want the thumbnails for my site, though.
So, what have I missed that is keeping this from working?
Despite having added Paperclip.options[:command_path] = 'C:/progra~2/imagem~1.0-q' to my config/environments/development.rb file, I had to add C:/progra~2/imagem~1.0-q before %SystemRoot%\system32 in the environment path.
I confirmed this by revising the environment path back and forth a few times.
In order to use Paperclip you need ImageMagick library installed.
ImageMagick provides two executables that are required by Paperclip: identify to identify the image format and convert that process the original image and generate the desired formats.
Now, ImageMagick does not come by default with Windows, so you need to install it manually.
Later, you will require to tell Paperclip where it will find the ImageMagick executables, which is all documented in Paperclip README
Please note that if you don't set Paperclip.options[:command_path] to the PATH where ImageMagick was installed, Paperclip will incorrectly attempt to use Windows' convert executable, which is a Filesystem conversion utility and not meant to process images.
I believe that is the error you're currently receiving about generating the thumbnails.
Hope that helps.
You might try using ImageMagick directly via the command line to manipulate an image. At the very least, this should confirm whether or not there's an issue with your ImageMagick installation.

Paperclip won't display/detect file size of anything that is not an image

I'm working on an app that allows users to upload videos, pictures, audio clips, and documents. The files are being uploaded with the Paperclip gem and being stored on s3.
Right now I am running some processing on the file sizes after they are uploaded, but paperclip continues to save the file to the database with a file size of '0' on any file that is not .jpg, .gif, .png. I've tried inspecting the file size myself, the upload's length, and several other hair brained ideas.
Is there anyway to monkeypatch, or hijack the request from paperclip to find out what the file size is? Has anyone run into this issue before?
By default, Paperclip is built to process images. If you need to be able to handle other file types, you'll have to look into writing your own Processor: https://github.com/thoughtbot/paperclip/blob/master/lib/paperclip/processor.rb. This will allow you to process files in whatever way you need when declared in your model:
has_attached_file :scan, :styles => { :text => { :quality => :better } },
:processors => [:ocr]
Here's an important point from the README docs:
NOTE: Because processors operate by turning the original attachment
into the styles, no processors will be run if there are no styles
defined.
This means you'll need to specify some sort of styles in order for your processor or any other processor to get run. For a basic custom processor something as simple as :styles => { :default => true } should be enough to get you going.

Rails missing image

I am currently using paperclip to upload images to my rails app. This is probably a very simple fix but how or where do I save the missing images to? This is the error that is produced from not having any missing images. How do I change this?
ActionController::RoutingError (No route matches "/photos/normal/missing.png"):
If you don't need any control over the default image, which I don't think you need, you can place it in any folder under RAILS_ROOT/public/images/
Just make sure you point it out in the attachment model with the :default_url parameter. So for example if you place the image in RAILS_ROOT/public/images/normal/missing.png you need t specify the path like this:
has_attached_file :photo, :default_url => "/images/normal/missing.png"

Resources