如何使用nokogiri替換所有帶圖像標籤的img標籤?這是爲了利用Rails自動插入正確資產服務器的能力?使用Nokogiri用<%= image_tag%>替換<img src />標籤?
require 'nokogiri'
class ToImageTag
def self.convert
Dir.glob("app/views/**/*").each do |filename|
doc = Nokogiri::HTML(File.open(filename))
doc.xpath("//img").each |img_tags|
# grab the src and all the attributes and move them to ERB
end
# rewrite the file
end
rescue => err
puts "Exception: #{err}"
end
end
也許你可以用一個例子來澄清你想要做什麼,爲什麼? 是您想要更改資產服務器,並添加緩存清除時間戳嗎? – 2011-06-15 19:28:21