运行 变成 "uninitialized constant" 每当 gem
Running into "uninitialized constant" with whenever gem
今天尝试使用 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
lib/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
config/schedule.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
据我所知,您已将 RedditScrapper
定义为模块,但您正试图将其用作 class...(即调用其上的方法)。
您可以:将其变成 class(只需将 module
更改为 class
)或将所有相关方法定义为 module_function
s
根据您选择的用途,前者可能更可取。
Rails 不会自动加载 lib
文件夹。您需要将以下行添加到 config/application.rb
:
config.autoload_paths << Rails.root.join('lib')
今天尝试使用 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
lib/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
config/schedule.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
据我所知,您已将 RedditScrapper
定义为模块,但您正试图将其用作 class...(即调用其上的方法)。
您可以:将其变成 class(只需将 module
更改为 class
)或将所有相关方法定义为 module_function
s
根据您选择的用途,前者可能更可取。
Rails 不会自动加载 lib
文件夹。您需要将以下行添加到 config/application.rb
:
config.autoload_paths << Rails.root.join('lib')