图像文件应在上传时调整大小。
上传器文件中的代码' process resize_to_fill: [800, 350]'
适用于生产,但不适用于我的开发环境。 (我使用Vagrant)
我的文件config/initializers/carrierwave.rb
已使用以下代码修改:
CarrierWave.configure do |config|
if Rails.env.development?
config.cache_dir = '/home/vagrant/uploads_tmp/tmp/uploads'
config.root = '/home/vagrant/uploads_tmp/tmp'
end
end
因为没有此代码,我会收到以下错误(由于使用Windows):
Errno::ETXTBSY in Instructor::CoursesController#create
并且在开发中根本无法上传。
我的控制器操作如下:
def create
@course = current_user.courses.create(course_params)
if @course.valid?
redirect_to instructor_course_path(@course)
else
render :new, status: :unprocessable_entity
end
end
在实例变量赋值行上抛出错误。
自添加初始化程序代码以来,某些图像文件以原始大小保存,而其他图像文件则保存为大部分空白区域(顶部显示的图像条纹较小)。我似乎无法确定任何一个类中的图像有什么特别之处!有趣的是,当应用程序部署到heroku时,似乎不会发生这种情况。
有人可以解释一下:
1)导致原始错误的原因是什么,以及初始化代码为什么要修复它。
2)图片上传出了什么问题?
自从发布我的问题后,我发现文件确实在初始化程序中没有代码修复的情况下上传;在这种情况下,问题是访问该文件。我在原始问题中省略了错误消息的第二行,这实际上必须是问题的关键:Text file busy @ unlink_internal - /vagrant/src/flixter/public/uploads/tmp/1499336954-11657-0001-9160/ecriture.jpg
这是完整的堆栈跟踪(没有初始化代码修复的原始错误):
Errno::ETXTBSY (Text file busy @ unlink_internal - /vagrant/src/flixter/public/uploads/tmp/1499420789-13682-0001-6708/ben-white-148430.jpg): app/controllers/instructor/courses_controller.rb:10:in 'create' Rendering /home/vagrant/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/actionpack-5.0.3/lib/ action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout Rendering /home/vagrant/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/actionpack-5.0.3/lib/ action_dispatch/middleware/templates/rescues/_source.html.erb
Rendered /home/vagrant/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/actionpack-5.0.3/lib/action_dispatch/middleware/templates/rescues/_source.html.erb (5.3ms)
Rendering /home/vagrant/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/actionpack-5.0.3/lib/ action_dispatch/middleware/templates/rescues/_trace.html.erb Rendered /home/vagrant/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/actionpack-5.0.3/lib/a ction_dispatch/middleware/templates/rescues/_trace.html.erb (2.3ms)
Rendering /home/vagrant/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/actionpack-5.0.3/lib/ action_dispatch/middleware/templates/rescues/_request_and_response.html.erb Rendered /home/vagrant/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/actionpack-5.0.3/lib/a ction_dispatch/middleware/templates/rescues/_request_and_response.html.erb (2.0ms) Rendered /home/vagrant/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/actionpack-5.0.3/lib/a ction_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (26.1ms)
我仍然不知道为什么初始化代码部分修复了错误!