Download presentation
Presentation is loading. Please wait.
1
XML and Ruby on Rails Jennifer Andrews LIS 531F April 25,2007
2
Strengths of databases Effective searching Efficient data storage (no redundancy) Scalable Reliable Good security features Been around a long time Part of the business structure
3
Strengths of XML Human readable Open standard, not tied to a platform Structure part of document facilitates transfer and sharing of information Flexible Becoming more important with increase of information sharing across businesses and applications
4
Relational Database Stores data in tables made up of rows and columns Each row represents a record Order of records irrelevant Many tables connected by “keys” Can express complex relationships between tables
5
XML Hierarchical Harder to express complex relationships Order of data important Preservation of order?
6
Ruby on Rails Based on Ruby Web based database applications Basic structure built into Rails All pieces of the application interact in standard way Advantage? Easy and speedy development of applications OPEN SOURCE
7
Architecture MVC or Model, View, Controller Controller = conductor Model = data handling, enforces “rules” Views = HTML, interface for interaction with user, but only displays, never handles data
8
Step 1 – create database and tables using SQL
9
New material Fiber Weight Back Brand Color
11
Table in SQL
12
Now what? Create an XML document Create view or “template” saved as.rxml One small piece of code in the controller Point browser to that view And …..
13
Code to generate XML resides in template xml.instruct! :xml, :version=>"1.0" xml.channel { for m in @materials xml.material do xml.title(m.fiber) xml.description(m.weight) xml.brand(m.brand) xml.color(m.color) end end }
14
Material Controller class MaterialController < ApplicationController def index list render :action => 'list' end # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html) verify :method => :post, :only => [ :destroy, :create, :update ], :redirect_to => { :action => :list } def list @material_pages, @materials = paginate :materials, :per_page => 10 end def show @material = Material.find(params[:id]) end def new @material = Material.new end def create @material = Material.new(params[:material]) if @material.save flash[:notice] = 'Material was successfully created.' redirect_to :action => 'list' else render :action => 'new' end def create @material = Material.new(params[:material]) if @material.save flash[:notice] = 'Material was successfully created.' redirect_to :action => 'list' else render :action => 'new' end def edit @material = Material.find(params[:id]) end def update @material = Material.find(params[:id]) if @material.update_attributes(params[:material]) flash[:notice] = 'Material was successfully updated.' redirect_to :action => 'show', :id => @material else render :action => 'edit' end def destroy Material.find(params[:id]).destroy redirect_to :action => 'list' end def materialsml @materials = Material.find(:all) render :layout => false end
16
How does it happen? MAGIC! A piece of Ruby code called Builder Takes what is after the xml. and turns it into a tag – xml.description becomes –can include attibutes, too xml.price(p.price, currency => “USD”) becomes 50.00.
17
Real Life Application Rails app collecting orders for books Part of that process – collecting names, addresses Marketing department wants names of all who bought a certain book (in XML) to feed into a another program they have.
18
Another way Autogenerate through command called to_xml Dumps everything No control over order of elements Possible to add code so if a request comes for HTML – that is what’s returned, if the request is for XML, that is what is returned
19
Bits and pieces Can you use XML to input data into RonR? Installing RonR on computer –Locomotive 2 tutorials Put your XML knowledge to use Start simple, ride the rails, and be amazed
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.