Rails 3, Paperclip - Custom Interpolations

不羁的心 提交于 2019-12-04 18:41:19

问题


I've been having some troubles making custom Interpolation, gone through every example I could find on web, but no matter what I did, had no success. At the moment I have this:

Model

has_attached_file :photo,
  :path => ":rails_root/public/images/:img_name-:style.:extension",
  :styles => {
    :original => '100x100',
    :thumb => '30x30'
}

initializers/paperclip.rb

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

img_name is field populated in form on upload with the image. The error I get on upload is:

Invalid argument - (C:/Users/.../stream20110410-384-stl2lk20110230-213-1fm2bab, C:/.../photo_upload/public/images/:img_name-original.jpg)


回答1:


Seems to work if it's directly in the model:

class Model < ActiveRecord::Base

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

  has_attached_file :photo,
    :path => ":rails_root/public/images/:img_name-:style.:extension",
    :styles => {
      :original => '100x100',
      :thumb => '30x30'
    }

end


来源:https://stackoverflow.com/questions/5613456/rails-3-paperclip-custom-interpolations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!