简体   繁体   English

Rake任务在读取文件时出现未定义的方法错误

[英]Rake Task Giving Undefined Method Error When Reading File

I've got a rake task that changes the meta tags on certain pages. 我有一个rake任务,可以更改某些页面上的meta标签。

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

I'm getting a cryptic error message: 我收到一条神秘的错误消息:

undefined method `[]' for nil:NilClass nil:NilClass的未定义方法“ []”

concerning this line: 关于这一行:

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

This line is supposed to figure out what the current tags are so that they can be replaced (via the next line of code). 该行应该找出当前标签是什么,以便可以替换它们(通过下一行代码)。

Any advice on what's out of place here? 关于这里不合适的地方有什么建议吗?

It's saying that 就是说

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

is nil or essentially 基本上是nil

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

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

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