rails LoadError:无法加载此类文件 - lib / scraper

时间:2016-12-18 19:00:23

标签: ruby-on-rails ruby rake

在我的rails应用程序中,我有一个rake任务从另一个网页上删除数据。我想将方法​​功能从rake任务移到ruby类或模块中。要做的是,我在lib / tasks中有rake任务,然后是lib中的scraper.rb。在rake任务中,我需要'lib / scraper',但这会引发错误。

这是我的佣金任务:

require "lib/scraper"
namespace :some_namespace do
    desc "A description"
    task :scrape_info => :environment do
        scraper = Scraper.new
        scraper.scrape_info
    end
end

红宝石刮刀类:

require 'mechanize'
class Scraper
    def scrape_info
        mechanize = Mechanize.new

        # Scrape players from fox sports
        url = "someurl"

        # do some other stuff
    end
end

1 个答案:

答案 0 :(得分:1)

您的代码应如下所示。

<强> lib/scraper.rb

require 'mechanize'

module Scraper
  class Scraper
    def scrape_info
    end
  end
end

<强> lib/tasks/some_namespace.rake

namespace :some_namespace do
  task some_task :environment do
    include Scraper
  end
end