Rails 3.0 中的自定义 Rake 任务中 Rails.root 为空
也许我在升级文档中错过了这一点,但如果我在 Rails 控制台中输出“Rails.root”,我会看到我的应用程序的根路径。但如果我在自定义 Rake 任务中引用“Rails.root”,它就是空的。我缺少什么?提前致谢。
示例:
namespace :admin do
desc "Import some data"
task :import => :environment do
csv = Rails.root + "/test/data.csv"
raise "#{csv} does not exit. Stopping task." if !File.exists?(csv)
CSV.foreach(csv, :headers => :first_row) do |row|
puts(row['id'])
end
end
end
我每次都会遇到异常,因为“Rails.root”是“”。
Maybe I missed this in the upgrade documentation, but if I output "Rails.root" in the rails console I see my app's root path. But if I reference "Rails.root" in a custom Rake task, it's empty. What am I missing? Thanks in advance.
Sample:
namespace :admin do
desc "Import some data"
task :import => :environment do
csv = Rails.root + "/test/data.csv"
raise "#{csv} does not exit. Stopping task." if !File.exists?(csv)
CSV.foreach(csv, :headers => :first_row) do |row|
puts(row['id'])
end
end
end
I get an exception every time because "Rails.root" is "".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试使用 join 方法
csv 成为文件的
路径名
。Try with join method
csv become a
Pathname
of your file.作为参考:
也可以,它是前导斜线把事情搞砸了。
也有效(你实际上需要那里的“前导”斜杠)。
For reference:
will work as well, it's the leading slash that screws things up.
also works (you actually need the "leading" slash there).