我使用Rails 3和回形针工作,上传的文件附加到使用多态关联的几个对象类型之前。我已经创建了一个资产模型和继承的图像模型(将增加其他的如视频和文档版本)如下:的Rails 3回形针Uploadify:保存上传附件对象保存对象
# 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
我然后有另一个对象类型,单元,其中我附上多个图像,以作为如下:
# 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不起作用。任何帮助将非常感激。先谢谢你。
UPDATE:Rails3, S3, Paperclip Attachment as it's own model?:
做更多的研究,我碰到这个评论跑到一个类似的问题后。有没有想过在这种情况下是否可行?有没有简单的方法确定/new
方法中的unit.id
并将其传递给Uploadify创建的资产?
我喜欢这种方法,正在实施。但是,将模型保存到“草稿”状态时,您对所需属性做了什么?例如,我有几个必须满足的'validates_presence_of'语句。有没有简单的方法来跳过验证? – theandym 2011-03-22 19:11:33
我们只通过验证'on update'解决了这个问题:'validates_presence_of:foobar,:on =>:update' – polarblau 2011-03-22 19:45:31