CSV-Mapper 没有这样的文件或目录

发布于 2024-09-25 19:14:20 字数 825 浏览 5 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓝梦月影 2024-10-02 19:14:20

这有点晚了,但您还应该注意,当您将 :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.

永不分离 2024-10-02 19:14:20

该错误是预料之中的,因为 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 to CsvMapper#import method then it should work.

See here for how to save uploaded files.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文