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:
The configuration file for the products and aisles databases.
The file to seed those databases.
The file which defines how to validate an aisle record
The file which defines hor to validate a product record
(These are the "model" files of the "Model - view - controller" architecture)
The file which defines what the home page looks like
Definition of how the "what's in stock" page looks
The look of the "product add" page
... 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:
the template file that encloses all views and gives a uniform look and feel to each view within a ruby on rails application.
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 ...
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
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
[trainee@iford shop]$ script/generate model product aisle_id:integer pname:string unitprice:decimal stocklevel:integer
And to set up the controller:
[trainee@iford shop]$ script/generate controller Floor index stock
To create the tables:
[trainee@iford shop]$ rake db:migrate
== CreateAisles: migrating ===================================================
== CreateAisles: migrated (0.0032s) ==========================================
== CreateProducts: migrating =================================================
== CreateProducts: migrated (0.0036s) ========================================
And to start the server:
[trainee@iford shop]$ ruby script/server
=> Booting WEBrick
=> Rails 2.3.5 application starting on http://0.0.0.0:3000
=> 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
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.
(written 2010-01-30, updated 2012-06-23)
| 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
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articlesR050 - Ruby - General 
Back in the saddle again - excellent open source course from Well House Consultants - (2015-11-26) 
A bright new gem - updated Ruby training - (2014-09-16) 
Ruby Documentation through rdoc - (2012-07-07) 
Ruby training - some fresh examples for string handling applications - (2011-02-05) 
Ruby - how does it compare and where is it the right language? - (2010-07-11) 
Ruby - training for automated testing users - (2010-06-25) 
Learning to program in ... - (2009-11-15) 
Learning PHP, Ruby, Lua and Python - upcoming courses - (2009-06-11) 
Ruby Programming and Rails - 4 different courses in one - (2009-03-26)R114 - Ruby on the Web 
Reading and parsing a JSON object in Ruby - (2015-06-01) 
Web and console - same principle, same code - Ruby example - (2013-02-14) 
Ruby on the web - a simple example using CGI - (2012-06-22) 
Some TestWise examples - helping use Ruby code to check your web site operation - (2012-02-24) 
3 digit HTTP status codes - what are they, which are most common, which should be a concern? - (2011-09-11) 
Ruby at both extremes of your website - (2011-09-10) 
Answers on Ruby on Rails - (2010-01-30) 
Ruby to access web services - (2008-11-16)R202 - Ruby on Rails 
Web Frameworks - nested templates - (2013-02-22) 
Really Simple Rails - (2013-02-17) 
What is a web framework? - (2012-11-10) 
Ruby of Rails - cleanly displaying model data in the view - (2012-06-23) 
Adding validation to form entries and sticky fields - Ruby on Rails - (2012-06-23) 
Providing a form to allow the user to add data to the model - Ruby on Rails - (2012-06-23) 
Multiple views in a single appication - sharing common parts of the template - Ruby on Rails - (2012-06-23) 
Hello World - Ruby on Rails - a checklist of each step - (2012-06-22) 
Ruby on Rails - how it flows, and where the files go - (2012-06-08) 
Why do we need a Model, View, Controller architecture? - (2012-02-25) 
Scope of variables - important to Ruby on Rails - (2010-01-31) 
Moodle, Drupal, Django (and Rails) - (2008-08-08) 
Python v Ruby - (2007-10-02) 
Ruby, Ruby, Ruby. Rails, Rails, Rails. - (2007-08-13) 
The HTML++ Metalanguage - (2007-01-22)
Some other Articles
Search and replace in Ruby - Ruby Regular ExpressionsSorting arrays and hashes in RubyRuby on Rails - a sample application to teach you howTips for writing a test program (Ruby / Python / Java)Ruby objects - a primerEast of Melksham - Building Work StartsRuby - is_a? v instance_of? - what is the difference?Go Programming Language and Courses?