ruby-on-rails – CarrierWave:检测图像是否已上传
发布时间:2020-12-17 02:21:32 所属栏目:百科 来源:网络整理
导读:模型使用远程URL作为图像播种,这意味着它不是在Rails中创建的db条目.然后第一次从Rails中的数据库中取出我想要检测到图像尚未上传,为图像URL分配remote_seed_url并保存!触发CarrierWave上传.显然,我只希望这样做一次,但下面的代码有时会不止一次上传相同的
模型使用远程URL作为图像播种,这意味着它不是在Rails中创建的db条目.然后第一次从Rails中的数据库中取出我想要检测到图像尚未上传,为图像URL分配remote_seed_url并保存!触发CarrierWave上传.显然,我只希望这样做一次,但下面的代码有时会不止一次上传相同的图像.
class Item include Mongoid::Document include Sunspot::Mongoid2 field :image,type: String field :remote_seed_url,type: String #URL for image mount_uploader :image,ImageUploader end 然后在控制器中 def show @ item = Item.find(params[:id]) # if CarrierWave has already uploded then do nothing if !@item.image? @item.image = @item.remote_seed_url # next save will trigger the upload? @item.save! end respond_to do ... end @ item.image的值通常是“new”或“old”和@ item.image?当我看到它已经上传了图像时,有时会返回false.这会导致上述代码多次上传. >控制器代码是否正确才能将图像上传一次? 解决方法
上传器安装在字段上后,当您调用该字段时,您将获得一个上传器对象.如果要检查是否有文件,则应在该对象上调用“file”:
[1] pry(main)> item.image.class => ImageUploader [2] pry(main)> item.image.file.class => CarrierWave::SanitizedFile [3] pry(main)> item.image.file.nil? => false (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |