I had some issues this week figuring out how to save a file using AttachmentFu. It is well suited to handling files uploaded via a Web form, but when you load a file in programmatically, it isn’t obvious what object properties need to be set. Here is what I ended up with.
response = HttpClient.default.request(url)
mugshot = Mugshot.new
temp_file_name = mugshot.write_to_temp_file(response.body)
mugshot.temp_path = temp_file_name
mugshot.content_type = “image/#{content_type}”
mugshot.filename = “imported_photo.#{content_type}”
if person.mugshot
# remove existing database records of files for this person
thumbnail = Mugshot.find_by_parent_id(person.mugshot.id)
thumbnail.destroy
person.mugshot.destroy
end
mugshot.person = person
mugshot.save