我正在升级到Rails 5.2.0并尝试使用ActiveStorage来保存数据Feed中的图像。在config.active_storage.service = :local
我有has_many_attached :images
,模型有storage.yml
,:local
有默认def listing_params
params.require(:listing).permit(:beds, :baths, :price, :parking, :description, :active, :prop_type, :latitude, :longitude, :address, :city, :city_id, :state, :state_id, :zip, :lo_name, :la_fname, :la_lname, :la_phone, :la_email, :mlsid, :cp, :st_number, :st_suffix, :baths_full, :baths_half, :status, :modtime, :st_name, :featured, :school_id, :picture_count, :parking_type, images: [])
end
设置,控制器操作有
listing.images.attach(pics)
我的代码挂断了这一行
ActiveSupport::MessageVerifier::InvalidSignature: ActiveSupport::MessageVerifier::InvalidSignature
出现此错误:def perform
require 'aws-sdk-s3'
require 'rets'
client = Rets::Client.new({
login_url: LOGIN_URL,
username: LOGIN,
password: PASSWORD,
version: VERSION
})
begin
client.login
rescue => e
puts 'Error: ' + e.message
exit!
end
p "Logged in"
Listing.find_each do |listing|
mlsid = listing.mlsid
photos = client.objects '*', {
resource: 'Property',
object_type: 'Photo',
resource_id: mlsid
}
p "about to attempt to attach #{photos.count} images to listing#{listing.id}"
pics = []
photos.each do |data|
pics << data.body
end
listing.images.attach(pics)
puts "#{photos.count} images attached to listing##{listing.id}"
end
end
我的代码:
tomb$ rake update:listing_images --trace
** Invoke update:listing_images (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute update:listing_images
Cookie#domain returns dot-less domain name now. Use Cookie#dot_domain if you need "." at the beginning.
"Logged in"
"about to attempt to attach 16 images to listing2873"
rake aborted!
ActiveSupport::MessageVerifier::InvalidSignature: ActiveSupport::MessageVerifier::InvalidSignature
/Users/tomb/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/activesupport-5.2.0/lib/active_support/message_verifier.rb:176:in `verify'
/Users/tomb/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/activestorage-5.2.0/app/models/active_storage/blob.rb:43:in `find_signed'
/Users/tomb/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/activestorage-5.2.0/lib/active_storage/attached.rb:30:in `create_blob_from'
/Users/tomb/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/activestorage-5.2.0/lib/active_storage/attached/many.rb:26:in `block in attach'
/Users/tomb/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/activestorage-5.2.0/lib/active_storage/attached/many.rb:22:in `collect'
/Users/tomb/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/activestorage-5.2.0/lib/active_storage/attached/many.rb:22:in `attach'
/Users/tomb/Projects/schoolsparrow/app/jobs/listing_image_job.rb:43:in `block in perform'
/Users/tomb/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/activerecord-5.2.0/lib/active_record/relation/batches.rb:70:in `block (2 levels) in find_each'
/Users/tomb/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/activerecord-5.2.0/lib/active_record/relation/batches.rb:70:in `each'
/Users/tomb/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/activerecord-5.2.0/lib/active_record/relation/batches.rb:70:in `block in find_each'
/Users/tomb/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/activerecord-5.2.0/lib/active_record/relation/batches.rb:136:in `block in find_in_batches'
/Users/tomb/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/activerecord-5.2.0/lib/active_record/relation/batches.rb:238:in `block in in_batches'
/Users/tomb/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/activerecord-5.2.0/lib/active_record/relation/batches.rb:222:in `loop'
/Users/tomb/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/activerecord-5.2.0/lib/active_record/relation/batches.rb:222:in `in_batches'
/Users/tomb/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/activerecord-5.2.0/lib/active_record/relation/batches.rb:135:in `find_in_batches'
/Users/tomb/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/activerecord-5.2.0/lib/active_record/relation/batches.rb:69:in `find_each'
/Users/tomb/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/activerecord-5.2.0/lib/active_record/querying.rb:11:in `find_each'
/Users/tomb/Projects/schoolsparrow/app/jobs/listing_image_job.rb:31:in `perform'
/Users/tomb/Projects/schoolsparrow/lib/tasks/update.rake:21:in `block (2 levels) in <top (required)>'
/Users/tomb/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/rake-12.3.1/lib/rake/task.rb:271:in `block in execute'
/Users/tomb/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/rake-12.3.1/lib/rake/task.rb:271:in `each'
/Users/tomb/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/rake-12.3.1/lib/rake/task.rb:271:in `execute'
/Users/tomb/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/rake-12.3.1/lib/rake/task.rb:213:in `block in invoke_with_call_chain'
/Users/tomb/.rbenv/versions/2.3.7/lib/ruby/2.3.0/monitor.rb:214:in `mon_synchronize'
/Users/tomb/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/rake-12.3.1/lib/rake/task.rb:193:in `invoke_with_call_chain'
/Users/tomb/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/rake-12.3.1/lib/rake/task.rb:182:in `invoke'
/Users/tomb/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/rake-12.3.1/lib/rake/application.rb:160:in `invoke_task'
/Users/tomb/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/rake-12.3.1/lib/rake/application.rb:116:in `block (2 levels) in top_level'
/Users/tomb/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/rake-12.3.1/lib/rake/application.rb:116:in `each'
/Users/tomb/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/rake-12.3.1/lib/rake/application.rb:116:in `block in top_level'
/Users/tomb/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/rake-12.3.1/lib/rake/application.rb:125:in `run_with_threads'
/Users/tomb/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/rake-12.3.1/lib/rake/application.rb:110:in `top_level'
/Users/tomb/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/rake-12.3.1/lib/rake/application.rb:83:in `block in run'
/Users/tomb/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/rake-12.3.1/lib/rake/application.rb:186:in `standard_exception_handling'
/Users/tomb/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/rake-12.3.1/lib/rake/application.rb:80:in `run'
/Users/tomb/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/rake-12.3.1/exe/rake:27:in `<top (required)>'
/Users/tomb/.rbenv/versions/2.3.7/bin/rake:22:in `load'
/Users/tomb/.rbenv/versions/2.3.7/bin/rake:22:in `<main>'
Tasks: TOP => update:listing_images
使用--trace:
运行后出现完整错误1 | very good
2 | good
3 | sufficient
答案 0 :(得分:3)
您将Rets::Parser::Multipart::Part
个对象数组传递给ActiveStorage::Attached::Many#attach
。它不知道如何处理它们。
对于每张照片,请使用IO和文件名调用#attach
:
photos.each_with_index do |photo, index|
listing.images.attach(io: StringIO.new(photo.body), filename: "photo-#{index + 1}.png")
end