Ruby on Rails - a sample application to teach you how
Archive - Originally posted on "The Horse's Mouth" - 2010-01-30 19:37:18 - Graham EllisThe 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




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
create app/controllers
create app/helpers
create app/modelsi
[etc]
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
[etc]
[trainee@iford shop]$ script/generate model product aisle_id:integer pname:string unitprice:decimal stocklevel:integer
exists app/models/
exists test/unit/
[etc]
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
[etc]
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 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
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 |