Сохранение изображения скрепки Rails - PullRequest
3 голосов
/ 11 ноября 2011

При использовании скрепки, если я пытаюсь ввести URL-адрес следующим образом (в функции создания после сохранения изображения

image = Magick::ImageList.new('public' + @picture.photo.url)

я получаю ошибку

Magick::ImageMagickError in PicturesController#create

no decode delegate for this image format `public/system/photos/115/original/Kitchener-2011103100531.jpg?1321026621' @ error/constitute.c/ReadImage/532

если я попытаюсь ввести

@picture.latitude = EXIFR::JPEG.new('public' + @picture.photo.url).gps_lat

чтобы иметь дело с данными EXIF, которые я получаю

Errno::ENOENT in PicturesController#create

No such file or directory - public/system/photos/116/original/Kitchener-20111031-00531.jpg?1321026744

Когда я пытаюсь сделать photo.url в консоли rails, он дает хороший формат, но в моем контроллере добавляет странный мусор «? 1321026621» в конце. Как это исправить? (регулярное выражение выглядит легко исправить, но есть ли лучший подход, чем этот)

1 Ответ

2 голосов
/ 11 ноября 2011

Из проекта paperclip github :

https://github.com/thoughtbot/paperclip/blob/master/lib/paperclip/attachment.rb

# Returns the public URL of the attachment with a given style. This does
# not necessarily need to point to a file that your Web server can access
# and can instead point to an action in your app, for example for fine grained
# security; this has a serious performance tradeoff.
#
# Options:
#
# +timestamp+ - Add a timestamp to the end of the URL. Default: true.
# +escape+    - Perform URI escaping to the URL. Default: true.
#
# Global controls (set on has_attached_file):
#
# +interpolator+  - The object that fills in a URL pattern's variables.
# +default_url+   - The image to show when the attachment has no image.
# +url+           - The URL for a saved image.
# +url_generator+ - The object that generates a URL. Default: Paperclip::UrlGenerator.
#
# As mentioned just above, the object that generates this URL can be passed
# in, for finer control. This object must respond to two methods:
#
# +#new(Paperclip::Attachment, Paperclip::Options)+
# +#for(style_name, options_hash)+
def url(style_name = default_style, options = {})
  default_options = {:timestamp => @options.use_timestamp, :escape => true}

  if options == true || options == false # Backwards compatibility.
    @url_generator.for(style_name, default_options.merge(:timestamp => options))
  else
    @url_generator.for(style_name, default_options.merge(options))
  end
end

По умолчанию скрепка добавляет параметр отметки времени, передавая :timestamp => false в urlметод вашего вложения:

image = Magick::ImageList.new('public' + @picture.photo.url(:original, :timestamp => false))

# => "public/system/photos/115/original/Kitchener-2011103100531.jpg"

Редактировать: На самом деле, похоже, они изменили имя опции, так как это не работало для меня с Paperclip 2.4.0.После проверки моего собственного источника, опция :use_timestamp, но на github это :timestamp - выберите, какой из них зависит от используемой версии Paperclip.

...