Documente Academic
Documente Profesional
Documente Cultură
1 1.1
1.2
Other aims
On the way, we will briey look at some aspects of the Ruby programming language. I will also briey mention the steps involved with deploying Rails using Apache. Finally, we will also look at REST, a technology for web services, and see how Rails automatically generates interfaces that are RESTful.
2 2.1
2.2
Another wikipedia page gives a list of 147 WAFs. The list of WAFs is divided into those using ASP, ASP.NET, ColdFusion, Java, JavaScript, Perl, PHP, Python and Ruby. I was keen to look at one based on a well-developed modern object-oriented programming language. For me, Java and Ruby are obvious programming languages to consider. And PHP and Perl are too primitive: they allow me easily to write bad code. I did quickly look at those WAFs using Java but there currently seems to be no frontrunner. Ruby on Rails (also known as Rails) is the WAF everyone keeps talking about.
2.3
Ruby
Ruby is an object-oriented programming language which is accompanied by a reasonably good library. I have one major criticism of Ruby. It ducks typing: typing of objects is dynamic. That does not appeal to me as in my opinion errors associated with the typing of objects should be caught at compile-time.
/rails/ictf08/handout.xml
2.4
RubyGems
A RubyGem is a way of forming a package of Ruby code that does a substantial piece of work in some area. Examples of RubyGems include: rake, rails, gnuplot, mongrel, mysql, tidy, xml-simple, payment, BlueCloth, feedtools and htmltools.
2.5
Rails
Rails is a web application framework that uses Ruby. There are two buzz-phrases associated with Rails: Dont Repeat Yourself (DRY ): Heres an example. If some data is going to be stored both in objects of a program and in a table of a database, give the description of the data once; Convention over Conguration (CoC): Heres an example. If the objects of a class called Phone are being stored in a table of a database, the table will have the name phones. Rails even knows some strange pluralizations such as Person- people. For a class with a name like RecentChanges, the table will, by default, have the name recent_changes.
3 3.1
3.2
Installing on Debian
I use a Debian package called ruby. This provides Ruby 1.8. I also use a Debian package for RubyGems (0.9.0/1.1.1). And then install the RubyGem for Rails (2.1.0). You can easily congure which database server software is to be used. Im using MySQL 5 (and so I install the Debian package mysql-server). There is more about installing on Debian in the document Rails HOW-TO: Installing Rails on Debian. This document is available from http://www.oucs.ox.ac.uk/rails/howtos
3.3
Installing on Windows
For Windows, you can install each bit separately (as was described for Debian). However, it is probably easier to use a software bundle such as Instant Rails. Ive used Instant Rails 2.0. This includes: Ruby 1.8.6 RubyGems 1.0.1 Rails 2.0.2 MySQL 5.0.27 Mongrel 1.1.2 Apache 1.3.33 phpMyAdmin 2.10.0.2 There is more about installing on Windows in the document Rails HOW-TO: Installing Rails on Windows. This document is available from http://www.oucs.ox.ac.uk/rails/howtos
/rails/ictf08/handout.xml
4 4.1
4.2
You use the rails command to create a new Rails application. By default, it creates an application that uses SQLite3. Use the -d option to use some other database server. The following command creates a directory called contacts and creates a lot of subdirectories/les in that directory. rails -d mysql contacts cd /var/apps/contacts ls
4.3
One subdirectory is called config. That directory contains a le called database.yml. This is the conguration for this applications access to the chosen database server. By default, it assumes you want three databases: one for development, one for testing and one for production. It also assumes your web application is going to contact the database server on the standard port (3306 for MySQL) using a username of root with an empty password. Because I want some different values, Im going to provide a different le.
4.4
/rails/ictf08/handout.xml
adapter: mysql port: 8116 encoding: utf8 database: contacts_test username: ruby password: PW4ruby production: adapter: mysql port: 8116 encoding: utf8 database: contacts_production username: ruby password: PW4ruby %
4.5
Having chosen to use a particular username and password, we also need to inform MySQL that these three databases are to be accessed in this way. mysql -P 8116 -u root -p mysql <<% grant all privileges on contacts_development.* \ to ruby@localhost identified by PW4ruby; grant all privileges on contacts_production.* \ to ruby@localhost identified by PW4ruby; grant all privileges on contacts_test.* \ to ruby@localhost identified by PW4ruby; flush privileges; \q % PW4root
4.6
The Rails software has a command called rake that behaves like Unixs make command or Windowss nmake. The following rake command gets MySQL to create the three databases. cd /var/apps/contacts rake db:create:all
Note: if I hadnt wanted to use a different port, username and password, we could have jumped from creating the Rails application to this stage.
4.7
Ruby comes with scripts that do useful things. One of these can be used to create a scaffold. A scaffold is an initial stab at all the les you need for a web application that manipulates data stored in some table of the database. We need to give the script the name of the class and the names and types of attributes of the class. This will also be used to create a table in the database. cd /var/apps/contacts ruby script/generate scaffold Phone \ name:string \ number:string
/rails/ictf08/handout.xml
I have chosen the type string for both attributes. Other possibilities for types are binary, boolean, date, datetime, decimal, float, integer, text, time, and timestamp.
4.8
Currently, we have an empty database. With Rails, you can change a database by performing a migration. One of the les the above command creates is a le with a name like 001_create_phones.rb (when using a version of Rails before Rails 2.1) or 20080615150932_create_phones.rb (when using Rails 2.1). Its in the directory db/migrate. cd /var/apps/contacts/db/migrate cat *_create_phones.rb
It can be used to add/remove the table to/from the database. Here is this migration le.
4.9
The migration le
class CreatePhones < ActiveRecord::Migration def self.up create_table :phones do |t| t.string :name t.string :number t.timestamps end end def self.down drop_table :phones end end
4.10
This command looks in the db/migrate directory to see if there are any les that have not been executed. It will execute the up methods of each of these in turn. In our example, it just nds 001_create_phones.rb or 20080615150932_create_phones.rb and it executes this les up method. If you were now to do the command rake db:migrate:down VERSION=20080615150932 it would execute the down method of this le to get the database back to the previous state.
4.11
Ruby comes with some web server software called WEBrick. It knows how to run Ruby applications. It needs to be started.
/rails/ictf08/handout.xml
cd /var/apps/contacts ruby script/server -p 8119 & # I got: # => Booting WEBrick... # => Rails application started on http://0.0.0.0:8119 # => Ctrl-C to shutdown server; call with --help for options # [2008-05-31 12:16:11] INFO WEBrick 1.3.1 # [2008-05-31 12:16:11] INFO ruby 1.8.5 (2006-08-25) [i486-1] # [2008-05-31 12:16:11] INFO WEBrick::HTTPServer#start: pid=28586 port=8119
4.12
Now we can go to the web pages provided by the scaffold. There is a web page to list the items of the table. http://www.abcd.ox.ac.uk:8119/phones
4.13
Suppose you just want a web application fronting one table of a database. Above, we saw that there are just ve steps: 1. Create the basic les of the application rails -d mysql contacts 2. Tell it to create the database for the app rake db:create:all 3. Tell it about the data you want to represent and it then builds a scaffold ruby script/generate scaffold Phone name:string number:string 4. Migrate the database (to include the new table) rake db:migrate 5. Start up the web server (WEBrick) and access the pages of the scaffold. We did all this without writing any code. Wow!
5 5.1
/rails/ictf08/handout.xml
5.2
At the top level of a Rails app, there is a directory called app. In this directory, there are subdirectories called models, controllers and views. With simple examples, there is little code in the models directory as the Model is described by the table of the database. The crucial le of the controllers directory has a name like phones_controller.rb This has one method for each action. Here is an example.
5.3
The nal part of the code of each method indicates what view to use (to show the result of the action). The request (for the action) will indicate the format of the response. It will often be HTML or XML.
5.4
The views has a subdirectory with a name like phones and in that directory there is one le for each view. For example, there is a le called index.html.erb that contains the code of the web page that just lists the items of the table.
5.5
A typical view
<h1>Listing phones</h1> <table> <tr> <th>Name</th> <th>Number</th> </tr> <% for phone in @phones %> <tr> <td><%=h phone.name %></td> <td><%=h phone.number %></td> <td><%= link_to Show, phone %></td> <td><%= link_to Edit, edit_phone_path(phone) %></td> <td><%= link_to Destroy, phone, :confirm => Are you sure?, :method => :delete %></td> </tr> <% end %> </table> <br /> <%= link_to New phone, new_phone_path %>
/rails/ictf08/handout.xml
6 6.1
6.2
We need to deal with the relationships between the tables. The tables involve three one-to-many relationships: an event refers to a course; a runner refers to an event; a runner refers to a person.
6.3
6.4
6.5
/rails/ictf08/handout.xml
end % cat app/models/event.rb cat >app/models/event.rb <<% class Event < ActiveRecord::Base belongs_to :course end %
6.6
The other change involves changing some of the views. We alter the web form where you choose an item of another table. Instead of typing an integer identifying which row of the other table ... ... you choose from a drop-down list.
6.7
http://www.abcd.ox.ac.uk:8119/events
6.8
More information
There is more about building this web app in the document Rails HOW-TO: A more involved Rails apps: jabs. This document is available from http://www.oucs.ox.ac.uk/rails/howtos
7 7.1
/rails/ictf08/handout.xml
7.2
It is easy to get a Rails app to ask for a username and a password. For security reasons, the Apache server should be congured to handle https connections. You then use the following code.
7.3
https://www.abcd.ox.ac.uk:8113/apps/contacts/phones https://www.abcd.ox.ac.uk:8113/apps/contacts/phones/new
7.4
More information
There is more about using Apache and passenger to deploy Rails apps and the use of Basic Authentication in the document Rails HOW-TO: Apache and Basic Authentication. There is information about how to get a Rails apps to work with WebAuth (the authentication system used at the University of Oxford) in the document Rails HOW-TO: Authenticating with WebAuth. Both these documents are available from http://www.oucs.ox.ac.uk/rails/howtos
8 8.1
8.2
The URLs and HTTP methods that we have been using to communicate with a Rails application are essentially RESTful. For example, if we click on the submit button of a form for a new entry of the phone book, it communicates the data using POST to the URL: https://www.abcd.oucs.ox.ac.uk:8113/apps/contacts/phones Ive been using a program called curl at a Unix command line to test this out. So Ive used curl to list items, to add an item, to delete an item, to update an item, ... . Date: 2008-06-05 (revised Tue, 15 Jul 2008) 10 /rails/ictf08/handout.xml
8.3
8.4
More information
There is more about using REST in the document Rails HOW-TO: Using REST. This document is available from http://www.oucs.ox.ac.uk/rails/howtos
9 9.1
9.2
Conclusions
With Rails, it is easy to build web applications that are supported by a database. The handout for the talk looks at some of the issues involved with building more involved web applications. Although all the code in the handout is given for Linux, similar commands can be used in Mac OS X or Windows. The handout looks at the steps involved with deploying Rails using Apache. We looked at REST, and saw how Rails automatically generates interfaces that are RESTful.
9.3
Over to you: its time for your questions and comments. ... ... ... The slides for this talk are available at http://www.oucs.ox.ac.uk/rails/ictf08 I found out about Ruby by immersing myself in a book. I recommend Programming Ruby by Dave Thomas (0-9745140-5-5). For Rails, the book I enjoyed reading was Agile Web Development with Rails: Second Edition by Dave Thomas and David Heinemeier Hansson (0-9776166-3-0). Unfortunately, this covers Rails 1.2: the third edition due out in October covers Rails 2.x.
/rails/ictf08/handout.xml
11