I'm having issues uploading images to my Rails app. I've downloaded ImageMagick and installed the Paperclip gem, but I keep getting the following error:
[paperclip] An error was received while processing: #<Paperclip::Errors::NotIdentifiedByImageMagickError: Paperclip::Errors::NotIdentifiedByImageMagickError>
I have the following line in my development.rb file:
Paperclip.options[:command_path] = 'C:\Program Files (x86)\GnuWin32\bin'
And here's what my model looks like:
class Movie < ApplicationRecord
belongs_to :user
has_attached_file :image, styles: { medium: "400x600" }
validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/
end
I'm running Rails version 5.0.0 and on Windows 10. I've reinstalled ImageMagick several times, but still no luck. And I've tried solutions I found on this site and others, but I still keep getting the error. Can someone tell me what I'm doing wrong?
Related
I've created a new project with Rails 5.0.0 and Ruby 2.5 on my local mac with macOs 10.12. Now I'm trying to add paperclip to this project but still no luck.
My model class looks like this:
class Photo < ApplicationRecord
has_attached_file :file, styles: { big: '1280x1024>', small: '640x480>' }
validates_attachment :file, content_type: { content_type: /\Aimage/ }, file_name: { matches: [/png\Z/i, /jpe?g\Z/i, /gif\Z/i] }, size: { less_than: 15.megabytes }
end
Whatever version of paperclip I try I don't have a paperclip generator in my project and I'm geting errors like this when I try to call a model or it's methods:
"NoMethodError (undefined method `has_attached_file' for Photo (call 'Photo.connection' to establish a connection):Class)"
When I add "include Paperclip::Glue" like suggested here https://github.com/thoughtbot/paperclip/issues/705 error changes to
NameError (uninitialized constant Photo::Paperclip)
Is there any way for me to bypass this mess ? ><
Link to paperclip issues
https://github.com/thoughtbot/paperclip/issues/2555
Link provided by hamdi in the first comment is an answer.
If you've got problems with Devise or with Paperclip like that, don't try to add "include Paperclip::Glue" or in case of Devise "extend Devise::Models". The only right way to fix this is to rollback ALL of your migrations, terminate console, start it again, migrate, terminate console one more time and thats it! Sounds stupid but it's working >< Pictures upload is working and paperclip generator is on the list.
For all of you who came from Rails 4:
2.5.0 :001 > Photo
=> Photo (call 'Photo.connection' to establish a connection)
is totally ok behavior for console in Rails 5. Before accessing model you've got to perform 'Photo.connection' from now on. If you don't like it you can always fix it by adding
console do
ActiveRecord::Base.connection
end
to your config/application.rb
I'm working on a rails web application. Just created and prepared some models and stuff. The application is very simple. But now I have a problem by setting up the gem paperclip to add attachments to a model. Almost everything works fine, like attaching images/jpg or even pdf.
But I can't upload zip files. I tried different zip files, but I'm always getting:
"Attachment Paperclip::Errors::NotIdentifiedByImageMagickError"
This is my model:
class Order < ActiveRecord::Base
has_attached_file :attachment, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
validates_attachment_content_type :attachment, :content_type => ["application/pdf", "application/zip", "application/x-zip", "application/x-zip-compressed","application/octet-stream","image/jpg","image/png"]
belongs_to :client
has_one :status
end
I'm developing on a Mac (Yosemite), installed imagemagick via brew and using SQLite.
I added this to my Gemfile:
gem "paperclip", "~> 4.2"
gem 'cocaine', '~> 0.5.4'
I did research on google the last hours, and there are many people struggling with paperclip, but I didn't find anybody with problems uploading zip files.
Maybe someone can help here.
Thanks
ImageMagick can't read .zip files. See the acceptable file types here:
http://www.imagemagick.org/script/formats.php
If you're trying to generate a thumbnail from a zip file ImageMagick should fail every time.
Try adding this to your model:
before_post_process :skip_for_zip
def skip_for_zip
! %w(application/zip application/x-zip).include?(asset_content_type)
end
Then your app won't try to process zip files as images
I've got a problem with a custom paperclip processor within my rails app. I upload a soundfile and the processor creates an waveform image via a shell command (provided by this gem)
I'm running RoR 3.2.7 / Ruby 1.9.3 on Ubuntu 12.04 (production environment). My model with paperclip attachment looks like the following:
# encoding: utf-8
class Track < ActiveRecord::Base
has_attached_file :original_file,
:styles => { :waveform_image => { :waveform => true } },
:processors => [:sound_processor],
:storage => :s3,
:s3_credentials => "#{Rails.root.to_s}/config/s3.yml",
:url => ":s3_eu_url",
:path => "sounds/:id/:five_digit_id_:basename_:style.:extension"
end
The corresponding Paperclip Processor:
class Paperclip::SoundProcessor < Paperclip::Processor
def initialize file, options = {}, attachment = nil
# cut for brevity
end
def make
src = #file
dst = Tempfile.new([#basename, #current_format])
dst.binmode
if #waveform
cmd = "waveform #{File.expand_path(src.path)} #{File.expand_path(dst.path)}"
Paperclip.log(cmd)
out = `#{cmd}`
dst = File.open "#{File.expand_path(dst.path)}"
end
dst
end
end
When the command
waveform #{File.expand_path(src.path)} #{File.expand_path(dst.path)}
is getting executed on the production machine (Ubuntu 12.04), the following error comes up:
/usr/bin/env: ruby: No such file or directory
However the usr/bin/env is a file instead of an directory. Since there is no ruby executable, how can I pass the right location when executing the shell command?
PS: On my development machine (OSX), on usr/bin/env is a copy of my rails app directory. It's working like a charm in development. I appreciate your help!
The error message is indicating that the ruby executable was not found. It is not related to the code that you have included in the question as of now.
The error is coming from the script file that includes the waveform #{File.expand_path(src.path)} #{File.expand_path(dst.path)} line.
Check out these answers to the issue:
- https://stackoverflow.com/a/6126419/429758
- https://stackoverflow.com/a/5241638/429758
- https://stackoverflow.com/a/5241593/429758
- https://stackoverflow.com/a/1064319/429758
They should give you some idea as to why the ruby executable is not being found on your production environment.
I can no longer render pages with paperclip urls in my Rails application. I recently updated my gem bundle. I'm using Rails 3.2.8 and Paperclip 3.1.4. I was using Paperclip 2.7.0 before.
The link in my view that is failing is:
ad.image.url(:medium)
My Ad model has these declarations:
Paperclip.interpolates :ad_subdomain do |attachment, style|
attachment.instance.brand.subdomain
end
has_attached_file :image,
:default_url => '/images/blank.gif',
:styles => { :medium => ["290x230>","jpg"],
:thumb => ["100x100>","jpg"] },
:storage => :file,
:path => "/mcp/ads/:style/:ad_subdomain/:basename.:extension"
The error that is being thrown is:
TypeError: wrong argument type Class (expected Module)
from /Users/me/.rvm/gems/ruby-1.9.3-p0#mcp5/gems/paperclip-3.1.4/lib/paperclip/attachment.rb:368:in `extend'
from /Users/me/.rvm/gems/ruby-1.9.3-p0#mcp5/gems/paperclip-3.1.4/lib/paperclip/attachment.rb:368:in `initialize_storage'
from /Users/me/.rvm/gems/ruby-1.9.3-p0#mcp5/gems/paperclip-3.1.4/lib/paperclip/attachment.rb:80:in `initialize'
from /Users/me/.rvm/gems/ruby-1.9.3-p0#mcp5/gems/paperclip-3.1.4/lib/paperclip/instance_methods.rb:5:in `new'
from /Users/me/.rvm/gems/ruby-1.9.3-p0#mcp5/gems/paperclip-3.1.4/lib/paperclip/instance_methods.rb:5:in `attachment_for'
from /Users/me/.rvm/gems/ruby-1.9.3-p0#mcp5/gems/paperclip-3.1.4/lib/paperclip.rb:191:in `block in has_attached_file'
For some reason, I had:
:storage => :file
in my declaration. That does not work. If you want to use the filesystem, you just leave out the storage line altogether and it will default.
Check out the requirements listed on the Paperclip github page:
Paperclip now requires Ruby version >= 1.9.2 and Rails version >= 3.0
(Only if you're going to use Paperclip with Ruby on Rails.)
If you're still on Ruby 1.8.7 or Ruby on Rails 2.3.x, you can still
use Paperclip 2.7.x with your project. Also, everything in this README
might not apply to your version of Paperclip, and you should read the
README for version 2.7 instead.
I've installed the paperclip gem for a Rails 3 application. Everything works fine in development mode. However, when running in production mode, if I upload a file and then try to download it again, it downloads a file with the correct name and extension, but it is an empty file. When looking on the server, the file does get uploaded and is in the correct directory. (I have an "uploads" folder in my application root.)
Anyone had this happen?
My model:
# app/models/document.rb
class Document < ActiveRecord::Base
belongs_to :kase
has_attached_file :document, :path => (Rails.root + "uploads/:class/:kase_id/:id").to_s, :url => ":class/:id"
validates_attachment_presence :document
validates_attachment_content_type :document, :content_type => [
'application/pdf',
'image/png',
'image/jpeg',
'image/pjpeg',
'text/plain'
]
end
My controller:
# app/controllers/documents_controller.rb
class DocumentsController < ApplicationController
respond_to :html
before_filter :initialize_kase # Sets the #kase instance
def show
#document = #kase.documents.find(params[:id])
send_file #document.document.path, :filename => #document.document_file_name, :content_type => #document.document_content_type
end
end
And my initializer (setting the :kase_id placeholder used in has_attached_file above:
# config/initializers/paperclip.rb
Paperclip.interpolates('kase_id') do |attachment, style|
"kases/#{attachment.instance.kase.id.to_s}"
end
I should probably mention, too, that I am accessing this as a nested controller (/kases/XX/documents/XX). Not sure if that has an effect or not...
If you are using Apache and Passenger, (possibly other servers as well) and have the line:
config.action_dispatch.x_sendfile_header = "X-Sendfile"
in your production.rb env file, then you have two options:
Install the apache module mod-xsendfile
Comment out that line and let Rails send the files instead of Apache, like it does in development mode.
Are you carrying over the uploads directory each time you deploy your app to production? Assuming that you're using capistrano (or similar) for deployment, each time you deploy you might be creating a new uploads directory in the newly-deployed release directory. In that case, the previously-uploaded files are residing in older deployed releases (if you didn't delete those) and would no longer be accessible to your app.
You want to create e.g. shared/uploads directory that is symlinked into your app on each deploy.