1

While trying to upload a file in Ruby on Rails, I ran into an issue.

Here is how I upload a file:

def upload_image(image)
  File.new(Rails.root.join('assets','images','products',image.original_filename),'wb') do |f|
    f.write(image.read)
  end
end

Which throws an exception:

Errno::ENOENT in ProductsController#update

No such file or directory - /home/alex/RubymineProjects/psg/assets/images/products/my-image.png

Why is this happening? I'm just creating a new file, I'm not trying to open an existing one.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303

1 Answers1

0

It does not create directories.

File.new("test", 'wb') #=> creates the file test
File.new("test/test", 'wb') #=> test.rb:1:in `initialize': No such file or directory - test/test (Errno::ENOENT)

If you add an /app you have the path you are looking for. Don't really think thats the way to use the asset pipeline though. See reasoning in this question.

File.open(Rails.root.join('app','assets','images','test.jpg'),'wb') do |f|
  f.write("image")
end
 => 5

cat app/assets/images/test.jpg #=> image% 
Community
  • 1
  • 1
Patrik Björklund
  • 1,217
  • 3
  • 11
  • 25
  • the directory `/assets/images/products/' exists. –  Aug 27 '12 at 16:02
  • What would you recommend me about the path of saving images? Yes, these are not static images and I'll be uploading them. These are the images of "products" in internet shop. And I'm going to upload them frequently. That is, there are kind of the static images. Should I use assets or public folder? –  Aug 27 '12 at 16:51