Ruby on rails Rake任务在读取文件时出现未定义的方法错误
我有一个rake任务,可以更改某些页面上的meta标记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
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