2012-07-27 33 views
0

我有一个rake任务,用于更改某些页面上的元标记。Rake任务在读取文件时给出未定义的方法错误

desc 'changes the meta tags' 
task mixup_meta_tags: :environment do 
    meta_tags = ['index','noindex','index,nofollow','nofollow,noindex'] 
    new_tag = meta_tags.sample(1)[0] 
    # 
    #iterate through html docs 
    base = 'app/views/site' 
    pages = ['home','about','products','pricing'] 
    pages.each do |page| 
    filename = base + '/' + page + '.html.haml' 
    text = File.read(filename) 
    current_tag = Nokogiri::HTML(File.open(filename, "r")).xpath("//meta[@name='robots']").first["content"] 
    File.open(filename, "w") { |file| file << text.gsub(current_tag,new_tag)} 
    end 
end 

我得到一个神秘的错误消息:

未定义的方法`[]”的零:NilClass

关于这一行:

current_tag = Nokogiri::HTML(File.open(filename,"r")).xpath("//meta[@name='robots']").first["content"] 

这条线应该找出当前标签是什么,以便它们可以被替换(通过下一行代码)。

有什么建议不在这里?

回答

1

它说

File.open(filename,"r")).xpath("//meta[@name='robots']").first 

nil或基本

File.open(filename,"r")).xpath("//meta[@name='robots']").empty? # true 
+0

啊哈。谢谢!!这正是我需要知道的。事实证明这是空的,一旦我解决了这个问题,我就很好。再次感谢。 – 2012-07-27 01:40:40

相关问题