简体   繁体   English

NoMethodError:Roo :: CSV:Class的未定义方法“生成”

[英]NoMethodError: undefined method `generate' for Roo::CSV:Class

I try to made a scheduled daily csv mail. 我尝试制作预定的每日csv邮件。 I put inside my Importer class: " include Roo" then I put 2 functions: 我放入了Importer类:“ include Roo”,然后放入了2个函数:

def self.daily_mail
  Company.find_each do |c|
    export_to_csv(c)
  end
end

def self.export_to_csv(company)
  @opportunities = company.opportunities
  csv_data = CSV.generate do |csv|
    csv <<  ["Opportunity Id", "Created At", "Request Id", "Champion Id", "Referral_Id"]
    @opportunities.all.each do |opportunity|
      csv << [opportunity.id, opportunity.created_at, opportunity.request_id, opportunity.champion_id, opportunity.referral_id]
    end
  end
end

And in the scheduler class I coded: 在调度程序类中,我编写了代码:

task :export => :environment do
  puts "Exporting..."
    Importer.daily_mail
  puts "done."
end

However, when I tried to run in my terminal: 但是,当我尝试在终端中运行时:

rake export

I got: 我有:

rake aborted!
NoMethodError: undefined method `generate' for Roo::CSV:Class

Can someone help me pleas? 有人可以帮助我吗?

I'm not sure why you need Roo here, but only for exporting csv file, the standard CSV library is enough. 我不确定为什么这里需要Roo,但是仅对于导出csv文件而言,标准CSV库就足够了。 CSV.generate is a method from standard library, so to use this, you have to add require 'csv' to Importer file. CSV.generate是标准库中的一种方法,因此,要使用此方法,您必须在Importer文件中添加require'csv require 'csv'

You also need to remove "include Roo" to avoid conflict. 您还需要删除“ include Roo”以避免冲突。 But if you still want to work with Roo, please replace CSV.generate by ::CSV.generate . 但是,如果你仍然想的百亩工作,请更换CSV.generate通过::CSV.generate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 NoMethodError:#的未定义方法“类型” <Class> - NoMethodError: undefined method `type' for #<Class> flying sphinx noMethodError =&gt;未定义的方法&#39;生成&#39; - flying sphinx noMethodError => Undefined method 'generate' NoMethodError:[[a],“ b”]的未定义方法“ to_csv”:数组 - NoMethodError: undefined method `to_csv' for [“a”, “b”]:Array 来自类中Private方法的NoMethodError(未定义方法) - NoMethodError (undefined method) from Private method in class 返回NoMethodError的类方法:未定义的方法Rails - Class method returning NoMethodError: undefined method Rails NoMethodError(未定义方法),但是定义了类方法 - NoMethodError (undefined method) however class method is defined NoMethodError(Admin :: Notification:Class的未定义方法“ all”) - NoMethodError (undefined method `all' for Admin::Notification:Class) ActionMailer — NoMethodError:MyMailer:Class的未定义方法X - ActionMailer — NoMethodError: undefined method X for MyMailer:Class 未定义方法'访问'#<Class:XYZ>(NoMethodError)[rspec] - Undefined method 'visit' for #<Class:XYZ> (NoMethodError) [rspec] ActionMailer :: Base:Class的未定义方法(NoMethodError) - undefined method for ActionMailer::Base:Class (NoMethodError)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM