0

这是在我的 rails 应用程序的 lib/tasks 文件夹中,由于某种原因,我每次尝试运行任务时都会遇到错误。

desc 'Fetch product prices'
task :fetch_prices => :environment do

  require "nokogiri"
  require "open-uri"

  Product.each do |product|
    url = "http://www.lowes.ca/search/#{CGI.escape(product.title)}.html"
    doc = Nokogiri::HTML(open(url))
    price = doc.at_css(".fntlb").text[/[0-9\.]+/]
    product.update_attribute(:price, price)
    puts "Product #{Product.id} has been updated with price #{price}"
  end
end

以下是我尝试运行此任务时遇到的错误:rake aborted!'中未定义的方法each' for nil:NilClass /vagrant/depot/lib/tasks/product_prices.rake:7:in

4

2 回答 2

1

Product.all.each不想Product.each。虽然有点奇怪,但错误说它为零。

于 2013-07-10T19:04:33.307 回答
0

您需要定义您的产品变量

改变这个:

desc 'Fetch product prices'
task :fetch_prices => :environment do

require "nokogiri"
require "open-uri"

 Product.each do |product|
  url = "http://www.lowes.ca/search/#{CGI.escape(product.title)}.html"
  doc = Nokogiri::HTML(open(url))
  price = doc.at_css(".fntlb").text[/[0-9\.]+/]
  product.update_attribute(:price, price)
  puts "Product #{Product.id} has been updated with price #{price}"
 end
end

对此:

desc 'Fetch product prices'
task :fetch_prices => :environment do

require "nokogiri"
require "open-uri"

url = "http://www.lowes.ca/search/#{CGI.escape(product.title)}.html"
doc = Nokogiri::HTML(open(url))
product = doc.css('CSS SELECTER FOR YOUR CONTENT')


 Product.each do |product|  
  price = doc.at_css(".fntlb").text[/[0-9\.]+/]
  product.update_attribute(:price, price)
  puts "Product #{Product.id} has been updated with price #{price}"
 end
end
于 2015-03-05T01:55:42.877 回答