ruby-on-rails – 如何播种图像的路径?
发布时间:2020-12-17 03:49:50 所属栏目:百科 来源:网络整理
导读:组织和图像具有一对一的关系. Image有一个名为filename的列,它存储文件的路径.我在资产管道中包含了这样一个文件:app / assets / other / image.jpg.播种时如何包含此文件的路径? 我试过我的种子文件: @organization = ...@organization.image.create!(fi
组织和图像具有一对一的关系.
Image有一个名为filename的列,它存储文件的路径.我在资产管道中包含了这样一个文件:app / assets / other / image.jpg.播种时如何包含此文件的路径?
我试过我的种子文件: @organization = ... @organization.image.create!(filename: File.open('app/assets/other/image.jpg')) # I also tried: # @organization.image.create!(filename: 'app/assets/other/image.jpg') 两者都生成错误: NoMethodError: undefined method `create!' for nil:NilClass 我检查了调试器,并确认它不是@organization是零. 更新:我尝试了以下内容: @image = Image.create!(organization_id: @organization.id,filename: 'app/assets/other/image.jpg') 我也尝试过: image = @organization.build_image(filename: 'app/assets/other/image.jpg') image.save 播种后,两次尝试都会产生错误: CarrierWave::FormNotMultipart: You tried to assign a String or a Pathname to an uploader,for security reasons,this is not allowed. 解决方法
由于您的错误清楚地表明了问题所在.事实证明@organization还没有任何图像.所以试试吧
file = File.open(File.join(Rails.root,'app/assets/other/image.jpg')) image = @organization.build_image(filename: file) image.save (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |