Discussion Forums
- Topic List
- Most Recent Posts
- Sign In for more options
Hi!
I'm new to Ruby On Rails and I've got some things I can't really figure out.
I've made a page to upload a zipfile, which contains XML-files. I've also been able to extract the zipfile, now having a lot of XML files.
Now I would like to take the information out of the XML file and put it in the database (sqlite3).
What should I put in the model? I've put the code to take out the information of the XML file in a controller, but what do I have to do to write it to the database?
Thanks a lot for helping me out .
Hi Jos Jones!
Sounds like your part way there! I think you are looking for this:
If your model is named MyData
my_new_data = MyData.new([:field_1 => xmldata, :field_2 => xmldata ....] ) unless my_new_data.save # something validation failed end
Obviously in some sort of loop. Quick suggestion, I would move the code for reading the xml data into the model. This will keep the controller clean and make your code look a lot better. Something like import_data_from_xml in your model.
Hope that helps, good luck with your project.
Z
Hi Jos, I assume you want to insert values from your xml files and NOT to insert the ENTIRE xml file. First you should parse the xml files, each at a time and access nodes (values on XML document) using an XML manipulation library such as REXML or Hpricot, when you get your values you simply create an instance like you always do. It is a good idea to have your XML node attributes match the Model's attributes (column names), that might make your work a lot easier than if they differ, than you'll have to do some adjustment work.
OK, I gave it a try, but I think I'm not doing very good.
I've got a controller called 'upload' and I've got a model called T512, which is empty.
The controller:
require 'zip/zipfilesystem'
class UploadController < ApplicationController
layout 'layout'
def index
render(:action => 'upload')
end
def upload
@page_title = 'Upload your xmlfiles'
end
def succesfull
@page_title = 'Successfully Uploaded the ZIP file!'
end
def uploadFile
post = XMLFile.save(params[:upload])
unzip_file('xml.zip', '~/xmls')
process_t512w
render(:action => 'succesfull')
end
def unzip_file (file, destination)
Zip::ZipFile.open(file) { |zip_file|
zip_file.each { |f|
f_path=File.join(destination, f.name)
FileUtils.mkdir_p(File.dirname(f_path))
zip_file.extract(f, f_path) unless File.exist?(f_path)
}
}
end
def process_t512w
@t512W_xml_string = File.read("T512W.xml")
@result = Hash.from_xml(@t512W_xml_string)
end
end
The model is:
class T512W < ActiveRecord::Base end
I'm still not very sure what the function of a model is. I've got some programming experience, but not at all with Ruby On Rails, or MVC. Is the model some kind of class, with datamembers and methods?
I think the controller fill the datamembers of an object, which is based on a model. I'm I correct about that?
@ Elad: yes, I've got a XML file with elements and attributes. In one way or another, I managed to create a table in my sqlite3 file, with the names of all elements from my XML file as column names. So I think I should indeed do the parsing?!
Thanks a lot!
