CSV-Mapper 没有这样的文件或目录
我正在使用 csv-mapper gem 导入 csv 文件。当我使用自述文件中的示例代码时(http://github.com/pillowfactory/csv-mapper< /a>) 在脚本/控制台中效果很好。但是,当我创建一个 Web 表单并使用它上传 csv 文件时,我收到错误“没有这样的文件或目录 - test.csv
这些是参数: 参数:
{"dump"=>{"file"=>#}, "提交"=>"提交", "authenticity_token"=>"Hb+XDPUGyZQqB5H2vZhnlfXpEE9bAE16kAjTT34uQ3U="}
这是我在控制器中的代码:
def csv_import
results = CsvMapper.import(params[:dump][:file].original_filename) do
map_to Sale # Map to the Sale ActiveRecord class instead of the default Struct.
after_row lambda{|row, sale| sale.save } # Call this lambda and save each record after it's parsed.
start_at_row 1
[start_date, country]
end
flash[:notice] = "Successfully uploaded file"
end
I am using the csv-mapper gem to import a csv file. When I use the example code on in the README (http://github.com/pillowfactory/csv-mapper) in script/console it works great. However, when I create a web form and use that to upload a csv file I get the error "No such file or directory - test.csv
These are the parameters:
Parameters:
{"dump"=>{"file"=>#},
"commit"=>"Submit",
"authenticity_token"=>"Hb+XDPUGyZQqB5H2vZnhlfXpEE9bAE16kAjTT34uQ3U="}
Here is what I have for my code in the controller:
def csv_import
results = CsvMapper.import(params[:dump][:file].original_filename) do
map_to Sale # Map to the Sale ActiveRecord class instead of the default Struct.
after_row lambda{|row, sale| sale.save } # Call this lambda and save each record after it's parsed.
start_at_row 1
[start_date, country]
end
flash[:notice] = "Successfully uploaded file"
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这有点晚了,但您还应该注意,当您将 :type => 传递给 CsvMapper#import 时,它会接受任何 IO。 :io选项。
结果 = CsvMapper.import(params[:dump][:file], :type => :io) 执行
...
这将允许您跳过导入之前保存文件的步骤。
This is a little late, but you should also note that CsvMapper#import takes any IO when you pass it the :type => :io option.
results = CsvMapper.import(params[:dump][:file], :type => :io) do
...
end
This would allow you to skip the step of saving the file prior to importing.
该错误是预料之中的,因为
params[:dump][:file].original_filename
仅返回上传的 CSV 的文件名。上传的 CSV 应首先保存到文件系统。将保存的 CSV 文件的路径传递给CsvMapper#import
方法,然后它应该可以工作。请参阅此处了解如何保存上传的文件。
The error is expected because
params[:dump][:file].original_filename
only returns the file name of the uploaded CSV. The uploaded CSV should be saved to the file system first. Pass the path to the saved CSV file toCsvMapper#import
method then it should work.See here for how to save uploaded files.