Difference between revisions of "Workbook"

From mi-linux
Jump to navigationJump to search
Line 5: Line 5:
 
# Download the One-click ruby installer from rubyforge http://rubyforge.org/frs/?group_id=167  (warning this is a big file!). The one I used was ruby186-25.exe but there may be a later version available. To be able to run the whole environment from a USB stick I’d really recommend you have about 500MB free (maybe it’s time to buy a new stick?)
 
# Download the One-click ruby installer from rubyforge http://rubyforge.org/frs/?group_id=167  (warning this is a big file!). The one I used was ruby186-25.exe but there may be a later version available. To be able to run the whole environment from a USB stick I’d really recommend you have about 500MB free (maybe it’s time to buy a new stick?)
 
# Double-click on the installer, after clicking next a few times, a screen like the one shown below is displayed.
 
# Double-click on the installer, after clicking next a few times, a screen like the one shown below is displayed.
 +
  
 
[[Image:Workbook01.gif|center]]
 
[[Image:Workbook01.gif|center]]
 +
  
 
# Ensure you install on your USB stick, not the C drive (check the drive letter, as shown. Mine is J:, yours may vary)
 
# Ensure you install on your USB stick, not the C drive (check the drive letter, as shown. Mine is J:, yours may vary)

Revision as of 18:30, 6 February 2008

XML and Web Services >> Workbook

Ruby install on a USB stick

  1. Download the One-click ruby installer from rubyforge http://rubyforge.org/frs/?group_id=167 (warning this is a big file!). The one I used was ruby186-25.exe but there may be a later version available. To be able to run the whole environment from a USB stick I’d really recommend you have about 500MB free (maybe it’s time to buy a new stick?)
  2. Double-click on the installer, after clicking next a few times, a screen like the one shown below is displayed.


Workbook01.gif


  1. Ensure you install on your USB stick, not the C drive (check the drive letter, as shown. Mine is J:, yours may vary)
  2. Once Ruby is installed, open a cmd prompt (enter cmd into the ‘run’ box from the start menu) change to your ruby directory (e.g. j:\ruby ) and enter the follow command:
  3. gem install rails -y
  4. You may need to add the ruby directory to your system path, the setenv.bat file discussed in class will do this – the command is:
  5. set path=%PATH%;j:\ruby\bin;
    1. Remember to change the drive letter (j: in the example above) to match the letter assigned to your USB stick. This will probably be different for each system – it’s a windows foible – what a wonderful system :) (maybe you should try Linux? – ok Rant over.) This is why we suggested in the lecture that you put the command above in its own file, which we called setenv.bat, so that it’s easy to change.
  6. The gem command should install rails, see below for a database.

Exercises

Create one (or more) programs using ruby that can perform the following tasks:

  1. Create a program that can take a phrase as input and print it out in reverse.
  2. Create a program that can take a phrase as input and output it’s length as a number
  3. Create an application that can encrypt any phrase you enter using the rot13 algorithm
  4. Create an application that can take a temperature in F or C and convert to the other form

SQLite install on Windows

  1. Download the sqlite package from http://sqlite.org/download.html (you will need a file called sqlitedll-3_x_y.zip where x and y are numbers (they change, unfortunately). For reference I used sqlitedll-3_3_17.zip when I downloaded it. The download page is shown below:
  2. When you have downloaded the file, unzip it and extract it to a directory called sqlite on your usb stick (you may need to create this directory first.) You should then copy the sqlite3.dll file to your ruby\bin directory.
  3. Next use the gem command again to install the ruby-sqlite bindings so that ruby can use sqlite. The command is given below:
  4. gem install sqlite3-ruby (choose option 2 from the menu)
  5. Rails apps should be stored in their own directory – I tend to call it railsapps. Create this directory on your usb stick
  6. To test that rails is working properly type the following commands:
    1. cd railsapps
    2. rails mytest --database=sqlite3
    3. cd mytest
    4. ruby script/server
  7. Now start IE (ugh!?!?) or Firefox (yep!) and enter localhost:3000 into the URL bar, you should see the screen below:

MVC & Controllers

  1. Using the instructions given above, create a new application called myapp, connected to a sqlite database, and cd into the directory.
  2. Create a controller using ruby script/generate controller Say
  3. cd into the app\controllers directory and open the say_controller.rb skeleton.
  4. Add the hello class as shown below:
  5. Create a new view template in the app\views\say directory, called hello.rhtml
  6. Code is given below:
  7. Test the above, what do you see?
  8. Add a goodbye method to the say controller using the instructions above.
  9. Create a goodbye view to accompany the goodbye method, but change the message to “see you later”
  10. Modify the hello view to have a link to the goodbye view, example below
  11. What does the h helper do?
  12. Create a link from goodbye back to hello
  13. Investigate the formatting options/helpers available to make the time display more manageable.

Models and ORM

  1. This will walk through how to create a model, open a cmd prompt and enter – ruby script/generate model student – this will create the appropriate structure
  2. edit K:\Documents\railsapps\myapp\db\migrate\001_create_students.rb to include the following:
  3. To maintain the model (and application) we will create a static scaffold using rails generators as below – ruby script/generate scaffold student admin
  4. Start the application with ruby script/server, start a browser (firefox?) and navigate to http://localhost:3000/admin
  5. Add some data by clicking on ‘new student’ and filling in the boxes that are displayed – remember to click ‘create’ after each entry.
  6. What happens if you leave any field blank? Is this what you require?
  7. You can add validation by editing the ruby file that wraps the database – in this case student.rb – found at K:\Documents\railsapps\myapp\app\models\student.rb. Edit this file to read as below:
  8. Create a new entry, but leave the stud_no and course fields blank. What happens? Is this a useful function?
  9. Experiment with the validation helpers to see if you can ensure that the student number lies within a certain range.

Views & templates

  1. Create a controller called info using the ruby script command (see above). Edit the file to be as below:
  2. This code retrieves the student info from your database and stores it in a hash called students (the @ represents an instance variable, which keeps a classes state). This will allow us to use the data in a view.
  3. Next create a new view with the extension rxml – this tells rails it is a ‘builder’ class and it will automatically associate it with the builder library. Builder allows you to use ruby code to create xml files. Call the file stulist.rxml (to associate it with the controller we have already) and edit it to contain the following
  4. This will create an xml file as shown below (depending on the data stored):
  5. The next task is to create a form and utilise the data captured through it.