2

Rails 5.2 app

I am saving multiple images via a POST from a an XML object sent from a mobile app.

The images look like this when they are received:

"image_group"=>
[
  {"report_image"=>"1565721255925.jpg"}, 
  {"report_image"=>"1565721267236.jpg"}, 
  {"report_image"=>"1565721279636.jpg"}, 
  {"report_image"=>"1565721292231.jpg"}
]

So, these images belong to a report. I am saving the report with the images in a controller that handles the POST.

I am saving the images to report with the following block:

r = Report.new()

  image_arr = record[:image_group]

  image_arr.each do |image|
    r.images.attach(io: File.open("#{params[image[:report_image]].path}"), filename: "#{image[:report_image]}")
  end

r

This works when there are 1 or 2 images. However, when I do 3 or more the last report_image does not come with a path.

Here is the error:

NoMethodError (undefined method `path' for nil:NilClass):

How do I resolve the issue and save ALL of the images?

Here is the entire output from the rails server. *Some of the labels are changed in the following output. The important thing to see here is that the last image does not have a path.

financial_image_arr ======== [{"financial_page_image"=>"1565721255925.jpg"}, {"financial_page_image"=>"1565721267236.jpg"}, {"financial_page_image"=>"1565721279636.jpg"}, {"financial_page_image"=>"1565721292231.jpg"}]
image  ================ {"financial_page_image"=>"1565721255925.jpg"}
path  ================ /var/folders/zp/w8d7c5bs3glbprv_hs22m2k80000gn/T/RackMultipart20190813-79202-sfqs4i.jpg
  Disk Storage (13.7ms) Uploaded file to key: 65GicEnFZ7868efthA3VaZHs (checksum: l+IKb1kWR786TDXTtZPShw==)
   (0.7ms)  BEGIN
  ↳ app/controllers/skooter/odk_controller.rb:255
  ActiveStorage::Blob Create (1.3ms)  INSERT INTO "active_storage_blobs" ("key", "filename", "content_type", "metadata", "byte_size", "checksum", "created_at") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id"  [["key", "65GicEnFZ7868efthA3VaZHs"], ["filename", "1565721255925.jpg"], ["content_type", "image/jpeg"], ["metadata", "{\"identified\":true}"], ["byte_size", 2558086], ["checksum", "l+IKb1kWR786TDXTtZPShw=="], ["created_at", "2019-08-13 20:37:55.864812"]]
  ↳ app/controllers/skooter/odk_controller.rb:255
   (43.7ms)  COMMIT
  ↳ app/controllers/skooter/odk_controller.rb:255
image  ================ {"financial_page_image"=>"1565721267236.jpg"}
path  ================ /var/folders/zp/w8d7c5bs3glbprv_hs22m2k80000gn/T/RackMultipart20190813-79202-1ulro7l.jpg
  Disk Storage (15.3ms) Uploaded file to key: Dc8kVypDbmkKfC9wWiVKrQiP (checksum: ZdzLsDJlDRFcLtdUCTrOng==)
   (0.9ms)  BEGIN
  ↳ app/controllers/skooter/odk_controller.rb:255
  ActiveStorage::Blob Create (1.0ms)  INSERT INTO "active_storage_blobs" ("key", "filename", "content_type", "metadata", "byte_size", "checksum", "created_at") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id"  [["key", "Dc8kVypDbmkKfC9wWiVKrQiP"], ["filename", "1565721267236.jpg"], ["content_type", "image/jpeg"], ["metadata", "{\"identified\":true}"], ["byte_size", 3038981], ["checksum", "ZdzLsDJlDRFcLtdUCTrOng=="], ["created_at", "2019-08-13 20:37:55.949371"]]
  ↳ app/controllers/skooter/odk_controller.rb:255
   (41.7ms)  COMMIT
  ↳ app/controllers/skooter/odk_controller.rb:255
image  ================ {"financial_page_image"=>"1565721279636.jpg"}
path  ================ /var/folders/zp/w8d7c5bs3glbprv_hs22m2k80000gn/T/RackMultipart20190813-79202-19wtddw.jpg
  Disk Storage (15.3ms) Uploaded file to key: SXdChJUgv6KP5AimKsi51m1y (checksum: k2vsaTsVXn+P/E7uUNj0wg==)
   (0.7ms)  BEGIN
  ↳ app/controllers/skooter/odk_controller.rb:255
  ActiveStorage::Blob Create (1.2ms)  INSERT INTO "active_storage_blobs" ("key", "filename", "content_type", "metadata", "byte_size", "checksum", "created_at") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id"  [["key", "SXdChJUgv6KP5AimKsi51m1y"], ["filename", "1565721279636.jpg"], ["content_type", "image/jpeg"], ["metadata", "{\"identified\":true}"], ["byte_size", 2931139], ["checksum", "k2vsaTsVXn+P/E7uUNj0wg=="], ["created_at", "2019-08-13 20:37:56.045458"]]
  ↳ app/controllers/skooter/odk_controller.rb:255
   (41.5ms)  COMMIT
  ↳ app/controllers/skooter/odk_controller.rb:255
image  ================ {"financial_page_image"=>"1565721292231.jpg"}
Completed 500 Internal Server Error in 518ms (ActiveRecord: 171.8ms)



NoMethodError (undefined method `path' for nil:NilClass):

UPDATE

Here is the output when I add:

puts "params ================ #{params[image[:financial_page_image]]}"

financial_image_arr ======== [{"financial_page_image"=>"1565733645208.jpg"}, {"financial_page_image"=>"1565733658237.jpg"}, {"financial_page_image"=>"1565733673964.jpg"}, {"financial_page_image"=>"1565733688371.jpg"}]
params  ================ #<ActionDispatch::Http::UploadedFile:0x00007ffca0f4b290>
image  ================ {"financial_page_image"=>"1565733645208.jpg"}
path  ================ /var/folders/zp/w8d7c5bs3glbprv_hs22m2k80000gn/T/RackMultipart20190813-85337-ffz5sv.jpg
params  ================ #<ActionDispatch::Http::UploadedFile:0x00007ffca0f4b218>
image  ================ {"financial_page_image"=>"1565733658237.jpg"}
path  ================ /var/folders/zp/w8d7c5bs3glbprv_hs22m2k80000gn/T/RackMultipart20190813-85337-1fs8j1i.jpg
params  ================
image  ================ {"financial_page_image"=>"1565733673964.jpg"}

Update

Here is the entire output. *Some of the labels have changed here, but everything else is the same and in the same order.

=======================XML Object=======================
### XML Object: {"has_assignment_card"=>"no", "assignment_name"=>"Pqpqpq", "set_assignment_id"=>"1", "has_organization_card"=>"no", "organization_name"=>"Pqpqpq", "set_organization_id"=>"1", "attendance_page_yes_no"=>"yes", "attendance_page_group"=>[{"attendance_page_image"=>"1565900276910.jpg"}, {"attendance_page_image"=>"1565900288347.jpg"}, {"attendance_page_image"=>"1565900306471.jpg"}], "attendance_data_yes_no"=>"no", "start"=>"2019-08-15T14:17:28.668-06:00", "end"=>"2019-08-15T14:18:32.940-06:00", "today"=>"2019-08-15", "deviceid"=>"353325097059590", "subscriberid"=>"706010493091431", "simserial"=>"8950301218077912618", "username"=>"[email protected]", "phonenumber"=>nil, "meta"=>{"instanceID"=>"uuid:e291337f-2da5-45e1-8990-c23a1f3adb5e", "instanceName"=>"Activity:  2019-08-15"}, "id"=>"activity", "xmlns:ev"=>"http://www.w3.org/2001/xml-events", "xmlns:orx"=>"http://openrosa.org/xforms", "xmlns:xsd"=>"http://www.w3.org/2001/XMLSchema", "xmlns:odk"=>"http://www.opendatakit.org/xforms", "xmlns:jr"=>"http://openrosa.org/javarosa", "xmlns:h"=>"http://www.w3.org/1999/xhtml"}
=======================XML Object=======================
  User Load (0.8ms)  SELECT  "users".* FROM "users" WHERE "users"."email" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["email", "[email protected]"], ["LIMIT", 1]]
  ↳ app/controllers/skooter/odk_controller.rb:31
  Program Load (0.7ms)  SELECT  "programs".* FROM "programs" WHERE "programs"."id" = $1 LIMIT $2  [["id", 3], ["LIMIT", 1]]
  ↳ app/controllers/skooter/odk_controller.rb:31
