I have a form_tag on a “Stream” model which accepts user input as show below:
<%= form_tag('/generate_xml', :method=>'post') do %>
<p>
Content:<br />
<%= text_field_tag "content" %>
</p>
<p>
<%= submit_tag 'Submit' %>
</p>
<% end %>
The generate_xml action is as below:
def generate_xml
@content = params[:content]
builder = Nokogiri::XML::Builder.new do |xml|
xml.streams {
xml.content {
xml.name { xml.text @content }
}
}
end
file = File.new('dir.xml','w')
file.puts builder.to_xml
file.close
end
While I can generate a file dir.xml I do not want this file to be stored locally but in the database directly.How can I do this? I so far have tried generating file-related columns in the “Stream” model(where I want to store the xml files) using paperclip but I stuck about how I can generate a file on the fly and save it to database.
You could store the xml content as a string.
So, do a rails migration to add the string column to your table.
Then just store it as a string in there: