Mar*_*rty 5 ruby ruby-on-rails asset-pipeline ruby-on-rails-4
Organization
并Image
有一个1-to-1 relationship
.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')
Run Code Online (Sandbox Code Playgroud)
两者都生成错误:
NoMethodError: undefined method `create!' for nil:NilClass
Run Code Online (Sandbox Code Playgroud)
我检查了debugger
并且可以确认它不是 @organization
零.
如何使这项工作并将文件的路径添加到Image
模型中?
更新:我尝试了以下内容:
@image = Image.create!(organization_id: @organization.id,
filename: 'app/assets/other/image.jpg')
Run Code Online (Sandbox Code Playgroud)
我也尝试过:
image = @organization.build_image(filename: 'app/assets/other/image.jpg')
image.save
Run Code Online (Sandbox Code Playgroud)
播种后,两次尝试都会产生错误:
CarrierWave::FormNotMultipart: You tried to assign a String or a Pathname to an uploader, for security reasons, this is not allowed.
Run Code Online (Sandbox Code Playgroud)
由于您的错误清楚地表明了问题所在.事实证明,@organization
还没有任何图像.所以试试吧
file = File.open(File.join(Rails.root,'app/assets/other/image.jpg'))
image = @organization.build_image(filename: file)
image.save
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2759 次 |
最近记录: |