我不确定我是否理解您的问题-您是要求远程运行rake任务还是如何导入图像?
在后面的例子中,有一个答案。
首先,您需要一些模型来保存图像和其他数据,比如:
class Picture < ActiveRecord::Base
has_attached_file :image, :styles => {
:thumb => "100x100>",
:big => "500x500>"
}
end
您可以在lib/tasks文件夹中创建简单的rake task(您应该用.rake扩展名命名该文件)
namespace :import do
desc "import all images from SOURCE_DIR folder"
task :images => :environment do
# get all images from given folder
Dir.glob(File.join(ENV["SOURCE_DIR"], "*")) do |file_path|
# create new model for every picture found and save it to db
open(file_path) do |f|
pict = Picture.new(:name => File.basename(file_path),
:image => f)
# a side affect of saving is that paperclip transformation will
# happen
pict.save!
end
# Move processed image somewhere else or just remove it. It is
# necessary as there is a risk of "double import"
#FileUtils.mv(file_path, "....")
#FileUtils.rm(file_path)
end
end
end
然后,您可以从控制台手动调用rake任务,提供源目录参数,该参数将是服务器上的文件夹(它可以是真正的文件夹或远程安装的文件夹)。
rake import:images SOURCE_DIR=~/my_images/to/be/imported
如果你打算自动运行这个,我建议你去更新调度宝石。
更新:为了简单起见,我故意省略了异常处理