Rails Paperclip, several different types (PDF, Image, Doc ...)

There are many good resources that show how to create a Rails application with multiple image downloads. In addition, there are many good resources showing how to use paperclip to load different types of files (PDF, image, .Doc).

I would like to create an application in which the user has an image object for their profile image, and also have the ability to upload PDF files or .Doc files to their user account, tied to the user. Has anyone had experience with this to confirm this is possible through Pearl Paper Clips? Any tutorials or resources to point me in the right direction will be appreciated.

+4
source share
3 answers

I also had the same problem as you. No need to do extra coding for this, just add the following

has_attached_file :attachment, :styles => lambda{ |a| ["image/jpeg", "image/png", "image/jpg", "image/gif"].include?( a.content_type ) ? { :thumb=> "100x100#", :small => "150x150>", :medium => "300x300>", :large => "500x500>" }: {} } 

Let me know if you need further explanation.

+8
source

Just in case, when someone looks at this recently, we just tried the accepted answer, and the content types did not work properly. We did it like this:

 has_attached_file :attachment, styles: lambda { |a| a.instance.attachment_content_type =~ %r(image) ? {:small => "x200>", :medium => "x300>", :large => "x400>"} : {} } 

The difference is in the "column name" for the object - usually "___content_type", not just "content_type"

+3
source

You can upload images and stuff filled with a paper clip, it might be worth a visit to www.railscasts.com, since there are many related posts to your question.

-1
source

Source: https://habr.com/ru/post/1385240/


All Articles