Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
For 2021 - online Python 3 training - see ((here)).

Our plans were to retire in summer 2020 and see the world, but Coronavirus has lead us into a lot of lockdown programming in Python 3 and PHP 7.
We can now offer tailored online training - small groups, real tutors - works really well for groups of 4 to 14 delegates. Anywhere in the world; course language English.

Please ask about private 'maintenance' training for Python 2, Tcl, Perl, PHP, Lua, etc.
Ruby on Rails - a sample application to teach you how

The Ruby on Rails Framework is a great way to put bolt web applications together quickly - but it can be overwhelming when you come to do it for the first time. There's a steep learning curve with all the various elements to put together to make up the whole.

We have a first (tiny!) demonstration in our "solutions centre" - see [here] - which in effect is "hello world" on rails. And that's a useful starting point if you're running on a server that doesn't yet have Ruby installed, or if it does have Ruby, doesn't yet have Rails. But "Hello World" doesn't use databases ...

Last week, I ran a Ruby Programming Course, which I extended to cover Ruby on Rails, which we have been working on today. We built up a simple application with two database tables that are joined to each other, with the ability to display the data in two different ways. We added an ability to enter new data for the products that a shop sells (that's one of our tables) and the aisles on which they display them (that's the other table). The application includes - even in this demonstration - data checking and validation.

What the sample application looks like

Let me show you some of the pages of the application so that you get an idea of what we achived in this short time. The first page is the home page of the application, and at this early stage we chose to display a listing of all the aisles, and a listing of all the products.

Here's an alternative view - showing the various items in stock in each aisle. The beauty of Ruby on Rails is that all you have to do is to define a new view on the data and the rest of the logic (the common code) remains unchanged - again making for faster development and easier maintainance.

When I clicked on a link to add a product to an aisle, I was given another view which included a form into which the new data could be entered. You see in this illustration the result of submitting that form with errors (I hadn't given a price, nor a stock quantity, but I had told Ruby on Rails that both were mandatory numeric fields). And the Ruby on Rails Framework has returned the page to me, with the data that I *had* included still there, and asked me to make corrections. When I did so ...

The corrected data was added to the database tables, and then the home page of the application (re)presented with the additional product - 14 Tiger Loaves in stock at 3.20 each - included in the list of what we have for sale, and even sorted (as we have specified in the application) in product price order.

What files are used for the sample application

This is very much a training example. I did not include a layer of HTML formatting and style sheets (though we did add a golden background and a few other elements to "show how to") ... and that means that you can see my files and if you've already done some Rails, you can work out what I have done. The complete application is downloadable from [here - a .tgz file], and you can also see the individual source files if you wish ... they're as follows:

[link] The configuration file for the products and aisles databases.
[link] The file to seed those databases.

[link] The file which defines how to validate an aisle record
[link] The file which defines hor to validate a product record
(These are the "model" files of the "Model - view - controller" architecture)

[link] The file which defines what the home page looks like
[link] Definition of how the "what's in stock" page looks
[link] The look of the "product add" page
[link] ... and the look of the "aisle add" page
(These are the "view" files of the "Model, View, Controller"). There is also a further file in the view:
[link] the template file that encloses all views and gives a uniform look and feel to each view within a ruby on rails application.

[link] There is a small (in our case) file of code that's shared between all the controllers (as we have just the one controller in this example, we didn't need to use it) and ...
[link] The Controller for the application. This is the key file in the application, tying the model to the views (and I have commented the methods)

How the sample application is started

Some of the other files can be generated through scripts - they're included in my download if you've used that, but if you're setting up a ruby on rails applications yourself, youl'll probably want to run the helper scripts yourself.

To create the application (named "shop"):

[trainee@iford rails]$ rails shop
create app/controllers
create app/helpers
create app/modelsi

To create the ailse and product models within the shop (cd to it first!):

[trainee@iford shop]$ script/generate model aisle aname:string restricted:boolean
exists app/models/
exists test/unit/
exists test/fixtures/
create app/models/aisle.rb
create test/unit/aisle_test.rb
[trainee@iford shop]$ script/generate model product aisle_id:integer pname:string unitprice:decimal stocklevel:integer
exists app/models/
exists test/unit/

And to set up the controller:

[trainee@iford shop]$ script/generate controller Floor index stock
exists app/controllers/
exists app/helpers/
create app/views/floor

To create the tables:

[trainee@iford shop]$ rake db:migrate
(in /home/trainee/rails/shop)
== CreateAisles: migrating ===================================================
-- create_table(:aisles)
-> 0.0030s
== CreateAisles: migrated (0.0032s) ==========================================
== CreateProducts: migrating =================================================
-- create_table(:products)
-> 0.0034s
== CreateProducts: migrated (0.0036s) ========================================

And to start the server:

[trainee@iford shop]$ ruby script/server
=> Booting WEBrick
=> Rails 2.3.5 application starting on
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2010-01-29 15:45:29] INFO WEBrick 1.3.1
[2010-01-29 15:45:29] INFO ruby 1.8.6 (2009-08-04) [i386-linux]
[2010-01-29 15:45:29] INFO WEBrick::HTTPServer#start: pid=11483 port=3000

This is agile programming, where you'll want to reset and restart your application many times while it's in test - so I'll also give you the magic lines to do that. Stop your server, then:

