Andy Sharkey
Andy Sharkey

Reputation: 65

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.

Upvotes: 4

Views: 2586

Answers (3)

Richard Peck
Richard Peck

Reputation: 76784

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"

Upvotes: 3

RAJ
RAJ

Reputation: 9747

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.

Upvotes: 8

Robbie Done
Robbie Done

Reputation: 1157

You sure can upload images and other filled with paperclip, may be worth going to www.railscasts.com as there are quite a few related posts to your question.

Upvotes: -1

Related Questions