Learn Ruby On Rails in 4 Days (2005).pdf

(726 KB) Pobierz
Four Days on Rails
Four Days
on Rails
on Rails
compiled by John McCreesh
Four Days
708031153.001.png 708031153.002.png
Table of Contents
Introduction..................................................................................................................................................1
Day 1 on Rails...............................................................................................................................................3
The ‘To Do List’ application.............................................................................................................................................3
Running the Rails script..................................................................................................................................................... 3
Adding the Application to the Web Server.....................................................................................................................3
Defining the Application in the hosts file.................................................................................................................3
Defining the Application in the Apache Configuration file...................................................................................3
Switching to fastcgi........................................................................................................................................................3
Checking that Rails is working....................................................................................................................................4
Versions of Rails............................................................................................................................................................4
Setting up the Database......................................................................................................................................................4
Creating the Categories Table......................................................................................................................................4
MySQL definition....................................................................................................................................................4
Data Model...............................................................................................................................................................5
Scaffold..................................................................................................................................................................................5
Enhancing the Model..........................................................................................................................................................6
Creating Data Validation Rules...................................................................................................................................6
Day 2 on Rails...............................................................................................................................................9
The Generated Scaffold Code...........................................................................................................................................9
The Controller................................................................................................................................................................9
The View.......................................................................................................................................................................10
Layout......................................................................................................................................................................11
Template..................................................................................................................................................................11
Partial.......................................................................................................................................................................12
The Rendered View for the “New” action........................................................................................................13
Analysing the View for the ‘List’ action.............................................................................................................13
Tailoring the Generated Scaffold Code.........................................................................................................................15
The Controller..............................................................................................................................................................15
The View.......................................................................................................................................................................15
Displaying Flash Messages...................................................................................................................................15
Sharing Variables between the Template and Layout......................................................................................16
Tidying up the Edit and New Screens............................................................................................................... 17
Day 3 on Rails............................................................................................................................................. 19
The ‘Items’ Table...............................................................................................................................................................19
MySQL table defintion...............................................................................................................................................19
The Model.....................................................................................................................................................................19
Validating Links between Tables........................................................................................................................ 20
Validating User Input............................................................................................................................................20
The ‘Notes’ table...............................................................................................................................................................20
MySQL table defintion...............................................................................................................................................20
The Model.....................................................................................................................................................................20
Using a Model to maintain Referential Integrity..............................................................................................21
More Scaffolding...............................................................................................................................................................21
More on Views...................................................................................................................................................................21
Creating a Layout for the Application......................................................................................................................21
The ‘To Do List’ screen.............................................................................................................................................22
Purging completed ‘To Dos’ by clicking on an icon....................................................................................... 23
Changing the Sort Order by clicking on the Column Headings....................................................................24
Adding a Helper.....................................................................................................................................................24
Using Javascript Navigation Buttons.................................................................................................................25
Formatting a Table with a Partial........................................................................................................................25
Formatting based on Data Values......................................................................................................................26
Handling Missing Values in a Lookup...............................................................................................................26
The ‘New To Do’ Screen...........................................................................................................................................26
Creating a Drop-down List for a Date Field.....................................................................................................27
Trapping Exceptions in Ruby..............................................................................................................................27
Creating a Drop-down List from a Lookup Table.......................................................................................... 28
Creating a Drop-down List from a List of Constants.....................................................................................28
Creating a Checkbox.............................................................................................................................................28
Finishing Touches.............................................................................................................................................................28
Tailoring the Stylesheet...............................................................................................................................................28
The ‘Edit To Do’ Screen............................................................................................................................................29
Day 4 on Rails............................................................................................................................................. 31
The ‘Notes’ screens...........................................................................................................................................................31
Linking ‘Notes’ to the ‘Edit To Do’.........................................................................................................................31
The ‘Edit Notes’ Screen.............................................................................................................................................32
The ‘New Note’ Screen..............................................................................................................................................32
Saving and retrieving Data using Session Variables.........................................................................................33
Changing the ‘Categories’ Screens..................................................................................................................................33
Navigation through the system.......................................................................................................................................34
Setting the Home Page for the Application............................................................................................................35
Downloading a Copy of this Application......................................................................................................................35
and finally............................................................................................................................................................................35
Appendix – afterthoughts...........................................................................................................................37
Multiple Updates...............................................................................................................................................................37
View...............................................................................................................................................................................37
Controller......................................................................................................................................................................38
User Interface considerations....................................................................................................................................39
Still to be done...................................................................................................................................................................39
Introduction
There have been many extravagant claims made about Rails. For example, an article in OnLAMP.com 1 claimed
that “you could develop a web application at least ten times faster with Rails than you could with a typical Java
framework...” The article then went on to show how to install Rails and Ruby on a PC and build a working
‘scaffold’ application with virtually no coding.
While this is impressive, ‘real’ web developers know that this is smoke and mirrors. ‘Real’ applications aren’t as
simple as that. What’s actually going on beneath the surface? How hard is it to go on and build ‘real’
applications?
This is where life gets a little tricky. Rails is well documented on-line – in fact, possibly too well documented for
beginners, with over 30,000 words of on-line documentation in the format of a reference manual. What’s
missing is a roadmap (railmap?) pointing to the key pages that you need to know to get up and running in Rails
development.
This document sets out to fill that gap. It assumes you’ve got Ruby and Rails up on a PC (if you haven’t got this
far, go back and follow Curt’s article). This takes you to the end of ‘Day 1 on Rails’.
‘Day 2 on Rails’ starts getting behind the smoke and mirrors. It takes you through the ‘scaffold’ code. New
features are highlighted in bold , explained in the text, and followed by a reference to either Rails or Ruby
documentation where you can learn more.
‘Day 3 on Rails’ takes the scaffold and starts to build something recognisable as a ‘real’ application. All the time,
you are building up your tool box of Rails goodies. Most important of all, you should also be feeling comfortable
with the on-line documentation so you can continue your explorations by yourself.
‘Day 4 on Rails’ adds in another table and deals with some of the complexities of maintaining relational integrity.
At the end, you’ll have a working application, enough tools to get you started, and the knowledge of where to
look for more help.
Ten times faster? after four days on Rails, judge for yourself!
Documentation : this document contains highlighted references, either to:
Documentation – the Rails documentation at http://api.rubyonrails.com (this documentation is also installed
on your PC as part of your gems installation in a location like C:\Program
Files\ruby\lib\ruby\gems\n.n\doc\actionpack-n.n.n\rdoc\index.html )
Ruby Documentation – “Programming Ruby - The Pragmatic Programmer's Guide” available online and for
Acknowledgements : many thanks to the helpful people on the the irc channel 2 and the mailing list 3 . The on-
line archives record their invaluable assistance as I clawed my way up the Rails and Ruby leaning curves.
Version: 2.3 using version 0.12.1 of Rails – see http://rails.homelinux.org for latest version and to download a
copy of the ToDo code. Document written and pdf file generated with OpenOffice.org 'Writer'.
Copyright : this work is copyright ©2005 John McCreesh jpmcc@users.sourceforge.net and is licensed under
the Creative Commons Attribution-NonCommercial-ShareAlike License . To view a copy of this license, visit
http://creativecommons.org/licenses/by-nc-sa/2.0/ or send a letter to Creative Commons, 559 Nathan Abbott
Way, Stanford, California 94305, USA.
Page 1
708031153.003.png 708031153.004.png
Zgłoś jeśli naruszono regulamin