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
doc = Nokogiri::HTML(content)
doc.css('div.zemanta-img img').each do |img|
io = open(URI.parse(img[:src]))
photo = photos.build(:data => io, :data_remote_url => img[:src])
img.set_attribute('src', photo.data.url(:original)) #doesn't work!
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
At the end of your method you will need to add something like:
self.content = doc.to_s