在运行我的rake任务时遇到访问模块方法的问题,我现在得到的错误是
rake aborted!
NameError: uninitialized constant CustomerTestimonialGrabber
我已经用类似
的类设置了我的模块require 'open-uri'
require 'nokogiri'
module CustomerTestimonialGrabber
class Grabber
def perform
get_testimonials
end
def get_testimonials
url = 'http://www.ratedpeople.com/profile/lcc-building-and-construction'
doc = Nokogiri::HTML.parse(open url)
testimonial_section = doc.css('.homeowner-rating.content-block:not(.main-gallery):not(:last-child)').each do |t|
title = t.css('h4').text.strip
comment = t.css('q').text.strip
author = t.css('cite').text.strip
end
Testimonial.where(title: title, comment: comment, author: author).first_or_create!
end
end
end
我的rake任务设置了我通常的
namespace :grab do
task :customer_testimonials => :environment do
CustomerTestimonialGrabber::Grabber.new.perform
end
end
有人可以解释为什么我无法访问该模块,我也在我的application.rb文件中设置了这个
config.autoload_paths += %W(#{config.root}/lib)
任何帮助将不胜感激
修改
我已经做了一些阅读,现在在运行我的rake任务之前需要模块
require './lib/customer_testimonial_grabber/grabber.rb'
但现在我似乎无法访问模型本身
NameError: undefined local variable or method `title' for #<CustomerTestimonialGrabber::Grabber:0x00000004be3878>
答案 0 :(得分:1)
也许你应该将见证行放在上一个块中?
testimonial_section = doc.css('.homeowner-rating.content-block:not(.main-gallery):not(:last-child)').each do |t|
title = t.css('h4').text.strip
comment = t.css('q').text.strip
author = t.css('cite').text.strip
Testimonial.where(title: title, comment: comment, author: author).first_or_create!
end
因为标题没有在它之外定义。