我正在使用Rails 3和Paperclip使用多态关联将上传的文件附加到多个对象类型。我创建了一个Asset模型和一个继承的Image模型(将添加其他模型,如后面的视频和文档),如下所示:
# 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的敬意,以便可以正确地进行多态关联。以下是我目前的上传代码:
%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?。有关这种情况是否适用的任何想法?是否有一种简单的方法可以从unit.id
方法确定/new
并将其传递给Uploadify创建的资产?
答案 0 :(得分:4)
我们在将表单加载到draft
状态(使用状态机)时立即保存模型,解决了一个非常类似的问题。像这样,当您尝试附加要上传的文件时,该模型可用,一旦您提交表格的其余部分,您基本上只是更新模型,将其状态更改为例如published
。更新控制器等是一项小工作,但它确实可以解决问题。