하나의 단일 클립 필드를 사용하여 다른 파일 유형을 처리하는 것이 얼마나 가능한가요? 예를 들어 다음과 같은 종이 클립 방법을 사용하는 파일 모델이 있습니다.
has_attached_file :file
이 파일은 사진, 오디오, 비디오 또는 문서 일 수 있습니다.
사진이라면 has_attached_file :file
이 방법으로 사진을 처리 할 수 있도록 어떻게 만들 수 있습니까?
has_attached_file :file, styles: {thumb: "72x72#"}
그런 다음 다른 문서 유형 인 경우 스타일없이 정상적으로 작동하므로 다른 파일 유형에 대한 필드를 만들 필요가 없습니다.
조건부 스타일을 처리하는 방법은 a lambda
를 사용하여 처리중인 콘텐츠 유형을 결정하는 것입니다. 이전 버전의 Rails / Paperclip에서이 작업을 수행했습니다.
#app/models/attachment.rb
Class Attachment < ActiveRecord::Base
has_attached_file :file,
styles: lambda { |a| a.instance.is_image? ? {:small => "x200>", :medium => "x300>", :large => "x400>"} : {}}
validates_attachment_content_type :file, :content_type => [/\Aimage\/.*\Z/, /\Avideo\/.*\Z/]
private
def is_image?
attachment.instance.attachment_content_type =~ %r(image)
end
end
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다