【问题标题】:Rails 3 Paperclip Uploadify : Save Uploaded Object Attachments Before Saving ObjectRails 3 Paperclip Uploadify:在保存对象之前保存上传的对象附件
【发布时间】:2011-03-22 14:48:21
【问题描述】:

我正在使用 Rails 3 和 Paperclip 使用多态关联将上传的文件附加到多个对象类型。我创建了一个 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

然后我有另一个对象类型 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'

使用这些设置,我可以一次上传一张图片,每次显示表单。

当我尝试集成 Uploadify 以添加多文件上传/预览时,问题就开始了。我已经满足了所有 Uploadify 依赖项,但是为了保存与 Unit 模型关联的图像,我需要以某种方式包含对 unit_id 的尊重,以便可以正确建立多态关联。以下是我当前的 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}",
      }
    });   
  });

因此,虽然我可以使用 Paperclip 轻松上传,但 Uploadify 将无法正常工作。任何帮助将非常感激。提前谢谢你。


更新:

在做了更多研究后,我发现了一条类似问题的评论:Rails3, S3, Paperclip Attachment as it's own model?。关于在这种情况下是否可行的任何想法?有没有一种简单的方法可以从 /new 方法中确定 unit.id 并将其传递给 Uploadify 创建的资产?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 paperclip uploadify


    【解决方案1】:

    我们曾经通过在draft 状态下加载表单时立即保存模型(使用状态机)解决了一个非常类似的问题。像这样,当您尝试附加要上传的文件时,该模型可用,并且一旦您提交了表单的其余部分,您基本上只是在更新模型,将其状态更改为例如published。更新控制器等需要一点工作,但它确实成功了。

    【讨论】:

    • 我喜欢这种方法并正在实施。但是,在将模型保存到draft 状态时,您对所需属性做了什么?例如,我有几个必须满足的validates_presence_of 语句。有没有跳过验证的简单方法?
    • 我们通过仅验证 on update: validates_presence_of :foobar, :on =&gt; :update 解决了这个问题
    猜你喜欢
    • 2011-02-17
    • 1970-01-01
    • 2018-01-29
    • 1970-01-01
    • 2010-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-16
    相关资源
    最近更新 更多