Upload images to a product via Ruby SDK

Highlighted
New Member
2 0 1

Hi all.

I've been trying, unsuccessfully, to add images to existing products using the Ruby SDK. 
The SDK doc and Google are surprisingly unhelpful.

I tried a few variations of what could make sense.

Adding the image to the images array:

p = @shopify_products.first
f = Base64.encode64(File.read(file_path))
p.images << {'attachement' => f, 'filename' => "blah"}
p.save

This returns without error and time suggest the image is uploaded, but the image is not saved in the end.
(this was the closest I got).

Creating an image object (ShopifyAPI::Image):

f = Base64.encode64(File.read(file_path))
i = ShopifyAPI::Image.new
i.attach_image = f
i.prefix_options={ :product_id => my_product_id }
i.save

This errors with :image=>["undefined method `first' for nil:NilClass"]

…and so on for quite some time.

Would anyone have a code snippet, or a link to a resource for this specific step ?
I have found resources using curl, but my whole workflow is in Ruby, I'd really prefer sticking with it.

Thank you in advance.

Raphael.

0 Likes
Highlighted
New Member
2 0 1

A good night of sleep and another look at the source of the gem gave me the answer:

p = @shopify_products.first
f = File.read(file_path)
i = ShopifyAPI::Image.new
i.attach_image(f) # <-- attach_image is a method, not an attribute
p.images << i
p.save

 

1 Like