回形针:样式取决于模型(has_many 多态图像) [英] Paperclip :style depending on model (has_many polymorphic images)

查看:46
本文介绍了回形针:样式取决于模型(has_many 多态图像)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将模型设置为使用多态图像模型.这工作正常,但是我想知道是否可以更改每个模型的 :styles 设置.找到了一些使用 STI 的示例(模型 < 图像)但是这对我来说不是一个选项,因为我使用的是 has_many 关系.

I have set up my models to use a polymorphic Image model. This is working fine, however I am wondering if it is possible to change the :styles setting for each model. Found some examples using STI (Model < Image) However this is not an option for me, because I am using a has_many relation.

艺术

has_many :images, :as => :imageable

图片

belongs_to :imageable, :polymorphic => true
has_attached_file :file, :styles => { :thumb => "150x150>", :normal => "492x600>"}
                         #Change this setting depending on model

更新

我尝试在 Proc 方法中启动调试器.仅填充与附件相关的字段:

UPDATE

I tried starting up a debugger inside the Proc method. Only fields related to the attached file is populated:

run'irb(Image):006:0> a.instance => #<Image id: nil, created_at: nil, updated_at: nil, imageable_id: nil, imageable_type: nil, file_file_name: "IMG_9834.JPG", file_content_type: "image/jpeg", file_file_size: 151326, file_updated_at: "2010-10-30 08:40:23">

这是来自 ImageController#create 的对象

This is the object from ImageController#create

ImageController#create
@image => #<Image id: nil, created_at: nil, updated_at: nil, imageable_id: 83, imageable_type: "Art", file_file_name: "IMG_9834.JPG", file_content_type: "image/jpeg", file_file_size: 151326, file_updated_at: "2010-10-30 08:32:49">

我使用的是回形针 (2.3.5) 和 Rails 3.0.1.无论我做什么,a.instance 对象都是只包含与附件相关的字段的图像.有什么想法吗?

I am using paperclip (2.3.5) and Rails 3.0.1. No matter what I do the a.instance object is the image with only the fields related to the attachment populated. Any ideas?

在 Paperclip 论坛上阅读了大量内容后,我认为在保存实例之前无法访问该实例.你只能看到回形针的东西,就是这样.

After reading a lot on the Paperclip forum I don't believe it's possible to access the instance before it has been saved. You can onlye see the Paperclip stuff and that's it.

我通过使用前置过滤器预先保存来自图像控制器的图像来解决这个问题 - 没有附件

I got around this problem by presaving the image from the Image controller with a before filter - without the attachment

  before_filter :presave_image, :only => :create

  ...

  private

  def presave_image
    if @image.id.nil? # Save if new record / Arts controller sets @image
      @image = Image.new(:imageable_type => params[:image][:imageable_type], :imageable_id => params[:image][:imageable_id])
      @image.save(:validate => false)
      @image.file = params[:file] # Set to params[:image][:file] if you edit an image.
    end
  end

推荐答案

真的参加这次聚会很晚了,但我想澄清一些关于为碰巧遇到的其他人访问模型数据的问题这个线程.我刚刚在使用 Paperclip 根据模型中的数据应用水印时遇到了这个问题,并在经过大量调查后得到了解决.

I am really late to the party here, but I wanted to clarify something about accessing model data for anyone else that happens on to this thread. I just faced this issue while using Paperclip to apply watermarks based on data from the model, and got it working after a lot of investigation.

你说:

在 Paperclip 论坛上阅读了大量内容后,我认为在保存实例之前无法访问该实例.你只能看到回形针的东西,就是这样.

After reading a lot on the Paperclip forum I don't believe it's possible to access the instance before it has been saved. You can only see the Paperclip stuff and that's it.

实际上,您可以看到模型数据如果在分配附件之前已在对象中设置了它

In fact, you can see the model data if it's been set in the object before your attachment is assigned!

在分配模型中的附件时会调用回形针处理器等.如果您依赖批量赋值(或不依赖,实际上),一旦附件被赋值,回形针就会起作用.

Your paperclip processors and whatnot are invoked when the attachment in your model is assigned. If you rely on mass assignment (or not, actually), as soon as the attachment is assigned a value, paperclip does its thing.

这是我解决问题的方法:

Here's how I solved the problem:

在带有附件(照片)的模型中,我设置了除附件 attr_accessible 之外的所有属性,从而防止在批量分配期间分配附件.

In my model with the attachment (Photo), I made all the attributes EXCEPT the attachment attr_accessible, thereby keeping the attachment from being assigned during mass assignment.

class Photo < ActiveRecord::Base
  attr_accessible :attribution, :latitude, :longitude, :activity_id, :seq_no, :approved, :caption

  has_attached_file :picture, ...
  ...
end

在我的控制器的 create 方法(例如)中,我从 params 中提取了 picture,然后创建了对象.(可能没有必要从 params删除图片,因为 attr_accessible 语句应该防止 picture 被分配,但它不会伤害).然后我分配了picture属性,照片对象的所有其他属性都已经设置之后.

In my controller's create method (for example) I pulled out the picture from the params, and then created the object. (It's probably not necessary to remove the picture from params, since the attr_accessible statement should prevent picture from being assgined, but it doesn't hurt). Then I assign the picture attribute, after all the other attributes of the photo object have been setup.

def create
  picture = params[:photo].delete(:picture)
  @photo = Photo.new(params[:photo])
  @photo.picture = picture

  @photo.save
  ...
end

在我的例子中,图片的一种样式要求应用水印,即 attribution 属性中保存的文本字符串.在我进行这些代码更改之前,从未应用属性字符串,并且在水印代码中,attachment.instance.attribution 始终是 nil.此处总结的更改使整个模型在回形针处理器中可用.关键是分配你的附件属性last.

In my case, one of the styles for picture calls for applying a watermark, which is the text string held in the attribution attribute. Before I made these code changes, the attribution string was never applied, and in the watermark code, attachment.instance.attribution was always nil. The changes summarized here made the entire model available inside the paperclip processors. The key is to assign your attachment attribute last.

希望这对某人有所帮助.

Hope this helps someone.

这篇关于回形针:样式取决于模型(has_many 多态图像)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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