Rails不知道如何构建任务
Rails don't know how to build task
我正在尝试从网站上抓取数据。我是来自 index.html.erb
的 运行ning rake
<% Article.run_rake("fetch_games") %>
我在 .rb 文件中定义了它。这里是article.rb
require 'rake'
require 'rubygems'
#load './lib/tasks/article_task.rake'
class Article < ActiveRecord::Base
def self.run_rake(fetch_games)
Rails.root + "lib/tasks/article_task.rake"
Rake::Task["fetch_games"].invoke
end
end
这里是 rakefile 本身:article_task.rake
desc "Fetch Games"
task :fetch_games => :environment do
require 'nokogiri'
require 'open-uri'
url = "http://espn.go.com/nba/schedule"
data = Nokogiri::HTML(open(url))
games = data.css('.responsive-table-wrap')
games.each do |game|
#check for a listing
if !game.at_css("caption").nil?
#Date
puts game.at_css("caption").text
else
puts "No Listing"
end
#check for the team name
if !game.at_css(".team-name").nil?
#Team name
puts game.at_css(".team-name").text
else
puts "No Games Scheduled"
end
#empty
puts ""
end
end
当我从终端 运行 执行此操作时,它会提取我需要的内容。但是当我尝试通过 rails 服务器 运行 它时,它给了我这个错误:
我做错了什么? ruby/rails 顺便说一句
的新手
我们需要 load_tasks 在您 运行 之前。你注释掉那一行 -
load './lib/tasks/article_task.rake'
这样做 -
require 'rake'
require 'rubygems'
load './lib/tasks/article_task.rake'
class Article < ActiveRecord::Base
def self.run_rake(fetch_games)
Rake::Task["fetch_games"].invoke
end
end
我认为这个帖子可以提供帮助:Run rake task in controller
显然,您必须在调用任务之前调用类似的东西:
NameOfYourApp::Application.load_tasks
您可以将代码移动到一个单独的单元并将其加载到模型中(如果需要,还可以加载 Rake 任务),而不是经历加载调用 Rake 任务所需的曲折。
module GameFetcher
def fetch
...
end
end
class Article < ActiveRecord::Base
extend GameFetcher
...
end
Article.fetch
这也使得为 fetch
逻辑编写单元测试变得更加容易。
我从 rake 文件转向了另一个方向。我在 articles controller 中定义了它,并在 index.html.erb
中调用了它。它现在显示在 rails 服务器中。
articles_controller.rb
require 'nokogiri'
require 'open-uri'
def index
url = "http://espn.go.com/nba/schedule"
data = Nokogiri::HTML(open(url))
@games = data.css('.responsive-table-wrap')
end
index.html.erb
<% @games.each do |game| %>
<% if !game.at_css("caption").nil? %>
<%= game.at_css("caption").text %>
<% else %>
<%= 'No Games Sheduled' %>
<% end %>
<% end %>
我正在尝试从网站上抓取数据。我是来自 index.html.erb
的 运行ning rake <% Article.run_rake("fetch_games") %>
我在 .rb 文件中定义了它。这里是article.rb
require 'rake'
require 'rubygems'
#load './lib/tasks/article_task.rake'
class Article < ActiveRecord::Base
def self.run_rake(fetch_games)
Rails.root + "lib/tasks/article_task.rake"
Rake::Task["fetch_games"].invoke
end
end
这里是 rakefile 本身:article_task.rake
desc "Fetch Games"
task :fetch_games => :environment do
require 'nokogiri'
require 'open-uri'
url = "http://espn.go.com/nba/schedule"
data = Nokogiri::HTML(open(url))
games = data.css('.responsive-table-wrap')
games.each do |game|
#check for a listing
if !game.at_css("caption").nil?
#Date
puts game.at_css("caption").text
else
puts "No Listing"
end
#check for the team name
if !game.at_css(".team-name").nil?
#Team name
puts game.at_css(".team-name").text
else
puts "No Games Scheduled"
end
#empty
puts ""
end
end
当我从终端 运行 执行此操作时,它会提取我需要的内容。但是当我尝试通过 rails 服务器 运行 它时,它给了我这个错误:
我做错了什么? ruby/rails 顺便说一句
的新手我们需要 load_tasks 在您 运行 之前。你注释掉那一行 -
load './lib/tasks/article_task.rake'
这样做 -
require 'rake'
require 'rubygems'
load './lib/tasks/article_task.rake'
class Article < ActiveRecord::Base
def self.run_rake(fetch_games)
Rake::Task["fetch_games"].invoke
end
end
我认为这个帖子可以提供帮助:Run rake task in controller
显然,您必须在调用任务之前调用类似的东西:
NameOfYourApp::Application.load_tasks
您可以将代码移动到一个单独的单元并将其加载到模型中(如果需要,还可以加载 Rake 任务),而不是经历加载调用 Rake 任务所需的曲折。
module GameFetcher
def fetch
...
end
end
class Article < ActiveRecord::Base
extend GameFetcher
...
end
Article.fetch
这也使得为 fetch
逻辑编写单元测试变得更加容易。
我从 rake 文件转向了另一个方向。我在 articles controller 中定义了它,并在 index.html.erb
中调用了它。它现在显示在 rails 服务器中。
articles_controller.rb
require 'nokogiri'
require 'open-uri'
def index
url = "http://espn.go.com/nba/schedule"
data = Nokogiri::HTML(open(url))
@games = data.css('.responsive-table-wrap')
end
index.html.erb
<% @games.each do |game| %>
<% if !game.at_css("caption").nil? %>
<%= game.at_css("caption").text %>
<% else %>
<%= 'No Games Sheduled' %>
<% end %>
<% end %>