繁体   English   中英

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

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

我有一个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

我收到一条神秘的错误消息:

nil:NilClass的未定义方法“ []”

关于这一行:

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

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

关于这里不合适的地方有什么建议吗?

就是说

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

基本上是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