2011-11-11 130 views
3

當使用回形針,如果我嘗試在創建函數進入像這樣的URL(後圖片保存Rails的回形針圖片保存

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?26621' @ 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?26744 

當我嘗試在軌道控制檯中做photo.url時,它給出了很好的格式,但在我的控制器中添加了一些怪異的垃圾'?26621'。我該如何解決? (正則表達式看起來很容易解決這個問題,但是否有比這更好的辦法)

回答

2

paperclip github project

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 - 根據您正在使用的回形針的版本選擇哪一個。

+0

很好,這工作 – Kevin