I am getting an error when trying to upload images to my (listings) using the paperclip gem. The error that the browser outputs is: 1 error prohibited this listing from being saved: Image has contents that are not what they are reported to be **As a note, image magic has been successfully installed on my computer and there are no issues there
my listing.rb file
class Listing < ActiveRecord::Base
has_attached_file :image, :styles => { :medium => "200x", :thumb => "100x100>" }, :default_url => "default.jpg"
validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
end
my gemfile
gem "paperclip", "~> 4.3"
my listings_controller
def listing_params
params.require(:listing).permit(:name, :description, :price, :image)
end
end
and finally my form
<%= form_for #listing, :html => { :multipart => true } do |f| %>
...
...
<div class="form-group">
<%= f.file_field :image, class: "form-control" %>
</div>
<div class="form-group">
<%= f.submit class: "btn btn-primary" %>
</div>
<% end %>
It sounds like you need to include file on your system.
If you're using Windows, you need to download file from this URL, install it on your hard drive and then add it to your PATH environment var:
Click "Start"
On "Computer", right-click and select "Properties"
In Properties, select "Advanced System Settings"
Click the "Environment Variables" button
Locate the "PATH" var - at the end, add the path to your newly installed file.exe (typically C:\Program Files (x86)\GnuWin32\bin)
Restart any CMD shells you have open & see if it works
You're probably trying to attach a file that is not properly recognised as an image, or one that has an image extension and has different content (like a PDF, for example).
Some workarounds are discussed here: https://github.com/thoughtbot/paperclip/issues/1924
It might help to check the log file as well - it should tell you what Paperclip thinks the type of the attachment is.
Related
I am making a website where users can upload images and PDF files.
I am working with Ruby on Rails and paperclip. I have a site called "show" where the user can see his post with "Name, Description and the Picture". But now I want to make that everybody can upload images OR PDF files and that there is a download link for each file next to the picture. I am using the sqlite database.
And on the place where normally the picture is I want to have a PDF logo where they can click on to see the PDF file.
Can someone help me in this situation?
My model:
class Picture < ActiveRecord::Base
belongs_to :bill
has_attached_file :image, styles: { medium: "300x300>", thumb: "100x100>" }
validates_attachment :image, :content_type => {:content_type => %w(image/jpeg image/jpg image/png application/pdf application/msword application/vnd.openxmlformats-officedocument.wordprocessingml.document)}
end
How I show the picture at the moment:
<%= image_tag #pictures[0].image.url(), class: "img-responsive" if #pictures.length > 0 %>
Thanks in advance.
Just add a special treatment for PDF. Something like:
<% if #picture.image_content_type=="application/pdf" %>
<%= link_to (image_tag "pdflogo.png"), #picture.image.url() %>
<% else %>
<%= image_tag #picture.image.url(), class: "img-responsive" %>
<% end %>
assuming that the image is held in variable #picture and pdflogo.png contains the pdf logo. (The use of #pictures[0] and if #pictures.length > 0 directly in the code for showing one image is not very DRY; this should be separated from the code for showing one picture.)
I want to understand how to download file using Paperclip. I upload file to local storage.
It's Model:
class AFile < ActiveRecord::Base
has_attached_file :attach,
:url => "public/attach/:basename.:extension",
:path => ":rails_root/public/attach/:basename.:extension"
validates_attachment_content_type :attach, content_type: "text/plain"
end
It's View show.html.erb :
<p>
<strong>AFile:</strong>
<%= #afile.name_file %>
</p>
<%= link_to 'Download', #afile.attach.url(:original, false) %> |
<%= link_to 'Edit', edit_afile_path(#afile) %> |
<%= link_to 'Back', afiles_path %>
I did like this:
File download using Paperclip
but it did not help.
But when i click on the Download, then an error:
No route matches [GET] "/public/attach/text.txt"
How to solve this problem? Why file cannot be downloaded by clicking "Download"?
Rails places the /public directory in the servers web root. So a file with the file system path /public/foo.txt will be accessible at http://localhost:3000/foo.txt - not http://localhost:3000/public/foo.txt.
So you need to change url option for the attached file:
class AFile < ActiveRecord::Base
has_attached_file :attach,
:url => "/attach/:basename.:extension",
:path => ":rails_root/public/attach/:basename.:extension"
validates_attachment_content_type :attach, content_type: "text/plain"
end
My solution to download file is something like :
<%= link_to 'Download', #afile.attach.url(:original),
download: #afile.attach.url(:original)%>
snippet from my model:
attr_accessible :package1_file_name, :package2_file_name
has_attached_file :package1
has_attached_file :package2
from my _form (simplified version):
<%= form_for(#submission, :html => { :multipart => true, :id => "fileupload" }) do |f| %>
<%= f.file_field :package1%>
<%= f.file_field :package2%>
<% end %>
The problem is that paperclip will insert two separate entries for each file in the database.
However, I want it to insert into only one entries since I have two separate fields in my table: package1_file_name, package2_file_name.
Is there a way to achieve this?
Thank you!
For Christian Varga:
Maybe I shouldn't simplified the code in my original question, but my view actually look like this after using the jQuery file upload plugin:
<%= f.fields_for :uploads do |upload| %>
<div class="row fileupload-buttonbar">
<!-- The first upload field -->
<span class="btn btn-success fileinput-button">
<%= upload.file_field :package1 %>
</span>
<!-- The second upload field -->
<span class="btn btn-success fileinput-button">
<%= upload.file_field :package2 %>
</span>
</div>
<% end %>
Where upload is a child model of the current model
I am not sure whether the multipart makes the two file fields act like separate attachment, but I am trying to merge those two attachments into one.
Ok, so I've done a bit of research and I'm still unable to replicate your issue. I built a test application with that code, and it only inserts a single entry into the database.
Create Project (terminal)
rails new paperclip-test
cd paperclip-test
echo "gem 'paperclip'" >> Gemfile
bundle
rails generate scaffold submission
rails generate paperclip submission package1 package2
rake db:migrate
Update Model (submission.rb)
attr_accessible :package1, :package2
has_attached_file :package1, :styles => { :medium => "300x300>", :thumb => "100x100>" }
has_attached_file :package2, :styles => { :medium => "300x300>", :thumb => "100x100>" }
Update controller (submissions_controller.rb)
def create
# #submission = Submission.new(params[:submission])
#submission = Submission.create(params[:submission])
end
Update form (_form.html.erb)
<%= f.file_field :package1 %>
<%= f.file_field :package2 %>
Update view (show.html.erb)
<%= image_tag #submission.package1.url(:medium) %>
<%= image_tag #submission.package2.url(:medium) %>
Run app & create new submission
Go back to console:
sqlite3 db/development.sqlite3
select * from submissions;
Result:
1|2013-02-21 21:16:38.898602|2013-02-21 21:16:38.898602|image_1.jpg|image/jpeg|54231|2013-02-21 21:16:38.419947|image_2.jpg|image/jpeg|61766|2013-02-21 21:16:38.658720
Paperclip instructions from https://github.com/thoughtbot/paperclip#quick-start
Does anyone know how to upload a multi-page pdf with Paperclip and convert each page into a Jpeg?
So far, every time I upload a PDF, it only allows me to see the first page of the PDF as a JPEG. But I would like to be able to upload and convert every page from the PDF into a JPEG.
Is there any gem or plug-in that can help me upload a 10-pg PDF and Convert/Store in the Database as 10 JPEG files?
I have looked at docsplit-images gem, but I am not sure if that is the solution best solution or how it works.
Post.rb
class Post < ActiveRecord::Base
belongs_to :Blogs
attr_accessible :content, :title, :pdf
has_attached_file :pdf,
:url => "/assets/products/:id/:style/:basename.:extension",
:path => ":rails_root/public/assets/products/:id/:style/:basename.:extension"
validates_attachment_content_type :pdf,
:content_type => [ 'application/pdf' ],
:message => "only pdf files are allowed"
end
_form.html.erb
<%= form_for ([#post]), :html => { :multipart => true } do |f| %>
<%= f.file_field :pdf %>
<% end %>
show.html.erb
<%= image_tag #post.pdf.url(:original) %>
Using an image tag for this makes no sense. Change your image_tag to a regular link and you'll be able to download and view all the pages.
<p>
<%= link_to 'My PDF', #post.pdf.url %>
</p>
Trying to run paperclip on windows on a mongrel server. Images won't upload, generates console log of
Invalid Parameter - /Users
[paperclip] An error was received while processing: #<Paperclip::PaperclipError: There was an error processing the thumbnail for stream20110831-1316-1naludd>
The model code is
has_attached_file :image, :styles => { :large => "1280x800", :thumb => "128x80" }
Form code is
<%= simple_form_for #item, :html => { :multipart => true } do |f| %>
<%= f.error_messages %>
<%= f.input :name %>
<%= f.input :price %>
<%= f.input :description %>
<%= f.input :image %>
<%= f.association :section, :include_blank => false %>
<%= f.submit %>
<% end %>
Imagemagick is installed, path is placed in the development.rb file. Tried multiple image formats (.jpg, .png, .gif) from multiple locations. Migration has been done. Any ideas?
EDIT: SOLVED. Turns out I made a super stupid mistake and put forward slashes in the path to imageMagick, forgot for a second windows uses backslashes.
It looks like the ImageMagick convert utility can't process your uploaded image temp file.
Can you dump the environment for your mongrel? Especially wherever the Rails temp directory is.
I bet the fully qualified path to the 'tmp' directory in your Rails project isn't processable on windows when passed to ImageMagick, causing the thumbnail generation to fail.
Sadly, you may need to move your Rails project to a path that works.
Have the same problem, it aborts with the same error message when it tries to convert the image.
It's because convert is also a windows command (Better described here).
Changing paperclip's command_path in config/environments/development.rb directly to imagemagick solves the problem:
Paperclip.options[:command_path] = 'C:\Program Files\ImageMagick-6.9.0-Q16'