Rails |
A framework for developing web
applications. |
Ruby on Rails |
Synonym for Rails. |
Ruby |
The computer language used to
write Rails, and also the language you use to turn the Rails framework
into an application. Ruby is a loosely typed interpreter with a full
yet simple object model, and in my opinion is a very productive
computer language. |
Web application |
A computer program that
interfaces with the user through a web browser. |
Framework |
A ready made bunch of code and
code generators to perform the majority of a software program. It is
then up to the application developer to add the code that makes his
application unique. Such code is typically added in many different
spots throughout the framework. |
MVC |
Stands for Model, View
and Controller. Many web application frameworks, including
Ruby, partition their code into models, views and controllers. Doing so
makes it easier to change and scale the program. |
Model |
The part of the application that
interfaces to persistant data, whether that data is stored in a DBMS
(MySQL, Postgres, MS SQL Server, Oracle and the like), or as a flat
file on the local disk, or some other way. The persistent data is
accessed and validated by code in the model. There is typically one model for each database table, and one for each relevent flat file. |
View |
The part of the application that
paints screens. Ideally, code in the view paints the screen but does
nothing else. Lookups and calculations are done elsewhere, and the view
simply sends results of those lookups and calculations to the screen,
properly formatted. There is typically one view for each type of screen, although often one view is used for several similar but slightly different screens. For instance, screens for data insert, modification and deletion are all similar enough to be accomplished with one view using flags set by the controller. |
Controller |
The part of the application that
does what the model and view don't. Some people claim the controller
contains the "business rules". I consider that a little pompous. After
all, many applications are not intended to be used just for business.
Also, some business rules, such as "we don't accept anyone with a
credit score under 500" are typically implemented in a model as
validation routines. Every Rails application has at least one controller. There might be more, but usually not a large number. One way of splitting the work is to create a controller for each type of person using the system. For instance, there might be one controller called DataEntryPerson, another called Accountant, and a third called Administrator. |
DRY |
Stands for Don't Repeat Yourself.
This means have each piece of information in one place. This is a basic
part of the Ruby philosophy, and of course is also the philosophy
behind data normalization. |
AJAX |
Stands for Asynchronous JavaScript And XML. This technology enables a web page to communicate with the server and update parts of itself without refreshing the whole page, thereby saving bandwidth. |
Webrick | The web server that comes with
Rails. You run it with this command:script/serverIt can serve only a single application on a single port, so it's more useful for development and testing than for production. Luckily, other web servers can serve Rails pages in production. |
Apache |
The market leader in web servers. Apache can serve Rails pages if you're willing to put in some deployment work. |
InstantRails |
Ruby, Gems and Rails, with
production quality web server, in one bundle. Unfortunately, as of
1/18/2006 it's Windows only, but a Linux/Unix/BSD version is being
worked on. |
Locomotive |
A production quality Rails-capable web server, which unfortunately is Mac only. |
fastcgi |
A system whereby CGI (Common
Gateway Interface) programs stay in memory rather than being spawned as
individual process when requested. This makes for much better
efficiency. The lighttpd server comes with a fastcgi interface. |
lighttpd | Production quality, Rails-capable, Ruby-centric web server available for Linux/Unix/BSD. Requires fastcgi. See http://wiki.rubyonrails.com/rails/pages/Lighttpd and http://www.lighttpd.net/. |
RubyGems | A package manager for Ruby packages. Used to install Rails. |
scaffold | An autocoded chunk of code facilitating creation ofscreens to list out a data table, and to provide create, edit and delete facilities for a data table, based on the structure of that data table, which the scaffold generator reads and uses as a specification. You can use a few scaffolds to create a quick and dirty web app to show your client. |
session |
A hash like structure within
Rails apps to hold state between pages. It's a front end to cookies,
where the state info is really held. |
flash |
This is NOT Macromedia flash, and is nothing like Macromedia flash! In Rails, the term "flash" refers to a facility to pass temporary objects between actions. It's a module: ActionController::Flash. Whatever you place in flash will be exposed in the very next action, but then deleted, so you don't need to delete it manually (which is why it's better than the session for this type of thing). It's often used for error, warning and informational messages displayed on the screen after one the user has just filled out. |
gem install rails --include-dependenciesWhen the preceding command finishes, run the rails command to verify that Rails was installed. If you see a "help" or "usage" screen, you succeeded.
NOTE
If you need to redo the creation of an app, either press the "a" key when it asks about overwriting, or simply erase the application's directory (in this case hello ) and then rerun the rails hello command. |
hello |
Head of this application's tree |
/home/slitt/tcrails/helloFrom now on, every directory and file discussed will be relative to the application directory, unless otherwise specified.
ruby script/serverYou'll see several lines scroll by, and then the text will stop. If it gripes about Errno::EADDRINUSE , scrolls several lines and then terminates, the port the server attempted to use is probably already in use. In that case, try a different port. Make sure the other port is not used by anything else, and make sure using it doesn't constitute a security problem. I've used port 8818 in the past, and that worked for me. The server defaults to port 3000. To use a different port, do this:
ruby script/server -p 8818You can substitute any safe port for 8818. For the rest of this tutorial we'll assume you're using port 3000. If you aren't, just substitute your port number.
[slitt@mydesk hello]$ script/generate controller Greetings index |
<h1>Greetings#index</h1> |
http://localhost:3000/greetings/index | LEGEND: Application Controller View |
class GreetingsController < ApplicationController |
<head><title><%= @title %></title></head>
|
http://localhost:3000/greetings/index | LEGEND: Application Controller View |
ActionController::Routing::Routes.draw do |map| |
http://localhost:3000/rails-demo/greetings/indexAs mentioned many times, the /index on the end is optional because index is the default view.
map.connect '', :controller => "greetings"The preceding mapping would bring up the index view at this URL:
http://localhost:3000/However, that suffers from the problem that only the index view is accessible. To make all views of that single controller available at http://localhost:3000/, do this:
map.connect ':action', :controller => "greetings"However, what you would typically do is leave the routing at its initial mapping:
map.connect ':controller/:action/:id'Then you would remove the existing public/index.html page, and substitute a page that acts as a series of links to various controllers.
map.connect ':controller/:action/:id'There are still other mapping issues, such as hooking your site to an actual domain name, and getting rid of the visitor-intimidating port number. These are deployment issues that are discussed later in this document.
<a href="other_page.html">Click here</a> to see other page.That's nice, and it's easy, but it mixes up logic flow with presentation, which, as we all know, can lead to maintenance and scalability problems. This article describes the Rails way to perform linking.
[slitt@mydesk hello]$ script/generate controller Greetings page2 |
<head><title>This is the second page</title></head> |
<head><title><%= @title %></title></head> |
class GreetingsController < ApplicationController |
rails fibbNow edit app/controllers/fibb_controller.rb so it looks like this:
cd fibb
script/generate controller Fibb index
class FibbController < ApplicationController |
<h1>Fibb#index</h1> |
script/serverBrowse to http://localhost:3000/fibb and you'll see a screen something like this:
class FibbController < ApplicationController |
def zap_session |
<h1>Fibb#index</h1> |
rails hellodataThe preceding are the familiar commands to create an app and get to the head of its tree.
cd hellodata
development: |
#################################################### |
mysql -u root -p < load_dogs.sqlUse the mysql program to verify you can access the table as user myuid. Remember, you modified config/database.yml so that Rails uses user mysql to connect to the database, and that config/database.yml calls for a database named hellodata_development.
ruby script/generate scaffold Dog DoglistIn the preceding command, we use the generate script to create a scaffold, which is a controller and a series of views to manipulate data. Here's the explanation of the command:
ruby script/generate scaffold Dog Doglist |
|
ruby script/generate |
Rails' generate script |
scaffold |
You're generating a scaffold. A
scaffold is a very simple Add/Change/Delete set of views. |
Dog |
The name of the model you're
creating. Rails' default naming conventions dictate that the name of
the model is the singular of the the name of the data table, which is
why we called it Dog
instead of Dogs. The
scaffold generator sees the model named Dog, makes it plural, and looks
for a data table named dogs,
which it finds because you created it. BE SURE THE MODEL NAME IS SINGULAR! |
Doglist |
The name of the controller.
Generating a model also generates a controller. In this case we ask the
generator to call the model Doglist. |
vi -R app/controllers/doglist_controller.rb app/views/doglist/list.rhtml app/views/doglist/edit.rhtml app/views/doglist/new.rhtmlI like scaffolds. In less than 5 minutes you can generate a barebones app that works. From there, you can modify the controller, model and views to achieve exactly what you want, in small iterations, knowing you can drop back to the previous iteration if necessary. Even though the scaffold doesn't give you what you want, it gives you a working prototype in less than 5 minutes. Unless you've memorized all Rails syntax for controllers, models and views, this is five minutes very well spent.
rails ajaxYou'll be working with three files created in the preceding commands:
cd ajax
ruby script/generate controller Fibb index changepart
F1=<%= session[:fa1] %>, F2=<%= session[:fa2]%> |
<head> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|