Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
Python and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Lua, etc
 
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
  [4583] Back in the saddle again - excellent open source course from Well House Consultants - (2015-11-26)
  [4294] A bright new gem - updated Ruby training - (2014-09-16)
  [3799] Ruby Documentation through rdoc - (2012-07-07)
  [3158] Ruby training - some fresh examples for string handling applications - (2011-02-05)
  [2866] Ruby - how does it compare and where is it the right language? - (2010-07-11)
  [2826] Ruby - training for automated testing users - (2010-06-25)
  [2605] Ruby on Rails - a sample application to teach you how - (2010-01-30)
  [2504] Learning to program in ... - (2009-11-15)
  [2227] Learning PHP, Ruby, Lua and Python - upcoming courses - (2009-06-11)
  [2104] Ruby Programming and Rails - 4 different courses in one - (2009-03-26)

Ruby - Installing and setting up Rails
  [3772] Hello World - Ruby on Rails - a checklist of each step - (2012-06-22)
  [2610] Cheat Sheet - what do you need for Ruby on Rails? - (2010-01-31)

Ruby on Rails
  [4013] Web Frameworks - nested templates - (2013-02-22)
  [4010] Really Simple Rails - (2013-02-17)
  [3919] What is a web framework? - (2012-11-10)
  [3780] Ruby of Rails - cleanly displaying model data in the view - (2012-06-23)
  [3779] Adding validation to form entries and sticky fields - Ruby on Rails - (2012-06-23)
  [3778] Providing a form to allow the user to add data to the model - Ruby on Rails - (2012-06-23)
  [3777] Multiple views in a single appication - sharing common parts of the template - Ruby on Rails - (2012-06-23)
  [3772] Hello World - Ruby on Rails - a checklist of each step - (2012-06-22)
  [3756] Ruby on Rails - how it flows, and where the files go - (2012-06-08)
  [3624] Why do we need a Model, View, Controller architecture? - (2012-02-25)
  [2609] Scope of variables - important to Ruby on Rails - (2010-01-31)
  [2607] Answers on Ruby on Rails - (2010-01-30)
  [2605] Ruby on Rails - a sample application to teach you how - (2010-01-30)
  [1745] Moodle, Drupal, Django (and Rails) - (2008-08-08)
  [1375] Python v Ruby - (2007-10-02)
  [1302] Ruby, Ruby, Ruby. Rails, Rails, Rails. - (2007-08-13)
  [1050] The HTML++ Metalanguage - (2007-01-22)

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., 2019: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01225 708225 • FAX: 01225 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