Docstoc

XML and Ruby on Rails

Document Sample
XML and Ruby on Rails Powered By Docstoc
					XML and Ruby on Rails


            Jennifer Andrews
               LIS 531F
              April 25,2007
        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
         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
        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
                    XML
•   Hierarchical
•   Harder to express complex relationships
•   Order of data important
•   Preservation of order?
             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
                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
Step 1 – create database and tables using SQL


+--------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra               |
+--------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| fiber | varchar(20) | NO | | |                     |
| weight | varchar(20) | YES | | NULL |                       |
| brand | varchar(20) | YES | | NULL |                       |
| color | varchar(20) | YES | | NULL |                     |
+--------+-------------+------+-----+---------+----------------+
5 rows in set (0.01 sec)
New material
Fiber



Weight



Brand



Color




Create


Back
Material was successfully created.

Listing materials

Fiber       Weight   Brand           Color

wool        2/20     Maine Line      red        Show   Edit   Destroy
Wool/silk   2/28     Zephyr          sea blue   Show   Edit   Destroy

cotton      3/2                      yellow     Show   Edit   Destroy

alpaca      sport    misti           brown      Show   Edit   Destroy


New material
Table in SQL

mysql> select * from materials;
+----+-----------+--------+------------+----------+
| id | fiber | weight | brand | color |
+----+-----------+--------+------------+----------+
| 1 | wool | 2/20 | Maine Line | red |
| 2 | Wool/silk | 2/28 | Zephyr | sea blue |
| 3 | cotton | 3/2 |           | yellow |
| 4 | alpaca | sport | misti | brown |
+----+-----------+--------+------------+----------+
4 rows in set (0.00 sec)
                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 …..
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 }
                                               Material Controller
class MaterialController < ApplicationController                     def create
 def index                                                             @material = Material.new(params[:material])
   list                                                                if @material.save
   render :action => 'list'                                              flash[:notice] = 'Material was successfully created.'
 end                                                                     redirect_to :action => 'list'
                                                                       else
 # GETs should be safe (see                                              render :action => 'new'
http://www.w3.org/2001/tag/doc/whenToUseGet.html)                      end
 verify :method => :post, :only => [ :destroy, :create, :update ],    end
       :redirect_to => { :action => :list }
                                                                      def edit
 def list                                                              @material = Material.find(params[:id])
  @material_pages, @materials = paginate :materials, :per_page        end
=> 10
 end                                                                  def update
                                                                       @material = Material.find(params[:id])
 def show                                                              if @material.update_attributes(params[:material])
  @material = Material.find(params[:id])                                 flash[:notice] = 'Material was successfully updated.'
 end                                                                     redirect_to :action => 'show', :id => @material
                                                                       else
 def new                                                                 render :action => 'edit'
  @material = Material.new                                             end
 end                                                                  end

 def create                                                           def destroy
  @material = Material.new(params[:material])                          Material.find(params[:id]).destroy
  if @material.save                                                    redirect_to :action => 'list'
    flash[:notice] = 'Material was successfully created.'             end
    redirect_to :action => 'list'
  else                                                                def materialsml
    render :action => 'new'                                                          @materials = Material.find(:all)
  end                                                                                render :layout => false
                                                                     end
−<channel>
 −<material>
      <title>wool</title>
      <description>2/20</description>
      <brand>Maine Line</brand>
      <color>red</color>
  </material>
 −<material>
      <title>Wool/silk</title>
      <description>2/28</description>
      <brand>Zephyr</brand>
      <color>sea blue</color>
   </material>
 −<material>
      <title>cotton</title>
      <description>3/2</description>
      <brand/>
      <color>yellow</color>
  </material>
 −<material>
      <title>alpaca</title>
      <description>sport</description>
      <brand>misti</brand>
      <color>brown</color>
  </material>
</channel>
    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 <description>
  – can include attibutes, too
     • xml.price(p.price, currency => “USD”) becomes
       <price currency=”USD”>50.00</price>.
       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.
             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
              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

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:8
posted:8/16/2012
language:
pages:19