Rails is a "Web Framwork" that uses code written in the Ruby language to do the things that vary from one web application to another. There are a huge number of features common to many web applications, and by using a framework you can save yourself the trouble of rewriting all these common things, using instead what's provided by Rails. It makes for quicker coding, a well thought out structure from the beginning, but (conversely) a whole lot of hooks and special names and terminologies that will leave the newcomer gasping for breath at first.
A very first demonstration of how the elements go together - see my previous blog
[here] - about how examples can get overcomplicated before they're published.
I don't pretend that this is useful in production, or clever, or even well structured - but it does show you how values from your model reach your view.
Background ... a new Rails application, installed and built using the
Rails Installation Cheat Sheet I wrote a while back. That cheat sheet sets you up with a running new application that does nothing on rails.
Then set up
a) Application called "serverstats"
b) Controller called "summarise"
c) Model called "access"
rails serverstats
cd serverstats
script/generate controller summarise
script/generate model access ip:string pagename:string
Now - what URL do I use to browse to that? Answer: /summarise/ (and it will tell you there is no route). So I want to set up a controller and a view. See
[here] for the directorys / files needed - and you only need the controller
app/controllers/summarise_controller.rb
and the view
app/views/summarise/index.erb
to start you off (in fact the index method in the controller can be empty at first, and the view template can contain just HTML).
The example code goes on to show you how you can add each of the following to your view
a) The standard (static) view
b) Data calculated in the view (you shouldn't do much of this)
c) Data to be displayed / calculated by the controller
d) Class data and application level information from a model
e) Database information provided through the model
For the database section to work, you'll want to add some seed values (see sample file) and you'll then need to:
rake db:migrate
rake db:seed
to set up and provide some initial test values in your database. Rails will default to SQLite.
Rails varies a bit from version 2 to 3 ... and at the time of writing, 4's on its way. At the end of our
Ruby Programming Course we offer an optional
Introduction to Ruby on Rails which will help you with your first steps into this framework, as in the example above.
(written 2013-02-17)
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)
[2605] Ruby on Rails - a sample application to teach you how - (2010-01-30)
[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)
[4013] Web Frameworks - nested templates - (2013-02-22)
Q915 - Object Orientation and General technical topics - Principles of Model - View - Controller [687] Presentation, Business and Persistence layers in Perl and PHP - (2006-04-17)
[2199] Improving the structure of your early PHP programs - (2009-05-25)
[2612] The Model, View, Controller architecture (MVC) - what, why and how. - (2010-02-01)
[3237] Using functions to keep look and feel apart from calculations - simple C example - (2011-04-09)
[3454] Your PHP website - how to factor and refactor to reduce growing pains - (2011-09-24)
[3705] Django Training Courses - UK - (2012-04-23)
[4066] MVC and Frameworks - a lesson from first principles in PHP - (2013-04-19)
[4114] Teaching CodeIgniter - MVC and PHP - (2013-06-12)
[4320] An example of Model-View-Controller techniques in a Perl / CGI script - (2014-11-20)
[4391] Refactoring Perl applications to give them a rosy future - (2015-01-11)
[4527] Hello Flask world / Python web micro framework - (2015-10-11)
[4641] Using an MVC structure - even without a formal framework - (2016-02-07)
[4691] Real life PHP application using our course training MVC example - (2016-06-05)
Some other Articles
Costa Coffee in Melksham - is it a good idea?A course is not just for a year - its for a careerFrom SalfordReally Simple RailsClear, concise examples - Ruby classes and objects.Reading and checking user inputs - first lessons - RubyWhich database should I use? MySQL v SQLiteRuby / SQLite3 example program, showing JOIN v LEFT JOINNew guest ... becoming returning guest