没有这样的文件或目录@ rb_sysopen

时间:2014-04-02 17:21:07

标签: ruby csv

我正在使用Ruby 2.1.1当我运行此代码时:

<CSV.foreach("public/data/original/example_data.csv",headers: true, converters:              :numeric) do |info|

我收到错误:

No such file or directory @ rb_sysopen

如果我将example_data.csv放在如下所示的同一目录中,它会起作用,但是我的老板说他不希望所有*.csv文件放在不同的目录中:

<CSV.foreach("example_data.csv",headers: true, converters: :numeric) do |info|

1 个答案:

答案 0 :(得分:0)

我不得不使用绕过File Utilities的解决方法。使用thoughtbot / paperclip生成了一个名为csvcontroller的目录。我将csv文件放在该目录文件夹中。

class Uploader < ActiveRecord::Base
attr_accessible :purchase_name, :item_description, :item_price, :purchase_count,
                  :merchant_address, :merchant_name, :csvdata

has_attached_file :csvdata, :url => "/csvcontroller/:basename.:extension",

                :path => ":rails_root/csvcontroller/:basename.:extension"

                #:default_url => "/controllers/original/example_data.csv"

  validates_attachment_content_type :csvdata, :content_type => ["text/csv"]

end

然后我将解析器放在该目录中以避免使用FileUtils

require 'csv'

@total_cost = 0

#errors out FileUtils.move '/public/data/original/example_data.csv', '/controllers'

#errors out require File.expand_path('../app/public/data/original/', __FILE__)

# errors outCSV.foreach("any_path_name_outside_the_same_directory/example_data.csv", 
  #headers: true, converters: :numeric) do    |info|

CSV.foreach("example_data.csv", headers: true, converters: :numeric) do |info|

a =(info["item price"]).to_f

b = (info["purchase count"]).to_i

@total_cost += a * b
@store = []
customer = []
customer << info["purchaser name"]
@store << info["item description"]
@store << (info["item price"]).to_f
@store << (info["purchase count"]).to_i
@store << info["merchant address"]
@store << info["merchant name"]
puts @customer
puts @store
puts @total_cost
end

看起来很难看,但就是这样。 我无法使FileUtils ::类正常工作。这是2.1.1的Ruby错误