holden - 10 months ago 53
Ruby Question

# 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_attribute('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.
self.content = doc.to_s