Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails Paperclip, Multiple of Different Type (PDF, Image, Doc...)

There are a lot of good resources out there that show how to create a Rails application with multiple image uploads. Additionally, there are a lot of good resources showing how to use paperclip to upload different file types (PDF, image, .Doc).

I'd like to create an application where a User has an image asset for their profile picture, and also has the ability to upload PDFs or .Doc files to their account to be tied to the User. Has anyone had experience with this to confirm it's possible through the Paperclip gem? Any tutorials or resources to point me in the right direction would be appreciated.

like image 375
Andy Sharkey Avatar asked Dec 27 '22 09:12

Andy Sharkey


2 Answers

I was also having exactly same problem like you. No need to do extra coding for this, just add 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 explanations.

like image 178
RAJ Avatar answered Jan 12 '23 21:01

RAJ


Just in case anyone is looking at this recently, we have just tried the accepted answer, and the content types did not work properly. We got it working 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 -- typically "___content_type", instead of just "content_type"

like image 30
Richard Peck Avatar answered Jan 12 '23 19:01

Richard Peck