问题描述
目前我尝试用回形针替换attachment_fu文件上传插件.
At the moment I try to replace attachment_fu fileuploading plugin with paperclip.
但上传时出现一些错误:
But there are some errors while uploading:
我的控制器如下所示:
def create
add_breadcrumb 'breadcrumb.upload_file', {:action => :new}
puts "BLUBBBBBBBBBBBBBBBBBBBBBBBBBBB"
puts params[:upload]
@upload = Upload.create(params[:upload])
@upload.lecturer_id = current_user.id
if @upload.save
flash[:notice] = I18n.t("flash.saved")
redirect_to :action => :index
else
render :action => :new
end
end
我的模型是这样的:
puts has_attached_file :image, :default_url => :file_system
validates_attachment_content_type :image, :content_type => [:image, 'audio/mpeg', 'application/mp3', 'application/octet-stream']
上传时出现此错误:
ActiveRecord::UnknownAttributeError in UploadsController#create
unknown attribute: uploaded_data
app/controllers/uploads_controller.rb:24:in `create'
更新:
{"name"=>"TEST", "uploaded_data"=>#<ActionDispatch::Http::UploadedFile:0x00000005518a38 @original_filename="2014-09-26 18.14.22.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"upload[uploaded_data]\"; filename=\"2014-09-26 18.14.22.jpg\"\r\nContent-Type: image/jpeg\r\n", @tempfile=#<Tempfile:/tmp/RackMultipart20150415-12671-190wpi5>>}
更新:那不是我想要的.数据库中的迁移产生了一些新列,如何返回到以前的版本?有移民命令吗?
Update: thats not that what I wanted. The migration generetad some new columns in database, how can I return to the previus version? is there a demigrate coammand?
推荐答案
要从 Paperclip 恢复迁移,您可以使用:
To revert the migration from Paperclip you can use either:
rake db:migrate:down VERSION=_current_migration_number
或
rake db:rollback
直到您之前的状态.然后你可以像这样创建一个新的迁移:
Until your previous state. Then you can create a new migration like this:
class ChangeUploadToPaperclip < ActiveRecord::Migration
change do
rename_column :uploads, :content_type, :upload_content_type
rename_column :uploads, :filename, :upload_filename
rename_column :uploads, :size, :upload_file_size
add_column :uploads, :upload_updated_at, :datetime
# Warning: the following will remove your old data, irreversible.
remove_column :uploads, :width
remove_column :uploads, :height
remove_column :uploads, :thumbnail
end
end
但是您的旧存储和 Paperclips 存储文件的方式之间可能存在潜在错误.
But there could be potential errors between your old storage and Paperclips way of storing files.
在这个答案中,我假设您的模型 has_attached_file :upload
.
In this answer I assumed that your model has_attached_file :upload
.
如果附加文件有其他名称,请相应地更新每个 upload
和 uploads
.
If you have another name for the attached file, update every upload
and uploads
accordingly.
编辑迁移文件需要有一定的名字:
EditThe migration file needs to have a certain name:
20150417125122_change_upload_to_paperclip.rb
(日期字符串是自动生成的,我从下面的评论中复制了这个.)
(the date-string is auto generated, I copied this from comment below.)
这是因为只有当两者之间有下划线时,Rails 才会查找 ChangeUploadToPaperclip
.
This is because Rails will look for ChangeUploadToPaperclip
only if there is underscore between.
如果你有 changeuploadtopaperclip.rb
,它会寻找 Changeuploadtopaperclip
.
If you have changeuploadtopaperclip.rb
it would look for Changeuploadtopaperclip
.
运行特殊迁移
rake db:migrate:up VERSION=20150417125122
这篇关于在 rails 应用程序中使用回形针时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!