我有两个使用paperclip的不同应用程序。在不保存附件或调用回形针的应用程序上,我上传应用程序时会收到此日志
started POST "/users/1/uploads" for 127.0.0.1 at 2011-04-23 13:38:11 +0100
Processing by UploadsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"t2dRIH4FgOPnLRhpDK0x7iIfjB9Xj5rqkJRPCWZvJ14=", "upload"=> {"document"=>#<ActionDispatch::Http::UploadedFile:0x2beeb08 @original_filename="Essay questions have various requirements.doc", @content_type="application/msword", @headers="Content-Disposition: form-data; name=\"upload[document]\"; filename=\"Essay questions have various requirements.doc\"\r\nContent-Type: application/msword\r\n", @tempfile=#<File:C:/DOCUME~1/Ed/LOCALS~1/Temp/RackMultipart20110423-3980-ycq74p>>}, "commit"=>"Upload", "user_id"=>"1"}
[1m[35mUser Load (0.0ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
[1m[36mUpload Load (0.0ms)[0m [1mSELECT "uploads".* FROM "uploads" WHERE ("uploads".uploadable_id = 1 AND "uploads".uploadable_type = 'User')[0m
Rendered uploads/_uploadify.html.erb (15.6ms)
Rendered uploads/_form.html.erb (31.2ms)
Rendered uploads/new.html.erb within layouts/application (46.9ms)
Completed 200 OK in 500ms (Views: 234.4ms | ActiveRecord: 0.0ms)
在回形针工作正常的应用程序中,我得到了这个日志:
Started POST "/uploads" for 127.0.0.1 at Mon Apr 25 11:35:50 +0100 2011
Processing by UploadsController#create as JSON
Parameters: {"_http_accept"=>"application/javascript", "Filename"=>"angels.txt", "folder"=>"/users/", "authenticity_token"=>"NVJj3ODIGuoc97wGvjWkez1YoN+SUDVtNJ+k80XdYXM=", "Upload"=>"Submit Query", "user_id"=>"1", "_uploadify_session"=>"BAh7ByIQX2NzcmZfdG9rZW4iMU5WSmozT0RJR3VvYzk3d0d2aldrZXoxWW9OK1NVRFZ0TkorazgwWGRZWE09Ig9zZXNzaW9uX2lkIiU0ZDEyNzZkNzczNzk1MDdiMmQ4NWZmYTY5MDY4YTU0MQ==--7eb8c0ca249e2566998a0e68322a89d731fdb4ad", "Filedata"=>#<ActionDispatch::Http::UploadedFile:0x4958490 @content_type="application/octet-stream", @original_filename="angels.txt", @tempfile=#<File:C:/DOCUME~1/Ed/LOCALS~1/Temp/RackMultipart20110425-4884-vyvdo8-0>, @headers="Content-Disposition: form-data; name=\"Filedata\"; filename=\"angels.txt\"\r\nContent-Type: application/octet-stream\r\n">}
[paperclip] identify -format %wx%h "C:/DOCUME~1/Ed/LOCALS~1 /Temp/stream20110425-4884-15he32x-0.txt[0]" 2>NUL
[paperclip] convert "C:/DOCUME~1/Ed/LOCALS~1/Temp/stream20110425-4884-15he32x-0.txt[0]" -resize "300x300>" "C:/DOCUME~1/Ed/LOCALS~1/Temp/stream20110425-4884-15he32x-020110425-4884-r65fe6-0" 2>NUL
[paperclip] identify -format %wx%h "C:/DOCUME~1/Ed/LOCALS~1/Temp/stream20110425-4884-15he32x-0.txt[0]" 2>NUL
[paperclip] convert "C:/DOCUME~1/Ed/LOCALS~1/Temp/stream20110425-4884-15he32x-0.txt[0]" -resize "100x100>" "C:/DOCUME~1/Ed/LOCALS~1/Temp/stream20110425-4884-15he32x-020110425-4884-1ttxfol-0" 2>NUL
[1m[36mAREL (15.6ms)[0m [1mINSERT INTO "uploads" ("user_id", "created_at", "photo_file_size", "photo_updated_at", "photo_content_type", "photo_file_name", "updated_at") VALUES (1, '2011-04-25 10:36:10.312500', 867, '2011-04-25 10:35:52.109375', 'text/plain', 'angels.txt', '2011-04-25 10:36:10.312500')[0m
[paperclip] Saving attachments.
[paperclip] saving C:/rails_project1/Uploadify-2/public/system/photos/2/medium/angels.txt
[paperclip] saving C:/rails_project1/Uploadify-2/public/system/photos/2/thumb/angels.txt
[paperclip] saving C:/rails_project1/Uploadify-2/public/system/photos/2/original/angels.txt
Completed 200 OK in 19422ms (Views: 62.5ms | ActiveRecord: 15.6ms)
两者之间的唯一区别是,不工作的是多态模型,多态模型的控制器如下:
class UploadsController < ApplicationController
before_filter :find_parent
before_filter :prepare_input_params
#respond_to :html, :json, :js
def index
@uploads = Upload.all
#@uploads = @parent.try(:uploads).try(:all)
@upload = Upload.new
#respond_with([@parent, @uploads])
end
def new
@upload = @parent.uploads.new
end
def create
@upload = @parent.uploads.build(params[:upload])
if @upload.save
flash[:notice] = "sucessfully saved upload"
respond_to do |format|
format.html {redirect_to [@parent, :uploads]}
format.json {render :json => { :result => 'success', :upload => polymorphic_url([@parent,:uploads]) } }
end
else
render :action => 'new'
end
end
def edit
@upload = Upload.find(params[:id])
end
def show
"puts @upload.inspect"
@upload = @parent.uploads.find(params[:id])
@total_uploads = @parent.uploads.find(:all, :conditions => { :user_id => @upload.user.id})
end
def update
@upload = Upload.find(params[:id])
if @upload.update_attributes(params[:upload])
flash[:notice] = "Successfully updated document"
redirect_to @upload
else
render :action => 'edit'
end
end
def destroy
@upload = Upload.find(params[:id])
@upload.destroy
redirect_to([@parent, :upload])
end
private
def prepare_input_params
params[:upload][:document] = params[:Filedata] if params[:Filedata]
end
端
以下是带有partial和uploadify位形式的gist文件:https://gist.github.com/940960。表单有:html =&gt; {:multipart =&gt;真的}。
感谢您的帮助。
修改:
似乎问题来自模型uploads.rb。当我评论下面的部分:一切正常,但我需要能够使用验证并指定路径,因为我想使用S3。任何帮助我如何取消注释代码没有回形针的初始错误而不是再次保存。
class Upload < ActiveRecord::Base
attr_accessible :document
belongs_to :uploadable, :polymorphic => true
has_attached_file :document, :styles => { :small => "150x150>",:thumb => "100x100>" }
=begin
:url => "/uploads/:id/:style/:basename.:extension",
:path => ":rails.root/public/:uploads/:id/:style/:basement.:extension"
validates_attachment_presence :document
validates_attachment_size :document, :less_than => 5.megabytes
validates_attachment_content_type :document, :content_type => ['application/octet-stream','image/jpeg','image/gif', 'image/png', 'image/pdf', 'image/doc',
'video/x-m4v', 'video/quicktime','application/x-shockwave-flash', 'audio/mpeg', 'video/mpeg', 'application/pdf','application/msword']
=end
端
更新:
我认为主要问题似乎是为什么附加新文件,回形针,以某种方式开始从“上传”调用 SELECT“上传”。*'而不是调用 INSERT INTO'上传',即控制器#create action 显示更高似乎是调用SELECT,每当我提供:url和:paperclip的路径选项has_attached_file方法。请参阅以下日志:
Processing by UploadsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Azxzi09R7NU7+jxj+lxHFGfs+qw7D6b7yRKegRjRQMc=", "upload"=> {"document"=>#<ActionDispatch::Http::UploadedFile:0x2994ff0 @original_filename="al night verses.txt", @content_type="text/plain", @headers="Content-Disposition: form-data; name=\"upload[document]\"; filename=\"al night verses.txt\"\r\nContent-Type: text/plain\r\n", @tempfile=#<File:C:/DOCUME~1/Ed/LOCALS~1 /Temp/RackMultipart20110505-3444-1rlpnr>>}, "commit"=>"Upload", "user_id"=>"1"}
[1m[35mUser Load (0.0ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
[paperclip] C:\ImageMagick-6.6.3-Q16/identify -format %wx%h "C:/DOCUME~1/Ed/LOCALS~1 /Temp/stream20110505-3444-1cpnf87.txt[0]"
[paperclip] C:\ImageMagick-6.6.3-Q16/convert "C:/DOCUME~1/Ed/LOCALS~1 /Temp/stream20110505-3444-1cpnf87.txt[0]" -resize "150x150>" "C:/DOCUME~1/Ed/LOCALS~1 /Temp/stream20110505-3444-1cpnf8720110505-3444-1vnp5jx"
[paperclip] C:\ImageMagick-6.6.3-Q16/identify -format %wx%h "C:/DOCUME~1/Ed/LOCALS~1 /Temp/stream20110505-3444-1cpnf87.txt[0]"
[paperclip] C:\ImageMagick-6.6.3-Q16/convert "C:/DOCUME~1/Ed/LOCALS~1 /Temp/stream20110505-3444-1cpnf87.txt[0]" -resize "100x100>" "C:/DOCUME~1/Ed/LOCALS~1 /Temp/stream20110505-3444-1cpnf8720110505-3444-1vyk9i"
[1m[36mUpload Load (0.0ms)[0m [1mSELECT "uploads".* FROM "uploads" WHERE ("uploads".uploadable_id = 1 AND "uploads".uploadable_type = 'User')[0m
Rendered uploads/_uploadify.html.erb (15.6ms)
Rendered uploads/_form.html.erb (31.2ms)
Rendered uploads/new.html.erb within layouts/application (62.5ms)
根据@ CharlieMezak的要求,这里是views / uploads / _form.html.erb :
<%= debug @parent %>
<%= render :partial => "uploads/uploadify" %>
</br>
<%= form_for [parent, upload], :html => { :multipart => true } do |f| %>
<div class="field">
<%= f.label :document %><br />
<%= f.file_field :document %>
</div>
<div class="actions">
<%= f.submit "Upload"%>
</div>
视图/上传/ new.html.erb :
<%= render 'form', :parent => @parent, :upload => @upload %>
视图/用户/ index.html.erb :
<%= render "uploads/form", :parent => user, :upload => user.uploads.new %>
更新:
就像我提到的那样,当我从paperclips的''has_many_attachment:document'中注释掉:styles,:: url和:path 选项时,会调用INSERT语句,尽管它会保存文件而不是显示附件,它最终会在网站上显示几个参数,如真品令牌等,如下所示:
attributes:
id: 1
email: xyz@yahoo.com
encrypted_password: $2a$10$HiksbkRXDtcXiJyUIRj
password_salt: $2a$10$HiksbkRXD
reset_password_token: !!null
remember_token: !!null
remember_created_at: !!null
sign_in_count: 3
current_sign_in_at: '2011-04-25 18:57:27.078125'
last_sign_in_at: '2011-04-25 09:25:31.406250'
current_sign_in_ip: 127.0.0.1
last_sign_in_ip: 127.0.0.1
created_at: '2011-04-09 17:46:15.546875'
updated_at: '2011-04-25 18:57:27.078125'
changed_attributes: {}
previously_changed: {}
attributes_cache: {}
marked_for_destruction: false
destroyed: false
readonly: false
new_record: false
答案 0 :(得分:1)
您是否记得在视图中将:multipart => true
添加到表单标记?
答案 1 :(得分:0)
尝试将:document_file_name添加到attr_accessible调用。
答案 2 :(得分:0)
我注意到的一件事是你的要点中的uploadify第17行有一个拼写错误。你有dat.upload,可能应该是data.upload?
另外,您是否尝试过一次取消注释验证?让我知道发生什么事。与Paperclip相比,我有过相当多的战斗。
答案 3 :(得分:0)
我正在做与你完全相同的事情,但使用aws-s3 gem来帮助我通过回形针上传到S3。这就是我模型中的内容:
has_attached_file :image,
:storage => :s3,
:s3_credentials => "#{Rails.root.to_s}/config/s3.yml",
:bucket => "your_s3_bucket_name",
:path => ":attachment/:id/:style/:filename"
我在我的gemfile中有这个:
gem "paperclip", "~> 2.3"
gem "aws-s3"
在我的小组表中:
t.string "image_file_name"
t.string "image_content_type"
t.integer "image_file_size"
t.datetime "image_updated_at"