rake db:migrate VERSION=0
rake db:migrate
rake db:seed

and restart the server.

Summary and Conclusions

Ruby on Rails is a great tool for bolting together a maintainable, database driven web site very quickly - and it's database and server portable too. We were using SQLite and WEBrick, but for production you would probably use MySQL and almost certainly run behind and Apache httpd server (there are various modules to help you).

However - there's a great deal of knowledge needed ahead of time - that's the nature of the technology more that Rails itself ... you need to know a little HTML, some Ruby, and have some understanding of databases before you get on to the Rails framework itself. Once you have that knowledge, you'll love it.

You'll admire the clever way the model view and controller are separated. You'll rejoice at the standard, provided helpers which mean that so many things like sticky fields, validation, look and feel and security are done for you. And you can't help but be impressed how Ruby is used as both an embedded language withing the views (PHP like, in .erb files) and at the same time in stand along classes for the model and controller. The very fact that Ruby is Object based gives you a huge flexibility and power as you integrate from a tiny to a medium sized application, ensuring that growth pains are minimised ...

... all of which is just TOO MUCH to put into a single article here. The best way to get started with rails is to take some data and write a sample application with someone.. If you've never programmed before, come on our Learning to program in Ruby training course, or if you have programmed before book onto our Ruby Programming Course. And ask to stay for an extra day (cost £350.00) and I'll run a Ruby on Rails course extension even if it's just for you. You'll leave after your course not only understanding how the elements go together, but also with practical experience of doing it yourself.

This is one of a series of summaries taking you from initial installation of Ruby and Rails through to a complete multitable application:
[link] - Installing Ruby and Rails
[link] - Hello World, Ruby on Rails Style
[link] - What and Why - Model, View, Controller
[link] - Multiple views, same model
[link] - A form to allow data to be added to the model
[link] - Validating data entries for storage in the model
[link] - Cleanly viewing model data
[link] - Complete sample, including a multiple table model
These topics are covered on our Introduction to Rails day - an optional extension of Ruby Programming or Learning to program in Ruby

(written 2010-01-30, updated 2012-06-23)

Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
R202 - Ruby on Rails
  [1050] The HTML++ Metalanguage - (2007-01-22)
  [1302] Ruby, Ruby, Ruby. Rails, Rails, Rails. - (2007-08-13)
  [1375] Python v Ruby - (2007-10-02)
  [1745] Moodle, Drupal, Django (and Rails) - (2008-08-08)
  [2607] Answers on Ruby on Rails - (2010-01-30)
  [2609] Scope of variables - important to Ruby on Rails - (2010-01-31)
  [3624] Why do we need a Model, View, Controller architecture? - (2012-02-25)
  [3756] Ruby on Rails - how it flows, and where the files go - (2012-06-08)
  [3772] Hello World - Ruby on Rails - a checklist of each step - (2012-06-22)
  [3777] Multiple views in a single appication - sharing common parts of the template - 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)
  [3779] Adding validation to form entries and sticky fields - Ruby on Rails - (2012-06-23)
  [3780] Ruby of Rails - cleanly displaying model data in the view - (2012-06-23)
  [3919] What is a web framework? - (2012-11-10)
  [4010] Really Simple Rails - (2013-02-17)
  [4013] Web Frameworks - nested templates - (2013-02-22)

R114 - Ruby on the Web
  [1891] Ruby to access web services - (2008-11-16)
  [3431] Ruby at both extremes of your website - (2011-09-10)
  [3432] 3 digit HTTP status codes - what are they, which are most common, which should be a concern? - (2011-09-11)
  [3623] Some TestWise examples - helping use Ruby code to check your web site operation - (2012-02-24)
  [3773] Ruby on the web - a simple example using CGI - (2012-06-22)
  [4003] Web and console - same principle, same code - Ruby example - (2013-02-14)
  [4502] Reading and parsing a JSON object in Ruby - (2015-06-01)

R050 - Ruby - General
  [2104] Ruby Programming and Rails - 4 different courses in one - (2009-03-26)
  [2227] Learning PHP, Ruby, Lua and Python - upcoming courses - (2009-06-11)
  [2504] Learning to program in ... - (2009-11-15)
  [2826] Ruby - training for automated testing users - (2010-06-25)
  [2866] Ruby - how does it compare and where is it the right language? - (2010-07-11)
  [3158] Ruby training - some fresh examples for string handling applications - (2011-02-05)
  [3799] Ruby Documentation through rdoc - (2012-07-07)
  [4294] A bright new gem - updated Ruby training - (2014-09-16)
  [4583] Back in the saddle again - excellent open source course from Well House Consultants - (2015-11-26)

Back to
Tips for writing a test program (Ruby / Python / Java)
Previous and next
Horse's mouth home
Forward to
Sorting arrays and hashes in Ruby
Some other Articles
Search and replace in Ruby - Ruby Regular Expressions
Sorting arrays and hashes in Ruby
Ruby on Rails - a sample application to teach you how
Tips for writing a test program (Ruby / Python / Java)
Ruby objects - a primer
East of Melksham - Building Work Starts
Ruby - is_a? v instance_of? - what is the difference?
Go Programming Language and Courses?
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page

This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

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

PAGE: http://www.wellho.net/mouth/2605_Rub ... u-how.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb