2010-11-13 92 views
2

after_photo_post_process:post_process_photoreturn语句

def post_process_photo 
    img = EXIFR::JPEG.new(photo.queued_for_write[:original].path) # error on this line 
    return unless img 

    self.width = img.width 
    self.height = img.height 
    self.model = img.model 
end 

我使用所谓的一个EXIFR红宝石的宝石,提取从JPEG文件的EXIF数据。 EXIF数据只是关于图片的技术数据。因此,当我上传JPEG时,我的代码工作正常,但是任何其他图像类型都会导致它中断。

:: EXIFR在MalformedJPEG#ImagesController创建

没有图像标记开始发现

我假定return语句将使如果没有被分配到IMG变量这项工作,但是这看起来像情况并非如此。

回答

1

您可以拯救错误并返回其他内容。

def post_process_photo 
    begin 
    img = EXIFR::JPEG.new(photo.queued_for_write[:original].path) # error on this line 
    self.width = img.width 
    self.height = img.height 
    self.model = img.model 
    rescue EXIFR::MalformedJPEG 
    return nil 
    end 
end 
+0

太棒了,这很好用。 – chief 2010-11-13 22:08:24

+0

我很高兴它为你工作。 – rwilliams 2010-11-13 22:13:27