简体   繁体   English

Rails 3 Paperclip Uploadify:在保存对象之前保存上传的对象附件

[英]Rails 3 Paperclip Uploadify : Save Uploaded Object Attachments Before Saving Object

I am working with Rails 3 and Paperclip to attach uploaded files to several object types using a polymorphic association. 我正在与Rails 3和Paperclip一起使用多态关联将上传的文件附加到几种对象类型。 I have created an Asset model and an inherited Image model (will be adding others, like Video and Documents later) as follows: 我创建了一个Asset模型和一个继承的Image模型(稍后将添加其他模型,如Video和Documents):

# app/models/asset.rb
class Asset < ActiveRecord::Base
  # Nothing here yet
end

# app/models/image.rb
class Image < Asset
  belongs_to :assetable, :polymorphic => true
  has_attached_file :file, {
    :styles => {
      :small => { :geometry => '23x23#', :format => 'png' },
      :medium => { :geometry => '100x100#', :format => 'png' } } 
  }.merge(PAPERCLIP_STORAGE_OPTIONS).merge(PAPERCLIP_STORAGE_OPTIONS_ASSET_IMAGE) # Variables sent in environments to direct uploads to filesystem storage in development.rb and S3 in production.rb
  validates_attachment_presence :file
  validates_attachment_size :file, :less_than => 5.megabytes
end

I then have another object type, Unit, which I am attaching multiple images to as follows: 然后,我有另一个对象类型,即Unit,我将多个图像附加如下:

# app/models/unit.rb
class Unit < ActiveRecord::Base
  # ...

  has_many :images, :as => :assetable, :dependent => :destroy
  accepts_nested_attributes_for :images

end

# app/controllers/units_controller.rb
class UnitsController < ApplicationController
  # ...

  def new
    @unit = current_user.units.new
    # ...
    @unit.images.build
  end

  def create
    @unit = current_user.units.new(params[:unit])
    # ...
    respond_to do |format|
      if @unit.save
        format.html { redirect_to(@unit, :notice => 'Unit creation successful!') }
      else
        format.html { render :action => "new" }
      end
    end
  end

  def show
    @unit = current_user.units.find(params[:id])
    @unit_images = @unit.images
    # ...
  end

  def edit
    @unit = current_user.units.find(params[:id])
    # ...
    @unit.images.build
  end

  def update
    @unit = current_user.units.find(params[:id], :readonly => false)

    respond_to do |format|
      if @unit.update_attributes(params[:unit])
        format.html { redirect_to(@unit, :notice => 'Unit was successfully updated.') }
      else
        format.html { render :action => "edit" }
      end
    end
  end

  def destroy
    @unit = current_user.units.find(params[:id])
    @unit.destroy

    respond_to do |format|
      format.html { redirect_to(units_url) }
    end
  end

end

# app/views/units/_form.html.haml
.field # Display already uploaded images
  = f.fields_for :images do |assets|
    - unless assets.object.new_record?
      = link_to(image_tag(assets.object.file.url(:medium)), assets.object.file.url(:original))
.field # Display field to add new image
  = f.fields_for :images do |assets|
    - if assets.object.new_record?
      = assets.label :images, "Image File"
      = assets.file_field :file, :class => 'uploadify'

Using these settings I am able to upload images one at at time, per display of the form. 使用这些设置,每次显示表单时,我就可以一次上传一张图像。

The issues start when I try to integrate Uploadify to add multi file uploading/previewing. 当我尝试集成Uploadify以添加多文件上传/预览时,问题就开始了。 I have satisfied all the Uploadify dependancies, but in order to save the images associated with the Unit model I need to somehow include a reverence to the unit_id so that the polymorphic association can be made properly. 我已经满足了所有的Uploadify相关性,但是为了保存与单元模型关联的图像,我需要以某种方式包括对unit_id的崇敬,以便可以正确地进行多态关联。 Below is my current Uploadify code: 以下是我当前的Uploadify代码:

%script
  $(document).ready(function() {
    $('.uploadify').uploadify({
      uploader        : '/uploadify/uploadify.swf',
      cancelImg       : '/uploadify/cancel.png',
      auto            : true,
      multi           : true,
      script          : '#{units_path}',
      scriptData      : {
        "#{key = Rails.application.config.session_options[:key]}" : "#{cookies[key]}",
        "#{request_forgery_protection_token}" : "#{form_authenticity_token}",
      }
    });   
  });

So while I can easily upload with Paperclip along, Uploadify will not work. 因此,尽管我可以轻松地使用Paperclip进行上传,但是Uploadify无法正常工作。 Any help would be much appreciated. 任何帮助将非常感激。 Thank you in advance. 先感谢您。


UPDATE: 更新:

After doing more research I ran across this comment to a similar issue: Rails3, S3, Paperclip Attachment as it's own model? 在进行了更多研究之后,我遇到了一个类似的问题: Rails3,S3,回形针附件是它自己的模型吗? . Any thoughts on whether or not that would work in this situation? 有什么想法在这种情况下是否可行? Is there an easy way of determining the unit.id from the /new method and passing it to the Uploadify-created Asset? 是否有一种简单的方法unit.id/new方法确定unit.id并将其传递给Uploadify创建的资产?

We had solved a very similar problem once by saving the model right away when loading the form in a draft state (using state machine). 我们曾经通过在以draft状态(使用状态机)加载表单时立即保存模型来解决一个非常相似的问题。 Like this the model is available when you're trying to attach the files you're uploading and once you're submitting the rest of the form, you're basically just updating the model which changes it's state to eg published . 像这样,当您尝试附加要上传的文件并且提交表单的其余部分时,就可以使用该模型,基本上就是在更新该模型,从而将其状态更改为例如published It's a little work to update the controllers etc., but it did the trick. 更新控制器等工作有些繁琐,但确实成功了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM