开发者

return statement

开发者 https://www.devze.com 2023-01-25 13:06 出处:网络
after_photo_post_process:post_process_photo def post_process_photo img = EXIFR::JPEG.new(photo.queued_for_write[:original].path)# error on this line

after_photo_post_process :post_process_photo

 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

I am using a ruby gem called EXIFR that extracts the EXIF data from JPEG files. The EXIF data开发者_如何学Python is simply technical data about the picture. So my code works fine when I upload a JPEG, however any other image type causes it to break.

EXIFR::MalformedJPEG in ImagesController#create

no start of image marker found

I assumed that the return statement would allow this to work if nothing gets assigned to the img variable, but that looks like it is not the case.


You could rescue the error and return something else.

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

精彩评论

暂无评论...
验证码 换一张
取 消