Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rake任务在读取文件时出现未定义的方法错误_Ruby On Rails_Rake - Fatal编程技术网

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

Ruby on rails Rake任务在读取文件时出现未定义的方法错误,ruby-on-rails,rake,Ruby On Rails,Rake,我有一个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/vi

我有一个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
这一行应该能够找出当前的标记是什么,以便可以替换它们(通过下一行代码)

有什么不合适的建议吗?

就是这么说的

File.open(filename,"r")).xpath("//meta[@name='robots']").first
nil
还是本质上

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

啊哈。非常感谢。这正是我需要知道的。原来那是空的,一旦我修好了,我就可以走了。再次感谢。
File.open(filename,"r")).xpath("//meta[@name='robots']").empty?  # true