image_arr ======== [{"attendance_page_image"=>"1565900276910.jpg"}, {"attendance_page_image"=>"1565900288347.jpg"}, {"attendance_page_image"=>"1565900306471.jpg"}]
puts "params[image[:attendance_page_image]]  ================ #<ActionDispatch::Http::UploadedFile:0x00007faa5bdd5560>
puts "params[image[:attendance_page_image]]  ================ #<ActionDispatch::Http::UploadedFile:0x00007faa5bdd44a8>
puts "params[image[:attendance_page_image]]  ================
  Rendering /Users/jefferson/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/bundler/gems/skooter-5bc83089cbef/app/views/skooter/odk/submissions.xml.erb
  Rendered /Users/jefferson/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/bundler/gems/skooter-5bc83089cbef/app/views/skooter/odk/submissions.xml.erb (0.5ms)
Completed 200 OK in 211ms (Views: 33.7ms | ActiveRecord: 11.1ms)

So, you can see that the first two images come with params and let me upload using .path, but the last one does not have params. It is nil.

Update

Here is the output when I add .inspect to the params:

=======================XML Object=======================
### XML Object: {"has_assignment_card"=>"no", "assignment_name"=>"Pqpqpq", "set_assignment_id"=>"1", "has_organization_card"=>"no", "organization_name"=>"Pqpqpq", "set_organization_id"=>"1", "attendance_page_yes_no"=>"yes", "attendance_page_group"=>[{"attendance_page_image"=>"1565900276910.jpg"}, {"attendance_page_image"=>"1565900288347.jpg"}, {"attendance_page_image"=>"1565900306471.jpg"}], "attendance_data_yes_no"=>"no", "start"=>"2019-08-15T14:17:28.668-06:00", "end"=>"2019-08-15T14:18:32.940-06:00", "today"=>"2019-08-15", "deviceid"=>"353325097059590", "subscriberid"=>"706010493091431", "simserial"=>"8950301218077912618", "username"=>"[email protected]", "phonenumber"=>nil, "meta"=>{"instanceID"=>"uuid:e291337f-2da5-45e1-8990-c23a1f3adb5e", "instanceName"=>"Activity:  2019-08-15"}, "id"=>"activity", "xmlns:ev"=>"http://www.w3.org/2001/xml-events", "xmlns:orx"=>"http://openrosa.org/xforms", "xmlns:xsd"=>"http://www.w3.org/2001/XMLSchema", "xmlns:odk"=>"http://www.opendatakit.org/xforms", "xmlns:jr"=>"http://openrosa.org/javarosa", "xmlns:h"=>"http://www.w3.org/1999/xhtml"}
=======================XML Object=======================
  User Load (3.5ms)  SELECT  "users".* FROM "users" WHERE "users"."email" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["email", "[email protected]"], ["LIMIT", 1]]
  ↳ app/controllers/skooter/odk_controller.rb:31
  Program Load (2.0ms)  SELECT  "programs".* FROM "programs" WHERE "programs"."id" = $1 LIMIT $2  [["id", 3], ["LIMIT", 1]]
  ↳ app/controllers/skooter/odk_controller.rb:31
