WHAT IS RUBY ON RAILS?
It's a set of tools which enables you "quickly and easily" interface data held in a database to the web. In other words, it provides you with the ability to have visitors to your web site see your data and, under careful restrictions, modify it. And the tools also allow you to provide admin and other functions for database access, so that not only your web site visitor, by also your support desk, your order fulfillers, your administrators and even your manager can see whatever part of the information they need, and can make any authorised changes.
A wonderful theory - but did you note that I put quote marks around the words
quickly and easily. Ruby on Rails
is a very effective framework once you have it set up and running, and you (the programmer) understand it. But it ain't quick and easy when you first see it - it's one of those products that's balanced to being efficient to use rather than for being easy for first time coders / installers.
So here's a brief component guide to getting Rails running - that way you can see each of the elements you need and how they come together. My example is run on Linux ... but once you've got a full RoR up and running the underlying operating system isn't really relevant.
1. RUBY
The glue between the elements of Rails is the Ruby language. Rails is written in Ruby, and you'll also need it to define all your actions.
a) Download an appropriate version of Ruby (1.8.5 is the latest as I write)
b) Unpack
tar xzf ruby-1.8.5-p2.tar.gz
c) Build
cd ruby-1.8.5-p2
./configure
./make
d) Install
su -
[change back to build dir]
make install
exit
e) Ensure that the new version of Ruby is on your Path
2. GEMS
Gems is the Ruby extension software which allows to install and add other modules
a) Download an appropriate version of gems such as rubygems-0.9.0.tgz
b) Unpack
tar xzf rubygems-0.9.0.tgz
c) Install
su -
[change to unpacked dir]
ruby setup.rb
exit
3. RAILS
Rails itself which - now that you have gem installed - can easily be done for you.
As administrator, run
gem install rails --include-dependencies
At this point, you have RUBY and the RAILS framework installed; you may wish to go on and also install MySQL, and the Ruby / MySQL drivers via gems ... or you may wish to create your first Rails application. We've been doing a lot of installation so far, so we'll go this route and come back to the database when we've got something at least running.
4. APPLICATIONS
Create yourself an application:
[trainee@daffodil local]$ rails /home/trainee/railsdemo
create
create app/controllers
create app/helpers
create app/models
create app/views/layouts
create config/environments
create components
[etc]
create doc/README_FOR_APP
create log/server.log
create log/production.log
create log/development.log
create log/test.log
[trainee@daffodil local]$
5. WEB SERVER
Start the web application server (WEBrick ... but you can switch to Apache httpd / port 80 hosting later!)
[trainee@daffodil local]$ cd /home/trainee/railsdemo
[trainee@daffodil railsdemo]$ ruby script/server
=> Booting WEBrick...
=> Rails application started on http://0.0.0.0:3000
=> Ctrl-C to shutdown server; call with --help for options
[2006-12-15 07:24:13] INFO WEBrick 1.3.1
[2006-12-15 07:24:13] INFO ruby 1.8.5 (2006-12-04) [i686-linux]
[2006-12-15 07:24:13] INFO WEBrick::HTTPServer#start: pid=17559 port=3000
If you browse to port 3000, you'll find you're at the application's home page and this confirms the installation so far:
6. HELLO WORLD IN RAILS
Rails is an application server and we started off these notes with a "not easy to do the first time" notice - but wouldn't it be good to put in some sort of 'hello world' application?
Rails uses a Model, View, Controller model.
The VIEW is what we'll see - so wee need that even for 'Hello World'. It will access a CONTROLLER which we'll need as well, but as 'Hello World' won't have any data behind it, we can do without the MODEL at this stage.
a) Create the controller - there's a script to help us
[trainee@daffodil railsdemo]$ pwd
/home/trainee/railsdemo
[trainee@daffodil railsdemo]$ script/generate controller ilib
exists app/controllers/
exists app/helpers/
create app/views/ilib
exists test/functional/
create app/controllers/ilib_controller.rb
create test/functional/ilib_controller_test.rb
create app/helpers/ilib_helper.rb
[trainee@daffodil railsdemo]$
b) Create an action within that controller - this is a Ruby method to be added to the class called ilibController (ilib is our name choice), which extends an already supplied class called ApplicationController.
[trainee@daffodil railsdemo]$ vi app/controllers/ilib_controller.rb
And you enter:
class IlibController < ApplicationController
def greeting
end
end
You can see that the action exists by browsing to it ...
http://daffodil:3000/ilib/greeting
but you'll be told that the template is missing. That's fair enough as you haven't defined a view you, and you can check that the URL is seen as a valid one by seeing that you get a different message with a made-up URL.
c) Create the template ... which we'll make into a .rhtml (ruby embedded in HTML!) file even though we're not embedding any Ruby at this "Hello World" stage.
Into file app/views/ilib/greeting.rhtml write your template:
<html>
<head>
<title>Ruby on Rails - "Hello World" Template</title>
</head>
<body>
<h1>Ruby on Rails Rocks</h1>
<p>This is a first demonstration from Ruby on Rails -
a simple view from a controller that's got a single
do-nothing action defined ...</p>
<p>Well House Consultants, +44 (0) 1225 708225</p>
</body>
</html>
Refresh your previous page that had the "missing template" message and instead you should see your new application:
7. MAKING THE CONTENT DYNAMIC
The reason what we used a .rhtml extension is because we want to use embedded Ruby (or eruby or ERb) within our template. (There ARE other ways such as 'Builder', but this is an introduction checksheet!). So we can add tags as follows over and above the HTML standard, and they'll be caught by WEBrick before being sent out to the user.
<%= to %> - a Ruby expression to be evaluated with results substituted in the page <% to %> - Ruby code
a) Adding dynamic content within the view:
<%
#%% Some Ruby at the top of a view
dayofmonth = Time.new.day
%>
<html>
<head>
<title>Ruby on Rails</title>
</head>
<body>
<h1>Ruby on Rails Rocks</h1>
<p>The time it is rocking is <%= Time.new %></p>
<p>The day of the month is <%= dayofmonth %></p>
<p>Well House Consultants, +44 (0) 1225 708225</p>
</body>
</html>
And you'll see
This is good ... you *can* embed the <% to %> tags anywhere, but I'm going to recommend that you keep them well out of the way of the HTML itself - if you embed them deep in the template, that template becomes very hard to maintain and you should really start as you mean to go on.
In fact - why are you working with data in the VIEW - shouldn't it be supplied to view by the CONTROLLER?
b) Adding dynamic content from the controller:
You set up an instance variable within your controller class - the usual Ruby @ notation - and then you can reference it within the view. Yep, that easy!
class IlibController < ApplicationController
def greeting
@month = Time.new.month
end
end
and
<%
#%% Some Ruby at the top of a view
dayofmonth = Time.new.day
%>
<html>
<head>
<title>Ruby on Rails</title>
</head>
<body>
<h1>Ruby on Rails Rocks</h1>
<p>The time it is rocking is <%= Time.new %></p>
<p>The day of the month is <%= dayofmonth %></p>
<p>The month is <%= @month %></p>
<p>Well House Consultants, +44 (0) 1225 708225</p>
</body>
</html>
and see it run with data from the controller:
WHERE NOW?
Rails, or Ruby on Rails, is a huge subject ... but you've seen how in this introductory chapter you've installed it, configured a simple application with a controller method and a view. You're now ready to learn how to roar by
- Linking pages together
- Reading and processing form data
- running Sessions
- and of course, implementing model
See also
Learning Ruby - training classes