Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Ror - Ruby on Rails

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

Please note that articles in this section of our web site were current and correct to the best of our ability when published, but by the nature of our business may go out of date quite quickly. The quoting of a price, contract term or any other information in this area of our website is NOT an offer to supply now on those terms - please check back via our main web site

Related Material

Ruby - General
  [2104] - ()
  [2227] - ()
  [2504] - ()
  [2605] - ()
  [2826] - ()
  [2866] - ()
  [3158] - ()
  [3799] - ()
  [4294] - ()
  [4583] - ()

Ruby - Installing and setting up Rails
  [2610] - ()
  [3772] - ()

Ruby on Rails
  [1050] - ()
  [1302] - ()
  [1375] - ()
  [1745] - ()
  [2605] - ()
  [2607] - ()
  [2609] - ()
  [3624] - ()
  [3756] - ()
  [3772] - ()
  [3777] - ()
  [3778] - ()
  [3779] - ()
  [3780] - ()
  [3919] - ()
  [4010] - ()
  [4013] - ()

resource index - Ruby
Solutions centre home page

You'll find shorter technical items at The Horse's Mouth and delegate's questions answered at the Opentalk forum.

At Well House Consultants, we provide training courses on subjects such as Ruby, Lua, Perl, Python, Linux, C, C++, Tcl/Tk, Tomcat, PHP and MySQL. We're asked (and answer) many questions, and answers to those which are of general interest are published in this area of our site.

Comment: "the way of explanation is very good ..."
Visitor Ranking 5.0 (5=excellent, 1=poor)

Comment by gagan (published 2010-08-03)
the way of explanation is very good [#3660]

You can Add a comment or ranking or edit your own comments

Average page ranking - 5.0

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/solutions/ruby-ror ... rails.html • PAGE BUILT: Wed Mar 28 07:47:11 2012 • BUILD SYSTEM: wizard