我使用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