nokogiri replace
I'm parsing an HTML document and trying to replace the image src. It seems to do what I want when I attempt it in the console however in my model it doesn't seem to save it. Now, I'm not sure if what I'm doing is wrong with the way to save in Rails (i'm trying to update the content field and replacing external images with local ones) or if it's using nokogiri, but it's not saving the result using the set_attribute
method
It does the rest of it perfectly.
before_save :replace_zemanta_images
def replace_zemanta_images
doc = Nokogiri::HTML(content)
unless doc.css('div.zemanta-img').blank?
doc.css('div.zemanta-img img').each do |img|
io = open(URI.parse(img[:src]))
if photos.find_by_data_remote_url(img[:src]).blank?
photo = photos.build(:data => io, :data_remote_url => img[:src])
img.set_attr开发者_StackOverflow中文版ibute('src', photo.data.url(:original)) #doesn't work!
end
end
end
end
I am assuming that content
is an attribute on your model.
When you are doing img.set_attribute
you are updating the attribute in the Nokogiri::XML::Element
object but this doesn't update the text of content
.
At the end of your method you will need to add something like:
self.content = doc.to_s
JackChance mentioned to use Nokogiri::HTTP::DocumentFragment.parse(content)
here for a fragment (if you don't want DOCTYPE/HTML/BODY tags), I didn't have any luck with that since my original HTML was a snippet instead of the whole document.
I ended up using something like this:
html = Nokogiri::HTML.fragment
to initially convert the HTML string snippet to a Nokogiri object without the unnecessary tags.
Then once we use img.set_attribute
, we can convert back html.to_s
精彩评论