我有一个用于创建虚拟数据的工厂,工厂名称是migration_session。
因此,我通过调用
创建了该工厂的一个实例@loan_migration = create(:migration_session, :loan, cooperative: @cooperative, management: @manager)
这是我的migration_session工厂代码
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
这是我的migration_session模型
class Migration::Session < ApplicationRecord
self.table_name = "migration_sessions"
has_one_attached :excel_file
end
因为我调用trait loan,它将通过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, '/'字符可能不允许在path上,所以不要传递路径字符串,让rails为你填充它。
使用Rails.root.join
方法,像这样:Rails.root.join('public', 'payment_migration.xlsx')