Ruby 在Carrierwave中手动上载和保存文件
我有一个现有文件的目录,作为遗留迁移的一部分,我需要将其迁移到我的rails应用程序中。基本上,我需要手动上传这些文件,并在数据库中为它们保存一条新记录。我还没有找到合适的方法来做这件事。目前,我在rake任务中有以下内容:Ruby 在Carrierwave中手动上载和保存文件,ruby,file-upload,carrierwave,ruby-on-rails-4,rmagick,Ruby,File Upload,Carrierwave,Ruby On Rails 4,Rmagick,我有一个现有文件的目录,作为遗留迁移的一部分,我需要将其迁移到我的rails应用程序中。基本上,我需要手动上传这些文件,并在数据库中为它们保存一条新记录。我还没有找到合适的方法来做这件事。目前,我在rake任务中有以下内容: @attachments.each do |attachment| begin new_attachment = Attachment.new @attachment_file_path = "/home/username/Attachments/"
@attachments.each do |attachment|
begin
new_attachment = Attachment.new
@attachment_file_path = "/home/username/Attachments/" + attachment.Filename
file = File.open(@attachment_file_path)
new_attachment[:file] = new_attachment.file.store!(file)
# Map old record fields to new
new_attachment.attributes = {
:project_id => attachment.ProjectID,
:name => attachment.Description,
:user_id => attachment.UserId,
:created_at => attachment.CreatedDate,
:updated_at => attachment.LastModifiedDate
}
new_attachment.save!
puts "Attachment added successfully "
rescue => error
puts "Error migrating Attachment: #{error}"
end
end
附件.rb
class Attachment < ActiveRecord::Base
mount_uploader :file, FileUploader
end
在本例中,该文件是一个“.doc”文件
打开本地文件并通过Carrierwave手动上载的正确方法是什么
感谢您的帮助。试试这个
@attachments.each do |attachment|
begin
options = {
:project_id => attachment.ProjectID,
:name => attachment.Description,
:user_id => attachment.UserId,
:created_at => attachment.CreatedDate,
:updated_at => attachment.LastModifiedDate,
:file => File.new(File.join("/home/username/Attachments/",attachment.Filename))
}
new_attachment = Attachment.new(options)
new_attachment.save!
puts "Attachment added successfully "
rescue => error
puts "Error migrating Attachment: #{error}"
end
end
也许这对你有好处,因为carrierwave会在内部调用商店代码>为您准备
问题
不确定您想在这里做什么,因为您已经定义了一个图像?
方法,该方法没有在条件中指定,也就是说,您希望内容类型
仅出现在图像
文件中
如果no
可能只有进程调用可以工作
过程:设置内容类型
如果是
,那么您可能必须这样做
process :set_content_type , :if => :image?
def image?(new_file)
%w(jpg jpeg gif).include?(new_file.extension)
end
希望这有帮助
根据评论进行编辑
试试这个,只是使用了相同的逻辑条件
version :thumb ,:if => image? do
// your code
end
试试这个
@attachments.each do |attachment|
begin
options = {
:project_id => attachment.ProjectID,
:name => attachment.Description,
:user_id => attachment.UserId,
:created_at => attachment.CreatedDate,
:updated_at => attachment.LastModifiedDate,
:file => File.new(File.join("/home/username/Attachments/",attachment.Filename))
}
new_attachment = Attachment.new(options)
new_attachment.save!
puts "Attachment added successfully "
rescue => error
puts "Error migrating Attachment: #{error}"
end
end
也许这对你有好处,因为carrierwave会在内部调用商店代码>为您准备
问题
不确定您想在这里做什么,因为您已经定义了一个图像?
方法,该方法没有在条件中指定,也就是说,您希望内容类型
仅出现在图像
文件中
如果no
可能只有进程调用可以工作
过程:设置内容类型
如果是
,那么您可能必须这样做
process :set_content_type , :if => :image?
def image?(new_file)
%w(jpg jpeg gif).include?(new_file.extension)
end
希望这有帮助
根据评论进行编辑
试试这个,只是使用了相同的逻辑条件
version :thumb ,:if => image? do
// your code
end
谢谢你的回复。事实上,我看了一下,我不再需要“image”方法了,所以我删除了它。即使在实现了您的更改之后,我仍然会遇到上述错误。我还尝试删除“进程集内容类型”,但仍然出现相同的错误。如何防止rmagick处理“文档”和文本文件?感谢您的回复。事实上,我看了一下,我不再需要“image”方法了,所以我删除了它。即使在实现了您的更改之后,我仍然会遇到上述错误。我还尝试删除“进程集内容类型”,但仍然出现相同的错误。如何防止rmagick处理“文档”和文本文件?