Rails 3,回形针-自定义插值 [英] Rails 3, Paperclip - Custom Interpolations

查看:117
本文介绍了Rails 3,回形针-自定义插值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在进行自定义插值时遇到了一些麻烦,遍历了我在网络上可以找到的每个示例,但是无论我做什么,都没有成功. 目前,我有这个:

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:

型号

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

initializers/paperclip.rb

initializers/paperclip.rb

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

img_name在上载图像时以表格形式填充. 我上载的错误是:

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

无效的参数-(C:/Users/.../stream20110410-384-stl2lk20110230-213-1fm2bab,C:/.../photo_upload/public/images/:img_name-original.jpg)

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

推荐答案

如果直接在模型中,则似乎可以使用:

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

这篇关于Rails 3,回形针-自定义插值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