我正在更新使用ruby_filemagic上传图片的项目。在iFrame中有一个AJAX表单可以工作,但是当我将代码传输到另一个表单时,显然是相同的结构(例如也是multipart,指的是同一个对象,在这种情况下是@listing),它会抛出一个错误。
为什么会出现这种情况?
在AJAX环境中是否存在某些行为?失败的表单标记如下所示:
<form action="/listings/add_description" class="edit_listing" enctype="multipart/form-data" id="edit_listing_26" method="post" onsubmit="return validateForm(this,'listing_description');">
<div style="margin:0;padding:0;display:inline"><input name="_method" type="hidden" value="put" /><input name="authenticity_token" type="hidden" value="2VyJP37Ff1ADOoB6ZDEtS4FrdxwGNgnqqstB+hoNWu0=" />
虽然有效的那个看起来像:
<form target="imageUpload" method="post" id="new_listing" enctype="multipart/form-data" class="new_listing" action="/listings">
<div style="margin: 0pt; padding: 0pt; display: inline;"><input type="hidden" value="2VyJP37Ff1ADOoB6ZDEtS4FrdxwGNgnqqstB+hoNWu0=" name="authenticity_token"></div>
成功上传(从第二张表格看起来像:
Processing ListingsController#create (for 12.13.14.15 at 2011-02-09 12:40:03) [POST]
Parameters: {"commit"=>"Create", "action"=>"create", "authenticity_token"=>"2VyJP37Ff1ADOoB6ZDEtS4FrdxwGNgnqqstB+hoNWu0=", "listing"=>{"end_date(3i)"=>"9", "discount_percent"=>"", "name"=>"test", "thumbnail"=>#<File:/var/folders/Oo/Oooj578kHdy3igRanTaILU+++TM/-Tmp-/RackMultipart20110209-5842-ipfdzh-0>, "start_date_text"=>"now", "category_id"=>"22", "regular_price"=>"100", "description"=>"test", "end_date(1i)"=>"2011", "end_date(2i)"=>"2", "sale_price"=>"30"}, "controller"=>"listings"}
虽然失败的一个(来自非AJAX,第一个表单看起来如下。正在创建/ tmp中的文件,但/ var中的文件不是:
Processing ListingsController#add_description (for 12.13.14.15 at 2011-02-09 12:41:39) [PUT]
Parameters: {"commit"=>"Next -- Set Dates", "action"=>"add_description", "_method"=>"put", "authenticity_token"=>"2VyJP37Ff1ADOoB6ZDEtS4FrdxwGNgnqqstB+hoNWu0=", "listing"=>{"thumbnail"=>#<File:/var/folders/Oo/Oooj578kHdy3igRanTaILU+++TM/-Tmp-/RackMultipart20110209-5842-1k0wmd3-0>, "description"=>"Description"}, "controller"=>"listings"}
City Load (0.4ms) SELECT * FROM "cities" WHERE ("cities"."id" = 18996)
found new listing
Errno::ENOENT (No such file or directory - /tmp/2c33a5f0-1684-012e-ab73-00254bb57590):
/opt/local/lib/ruby/1.8/fileutils.rb:1299:in `unlink'
/opt/local/lib/ruby/1.8/fileutils.rb:1299:in `remove_file'
/opt/local/lib/ruby/1.8/fileutils.rb:1304:in `platform_support'
/opt/local/lib/ruby/1.8/fileutils.rb:1298:in `remove_file'
/opt/local/lib/ruby/1.8/fileutils.rb:772:in `remove_file'
/opt/local/lib/ruby/1.8/fileutils.rb:550:in `rm'
/opt/local/lib/ruby/1.8/fileutils.rb:549:in `each'
/opt/local/lib/ruby/1.8/fileutils.rb:549:in `rm'
app/controllers/listings_controller.rb:47:in `check_listing_exists'
更新:根据要求,这是控制器操作:首先,操作一个,创建:
def create
@listing = Listing.new(params[:listing])
@listing.merchant = Merchant.find(current_merchant)
responds_to_parent do
if @listing.save
render :update do |page|
page.replace_html 'formView', :inline => "<div>Listing '#{@listing.name}' was successfully created.</div>"
page['formView'].setStyle('overflow' => 'auto')
page.delay(2) do
page.visual_effect(:toggle_appear, 'formView', :duration => 0.2)
end
end
else
render :update do |page|
page.replace_html 'formView', :partial => 'new'
end
end
end
end
现在,失败的那个:
def add_description
unless @listing.save || session["skip_validation"]
session["skip_validation"] = true
redirect_to :back
return
end
@listing.current_step_index= session["current_step_index"] = 2
@previous_stages = ["pre_listing","listing_comment"]
@listing_partial = "complete_listing"
render :partial => "listings", :layout => "application"
end
我应该提一下,当表单中没有文件上传html时,行为“add_description”正在运行,但是没有对图像thumnail的验证,并且表单中的html直接从工作中复制形式...