I'm learning rails, and I've spent the last two days trying to figure out how to upload both video and images through the same model using paperclip. I feel like I've exhausted my resources in search of an answer, but haven't found anything that seems to help me apply the correct styles to the correct file type.
Here is my Image model (note: I started with an Image model and an avatar attribute. I am expanding it's use, so please don't get confused on how it's named)
class Image < ActiveRecord::Base
belongs_to :imageable, polymorphic: true
has_attached_file :avatar,
if: :is_image_type?, styles: {:large => "750x750>", :medium => "300x300#", :thumb => "100x100#" }, :default_url => "no_image.png",
if: :is_video_type?, :styles => {
:medium => { :geometry => "640x480", :format => 'flv' },
:thumb => { :geometry => "100x100#", :format => 'jpg', :time => 10 }
}, :processors => [:transcoder]
validates_attachment_content_type :avatar,
:content_type => ['image/png', 'image/jpeg', 'image/jpg', 'image/gif', "video/mp4", "video/m4v", "video/mpeg"]
def is_image_type?
content_type = ['image/png', 'image/jpeg', 'image/jpg', 'image/gif']
# /\Aimage\/.*\Z/
end
def is_video_type?
content_type = ["video/mp4", "video/m4v", "video/mpeg"]
end
end
Essentially I'm trying to figure out how to get the styles to work for their appropriate file types. If it is a video, I want it to be styled as a video, if it is an image, as an image.
The content validation is working fine, and I will alter it to include all image formats and all video formats once it is working.
Goal To summarize, my goal is to be able to have one file upload field where a user can upload multiple files and multiple file types including images, videos, audio, pdf, and other file types.
I will then use the "content_type" field to pull the file types I want to display on different areas of the site.
If this approach with one model seems inadequate, please indicate a better approach.
I'm using the 'paperclip-av-transcoder' gem to process video.
Again, I am learning rails, so if something is unclear I am happy to clarify and revise.
Thank you very much.