简体   繁体   English

Rails不知道如何构建任务

[英]Rails don't know how to build task

I am attempting to scrape data from a website. 我正在尝试从网站上抓取数据。 I am running rake from index.html.erb 我正在从index.html.erb运行rake

 <% Article.run_rake("fetch_games") %>

I have it defined in a .rb file. 我在.rb文件中定义了它。 Here is article.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

And here is the rakefile itself: article_task.rake 这是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

When I run this from the terminal it pulls what I need. 当我从终端运行它时,它会拉出我需要的东西。 But when I try to run it through rails server it gives me this error: 但是,当我尝试通过Rails服务器运行它时,出现了以下错误消息:

运行时错误

What am I doing wrong? 我究竟做错了什么? New to ruby/rails btw 红宝石/铁轨新手

We need to load_tasks before you run that. 在运行之前,我们需要先load_tasks。 You comment out that line - 您注释掉该行-

 load './lib/tasks/article_task.rake'

Do this - 做这个 -

  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

Rather than go through the contortions needed to load up an invoke a Rake task, you could move the code into a separate unit and load it in the model (and the Rake task, if you need it). 您可以将代码移到一个单独的单元中,然后将其加载到模型中(如果需要,也可以将其加载到Rake任务中),而不是通过加载调用Rake任务所需的扭曲。

module GameFetcher
  def fetch
    ...
  end
end

class Article < ActiveRecord::Base
  extend GameFetcher
  ...
end

Article.fetch

This also makes it easier to write a unit test for the fetch logic. 这也使得为fetch逻辑编写单元测试变得更加容易。

I went in another direction from the rake file. 我从rake文件转向了另一个方向。 I defined it in articles controller and called it in index.html.erb . 我在文章控制器中定义了它,并在index.html.erb中将其称为。 It now displays in the rails server. 现在,它显示在rails服务器中。

articles_controller.rb article_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 index.html.erb

<% @games.each do |game| %>
        <% if !game.at_css("caption").nil? %>
          <%= game.at_css("caption").text %>
        <% else %>
          <%= 'No Games Sheduled' %>
          <% end %>
        <% end %>

I think this thread can help: Run rake task in controller 我认为此线程可以帮助您: 在控制器中运行rake任务

Apparently, you have to call something like that before you call your task: 显然,在调用任务之前,您必须调用类似的名称:

NameOfYourApp::Application.load_tasks

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM