Reputation: 48443
I need to generate all my products from database table into the XML file. Because my app run on Heroku, I need to use Amazon S3 as the storage.
Here's an example of on how to save an image:
has_attached_file :photo,
:styles => {
:thumb=> "100x100#",
:small => "400x400>" },
:storage => :s3,
:s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
:path => "/:style/:id/:filename"
Which makes me a bit confused... because I am not sure about the logic of this task.
My idea is to run an action (say create_xml_feed
) in the Products
controller
xml = Builder::XmlMarkup.new( :indent => 2 )
xml.instruct! :xml, :encoding => "ASCII"
xml.product do |p|
p.name "Test"
end
But here comes the problem - I don't know, how to save the newly created file into Amazon S3 Bucket.
I'd be grateful for each advance, thank you
Upvotes: 1
Views: 3311
Reputation: 76
First, create an active record class to hold your uploaded XML file. After this you can write the logic to create your xml string, create a new file by first creating a new instance of the XmlUploader class, then create the xml string you want, save it in a file and then assign this file as the attachment file. Once you save your xml file will be uploaded to s3.
class XmlUploader < ActiveRecord::Base
has_attached_file :uploaded_file, :storage => :s3,
:s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
:path => "/:id/:filename"
def create_uploaded_file
xml = ::Builder::XmlMarkup.new( :indent => 2 )
xml.instruct! :xml, :encoding => "ASCII"
xml.product do |p|
p.name "Test"
end
file_to_upload = File.open("some-file-name", "w")
file_to_upload.write(xml)
file_to_upload.close()
self.uploaded_file = File.open("some-file-name")
self.save!
end
end
class CreateXmlUploaders < ActiveRecord::Migration
def change
create_table :xml_uploaders do |t|
t.attachment :uploaded_file
t.timestamps
end
end
end
xml_file = XmlUploader.new
xml_file.create_uploaded_file
Upvotes: 5