Rails 7 中的文件附件固定装置未正确连接

发布于 2025-01-11 18:05:35 字数 977 浏览 2 评论 0原文

我逐字遵循此处的指南,但是当我运行系统测试时,我尝试上传的文件似乎没有附加到我的装置中。我相信我已经正确设置了所有内容,因为我确实看到上传的文件位于临时目录(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我乃一代侩神 2025-01-18 18:05:35

事实证明我遇到的问题是在城镇固定装置中定义一个 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文