Ruby on Rails中的文件出现意外行为

Ruby on Rails中的文件出现意外行为,ruby,ruby-on-rails-3.2,Ruby,Ruby On Rails 3.2,在尝试用RubyonRails上传文件时,我遇到了一个问题 以下是我上传文件的方式: def upload_image(image) File.new(Rails.root.join('assets','images','products',image.original_filename),'wb') do |f| f.write(image.read) end end 这会引发异常: Errno::ENOENT in ProductsController#update No

在尝试用RubyonRails上传文件时,我遇到了一个问题

以下是我上传文件的方式:

def upload_image(image)
  File.new(Rails.root.join('assets','images','products',image.original_filename),'wb') do |f|
    f.write(image.read)
  end
end
这会引发异常:

Errno::ENOENT in ProductsController#update

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

为什么会这样?我只是创建一个新文件,而不是试图打开一个现有文件。

它不创建目录

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)
如果你添加了一个/应用,你就有了你要寻找的路径。但不要真的认为这是使用资产管道的方式。参见中的推理


存在目录“/assets/images/products/”。关于保存图像的路径,您有什么建议吗?是的,这些不是静态图像,我将上传它们。这些是网上商店中“产品”的图片。我会经常上传它们。也就是说,有一种静态图像。我应该使用资产还是公用文件夹?
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%