0

So I'm using Spree as my shopping cart in Ruby on Rails. Spree is version 1-1-stable, and Ruby is v1.9.3, and Ruby on Rails is v3.2.3.

I have a remote host that has images that I want to download for my Spree cart. This is the code I'm using to pull it. Some of it may not make sense, because I'm trying to do whatever I can to get this to work so it could use a little cleaning.

# Add image to the product
        vendor_id = plink_and_pull(item, "VendorID")
        image_name = plink_and_pull(item, "ImageName")
        # TODO: add if image exists to this unless
        unless image_name.nil? || vendor_id.nil? || plink_and_pull(item, "ImageFound").to_i == 0 || File.exists?("/public/prod_images/#{vendor_id}/#{image_name.gsub(' ', '%20')}")
            unless Dir.exists? "/public/prod_images/#{vendor_id}"
                Dir.mkdir("/public/prod_images/#{vendor_id}", 777)
            end
            file = File.new("public/prod_images/#{vendor_id}/#{image_name.gsub(' ', '%20')}", 'w+')
            file.binmode

            open(URI.parse("http://login.xolights.com/vendors/#{vendor_id}/large/#{image_name.gsub(' ', '%20')}")) do |data|
                file.write data.read
            end
            img = Spree::Image.create({:attachment => "public/prod_images/#{vendor_id}/#{image_name}",
                                      :viewable => product}, :without_protection => true)
        end

But the error I get says "No such file or directory - /public/prod_images/29" and it references the "Dir.mkdir" line up there. However, I manually created this directory to try to get it to work. In my exception rescue I have the working directory printed out, which is the base directory of my app on my machine. (I am running this on localhost atm.)

I am thinking that maybe I need to do something in my routes.rb file? But I am such a novice at Ruby on Rails routes that I'm not sure where to start... or even if that's the problem here.

melissanoelle
  • 112
  • 1
  • 8
  • Did you create "/public/prod_images/" or "public/prod_images/"? (Note that the first one is under root, which is seldom a good idea) – Salil Jun 02 '12 at 09:15
  • Sorry, I should clarify. :) I was getting this error before I created the directories (and after too), and I just wanted to verify that the actual file upload was working, so in my GUI in RubyMine I literally right-clicked and created /prod_images and /29. :) – melissanoelle Jun 02 '12 at 13:00
  • I am not that familiar with RubyMine. But rest assured that it's not about routes. It's not Rails-specific. It's Ruby (and OS)-specific because Dir.mkdir is part of standard Ruby library. Just remove the leading / from the path and see whether it works. (So, the actual path will be {your Rails app's root directory}/public/prod_images – Salil Jun 03 '12 at 07:25
  • Hey you were totally right, but you put it as a comment so i can't put it as the right answer! :( I also had to change 777 to 0777 and it's working very well right now! :D – melissanoelle Jun 04 '12 at 16:11
  • Added as an answer. Do accept it if you found it useful :) – Salil Jun 05 '12 at 02:21

1 Answers1

0

I am not that familiar with RubyMine. But rest assured that it's not about routes. It's not Rails-specific. It's Ruby (and OS)-specific because Dir.mkdir is part of standard Ruby library. Just remove the leading / from the path and see whether it works. (So, the actual path will be {your Rails app's root directory}/public/prod_images

Salil
  • 9,534
  • 9
  • 42
  • 56