加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 如何在Rails Rake任务中使用File.open?

发布时间:2020-12-17 03:13:29 所属栏目:百科 来源:网络整理
导读:我需要创建一个定期任务,从外部文本文件创建(或编辑)产品记录.来自irb内部: f = File.open(filename) # file in same directory path 没有问题. 但是当粘贴到Rake任务文件时,脚本总是炸弹“未找到文件”. (Rails 3.1,Ubuntu.) namespace :sap do desc "uplo
我需要创建一个定期任务,从外部文本文件创建(或编辑)产品记录.来自irb内部:

>> f = File.open(<filename>) # file in same directory path

没有问题.

但是当粘贴到Rake任务文件时,脚本总是炸弹“未找到文件”. (Rails 3.1,Ubuntu.)

namespace :sap do
  desc "uploads data from raw SAP file"
  task :upload => :environment do
     f = File.open("sap_pnlist_20111010a.csv")
     records = f.readlines
     records.each {|row|
     ... etc etc ...
     }
  end
end

建议?

解决方法

如果文件位于Rails根目录中的某个位置,请使用

Rails.root.join('grandparent_dir','parent_dir','file.txt')

如果该文件不在您的Rails根目录中,则必须为其提供完整路径.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读