whenever
今天尝试使用gem。uninitialized constant EntriesController::RedditScrapper
遇到这个错误......我该如何解决这个问题?
电流控制器
class EntriesController < ApplicationController
def index
@entries = Entry.all
end
def scrape
RedditScrapper.scrape
respond_to do |format|
format.html { redirect_to entries_url, notice: 'Entries were successfully scraped.' }
format.json { entriesArray.to_json }
end
end
end
库/reddit_scrapper.rb
require 'open-uri'
module RedditScrapper
def self.scrape
doc = Nokogiri::HTML(open("https://www.reddit.com/"))
entries = doc.css('.entry')
entriesArray = []
entries.each do |entry|
title = entry.css('p.title > a').text
link = entry.css('p.title > a')[0]['href']
entriesArray << Entry.new({ title: title, link: link })
end
if entriesArray.map(&:valid?)
entriesArray.map(&:save!)
end
end
end
配置/计划.rb
RAILS_ROOT = File.expand_path(File.dirname(__FILE__) + '/')
every 2.minutes do
runner "RedditScrapper.scrape", :environment => "development"
end
请帮我找出正确的跑步任务来写......
Application.rb
require_relative 'boot'
require 'rails/all'
Bundler.require(*Rails.groups)
module ScrapeModel
class Application < Rails::Application
config.autoload_paths << Rails.root.join('lib')
end
end
Rails 不会自动加载lib
文件夹。您需要将以下行添加到您的config/application.rb
:
config.autoload_paths << Rails.root.join('lib')
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句