image_arr ======== [{"attendance_page_image"=>"1565900276910.jpg"}, {"attendance_page_image"=>"1565900288347.jpg"}, {"attendance_page_image"=>"1565900306471.jpg"}]
params[image[:attendance_page_image]] ================ #<ActionDispatch::Http::UploadedFile:0x00007faa61f88d20 @tempfile=#<Tempfile:/var/folders/zp/w8d7c5bs3glbprv_hs22m2k80000gn/T/RackMultipart20190815-11949-hqxxbg.jpg>, @original_filename="1565900276910.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"1565900276910.jpg\"; filename=\"1565900276910.jpg\"\r\nContent-Type: image/jpeg\r\nContent-Length: 2768335\r\n">
params[image[:attendance_page_image]] ================ #<ActionDispatch::Http::UploadedFile:0x00007faa61f887a8 @tempfile=#<Tempfile:/var/folders/zp/w8d7c5bs3glbprv_hs22m2k80000gn/T/RackMultipart20190815-11949-l9mkv.jpg>, @original_filename="1565900288347.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"1565900288347.jpg\"; filename=\"1565900288347.jpg\"\r\nContent-Type: image/jpeg\r\nContent-Length: 3848689\r\n">
params[image[:attendance_page_image]] ================ nil
  Rendering /Users/jefferson/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/bundler/gems/skooter-5bc83089cbef/app/views/skooter/odk/submissions.xml.erb
  Rendered /Users/jefferson/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/bundler/gems/skooter-5bc83089cbef/app/views/skooter/odk/submissions.xml.erb (0.4ms)
Completed 200 OK in 277ms (Views: 40.2ms | ActiveRecord: 19.4ms)

Update

I just reviewed my code and it started working. Here is the block that is working saving all of the images: I am open to suggestions to improve this.

if record[:attendance_page_yes_no].eql?('yes')
  image_arr = record[:attendance_page_group]
  if image_arr.length > 1
    puts "image_arr ======== #{image_arr}"
    image_arr.each do |image|

      #puts "path ================ #{params[image[:attendance_page_image]].inspect}"
      #puts "name ================ #{image[:attendance_page_image]}"

      a.attendance_page_images.attach(io: File.open("#{params[image[:attendance_page_image]].path}"), filename: "#{image[:attendance_page_image]}")

    end if !image_arr.empty?
  else 
    a.attendance_page_images.attach(io: File.open("#{params[record[:attendance_page_group][:attendance_page_image]].path}"), filename: "#{record[:attendance_page_group][:attendance_page_image]}")
  end
end
13
  • 4
    you're expecting a passed param to be an object that has a path method which is why you're getting the error. params is a hash of objects — none of which should respond to path. Commented Aug 13, 2019 at 21:56
  • 1
    This params[image[:report_image]] is difficult to understand, are you trying to access a key on params? wouldn't it be params[:image][:report_image]? Commented Aug 15, 2019 at 21:12
  • 1
    Could you also show the complete output from params? It's also not easy to follow your log, when you can't see how the log was created. Commented Aug 15, 2019 at 21:29
  • 1
    For some files, ActionDispatch::Http::UploadedFile object could not be created. Application gives error because ActionDispatch::Http::UploadedFile does not exist. The problem seems to be in the file upload phase. Could it be because the file sizes are large? Commented Aug 15, 2019 at 21:42
  • 1
    I don't have any validations for file size Commented Aug 15, 2019 at 21:48

1 Answer 1

1
+50

ActionDispatch::Http::UploadedFile object could not be created for some images. Application gives error because ActionDispatch::Http::UploadedFile does not exist. The problem seems to be in the file upload phase. Images with a large file size may cause this error.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.