Rails 7 中的文件附件固定装置未正确连接
我逐字遵循此处的指南,但是当我运行系统测试时,我尝试上传的文件似乎没有附加到我的装置中。我相信我已经正确设置了所有内容,因为我确实看到上传的文件位于临时目录(tmp/storage_fixtures)中,因此无论出于何种原因,它都没有将自身附加到模型上。这是我的文件:
towns.yml
toronto:
id: 1
name: Toronto
blobs.yml
toronto_map_image_blob: <%= ActiveStorage::FixtureSet.blob filename: "toronto_map.png", service_name: "test_fixtures" %>
attachments.yml
toronto_map_image:
name: map_image
record: toronto (Town)
blob: toronto_map_image_blob
config/storage.yml
test_fixtures:
service: Disk
root: <%= Rails.root.join("tmp/storage_fixtures") %>
镇.rb
class Town < ApplicationRecord
has_one_attached :map_image
end
I am following the guide here verbatim but when I run my system tests, the file I am trying to upload does not seem to attach to my fixture. I believe I have everything set up correctly as I do see the uploaded file sitting in the temporary directory (tmp/storage_fixtures) so for whatever reason it's not attaching itself to the model. Here are my files:
towns.yml
toronto:
id: 1
name: Toronto
blobs.yml
toronto_map_image_blob: <%= ActiveStorage::FixtureSet.blob filename: "toronto_map.png", service_name: "test_fixtures" %>
attachments.yml
toronto_map_image:
name: map_image
record: toronto (Town)
blob: toronto_map_image_blob
config/storage.yml
test_fixtures:
service: Disk
root: <%= Rails.root.join("tmp/storage_fixtures") %>
town.rb
class Town < ApplicationRecord
has_one_attached :map_image
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实证明我遇到的问题是在城镇固定装置中定义一个 id。一旦我删除它并允许自动生成 id,它就起作用了。这解释了为什么附件固定装置无法将自身正确分配给城镇模型(尽管我不确定为什么使用自定义 ID 在这种情况下不起作用,如果有人有任何见解,我将不胜感激)。
It turned out the issue I was having was defining an id within the towns fixture. Once I removed that and allowed the id to autogenerate, it worked. This explains why the attachments fixture was unable to properly assign itself to the town model (though I'm not sure why using a custom id doesn't work in this case and would appreciate insight if anyone has any).