gpt4 book ai didi

ruby-on-rails - 如何播种图像的路径?

转载 作者:数据小太阳 更新时间:2023-10-29 07:12:00 26 4
gpt4 key购买 nike

OrganizationImage 具有一对一的关系Image 有一个名为 filename 的列,它存储文件的路径。我在 Assets 管道中包含这样一个文件: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 是 nil。
我怎样才能让它工作并将文件的路径添加到 Image 模型?


更新:我尝试了以下操作:

@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

关于ruby-on-rails - 如何播种图像的路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32522992/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com