如何解决ActiveStorage::IntegrityError Rspec 附加文件
我有一个创建虚拟数据的工厂,工厂名称是migration_session。
所以我通过这样调用来创建该工厂的一个实例
@loan_migration = create(:migration_session,:loan,cooperative: @cooperative,management: @manager)
这是我的迁移会话工厂代码
FactoryBot.define do
factory :migration_session,class: Migration::Session do
cooperative
management
trait :loan do
excel_file { Rack::Test::UploadedFile.new("#{Rails.root}/public/payment_migration.xlsx",'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') }
end
这是我的迁移会话模型
class Migration::Session < ApplicationRecord
self.table_name = "migration_sessions"
has_one_attached :excel_file
end
因为我调用了特质贷款,它会通过excel文件payment_migration.xlsx附加excel_file属性。但是尝试保存实例后会出现这样的错误
ActiveStorage::IntegrityError:
ActiveStorage::IntegrityError
# /usr/local/bundle/gems/activestorage-6.0.3.4/lib/active_storage/service/disk_service.rb:154:in `ensure_integrity_of'
# /usr/local/bundle/gems/activestorage-6.0.3.4/lib/active_storage/service/disk_service.rb:21:in `block in upload'
# /usr/local/bundle/gems/activestorage-6.0.3.4/lib/active_storage/service.rb:126:in `instrument'
# /usr/local/bundle/gems/activestorage-6.0.3.4/lib/active_storage/service/disk_service.rb:19:in `upload'
# /usr/local/bundle/gems/activestorage-6.0.3.4/app/models/active_storage/blob.rb:196:in `upload_without_unfurling'
# /usr/local/bundle/gems/activestorage-6.0.3.4/lib/active_storage/attached/changes/create_one.rb:25:in `upload'
# /usr/local/bundle/gems/activestorage-6.0.3.4/lib/active_storage/attached/model.rb:56:in `block in has_one_attached'
# ./spec/requests/bulk_uploads_spec.rb:36:in `block (2 levels) in <top (required)>'
# /usr/local/bundle/gems/webmock-3.9.5/lib/webmock/rspec.rb:37:in `block (2 levels) in <top (required)>'
如何解决这个问题?我的文件很好,我可以打开和编辑它。
解决方法
我认为你的问题出在文件路径上
excel_file { Rack::Test::UploadedFile.new("#{Rails.root}/public/payment_migration.xlsx",'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') }
根据您的 SO,路径上可能不允许使用 '/' 字符,因此不要传递路径字符串,而是让 Rails 为您填充它。
使用 Rails.root.join
方法,如下所示:
Rails.root.join('public','payment_migration.xlsx')
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。