Paperclip.interpolates 隨機不返回值 (Paperclip.interpolates randomly not returning a value)


問題描述

Paperclip.interpolates 隨機不返回值 (Paperclip.interpolates randomly not returning a value)

I have the following paperclip setup in my model:

  #Paperclip for photo
  has_attached_file :photo,
                    :styles => {
                      :large => '1024x758>',
                      :medium => "200x150#",
                      :small => "50x50>"
                    },
                    :default_style => :original,
                    :default_url => '/images/:attachment/default_:style.png',
                    :path => ":instance_id/:attachment/:id/:version/:style/:basename.:extension",
                    :storage => :s3,
                    :s3_credentials => File.join(Rails.root, 'config', 's3.yml'),
                    :s3_protocol => 'https'

for instance_id I have the following:

Paperclip.interpolates :instance_id do |attachment, style|

  def instance_id(attachment, style)
    attachment.instance.instance_id
  end

end

What's happening is when I first fire up the server, I'm noticing 404s with my images. I had thought that was Amazon S3, but then I looked into the URLs, and noticed that sometimes instance_id is not being returned by Paperclip.interpolates.

Any idea why? Have you experienced anything like this?

Thanks


參考解法

方法 1:

If I understand correctly what you're trying to achieve, then the following should work:

Paperclip.interpolates :instance_id do |attachment, style|
  attachment.instance.instance_id
end

FYI: your interpolates proc defines a method, but doesn't actually do something... 

Hope this helps,

Peter

(by AnApprenticepvandenberk)

參考文件

  1. Paperclip.interpolates randomly not returning a value (CC BY-SA 3.0/4.0)

#paperclip #ruby-on-rails






相關問題

帶有 S3 的回形針 - 未定義的方法 (Paperclip with S3 - undefined method)

添加文件上傳進度條的最簡單方法(回形針)Apache/Paperclip/Rails (Easiest way to add file upload progress bar (paperclip) Apache/Paperclip/Rails)

EngineYard:分離代碼和資產 (EngineYard : Segregating the code & assets)

在 rails/paperclip 中處理一系列圖像 (handling an array of images in rails/paperclip)

rails - không thể tạo bản ghi với tệp đã tải lên (rails - can't build a record with uploaded file)

在保存到 Rails 模型之前打開臨時文件 (Open the temporary file before it is saved in Rails model)

Rails - 回形針 - 多張照片上傳不保存 (Rails - paperclip - Multiple photo upload not saving)

回形針不為一種型號存儲替代尺寸,但為另一種型號存儲 (Paperclip is not storing alternative sizes for one model, but does for another)

無法使用 S3 圖像的 image.to_file 使用 Paperclip gem 複製圖像 (Can't copy images with Paperclip gem using image.to_file for S3 images)

Paperclip.interpolates 隨機不返回值 (Paperclip.interpolates randomly not returning a value)

未初始化的常量 AWS::S3::NoSuchBucket (uninitialized constant AWS::S3::NoSuchBucket)

來自基類的 Rails STI 條件子類化 (Rails STI conditional sub-classing from base class)







留言討論