Home Accessibility Courses Twitter The Mouth Facebook 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
 
Hello World - Ruby on Rails - a checklist of each step

Ruby on Rails is a framework under which web applications written in Ruby can be run on a web server, via a browser. You could make your own applications from first principles, but Rails does most of this for you already, so it's typically going to be a huge resource saving for you as you write and maintain the application. However, that comes at an initial cost of having to do a bit more learning than you would require with a simple "CGI" application.

Yesterday we set up a "Hello World" example using Rails ... notes here, sample files on our server. I'm publishing this in the form of a checklist rather than a long explanation of each stage, to help you see the wood for the trees. Longer explanations, of course, on our Introduction to Rails day.

1. Hello World in Ruby on Rails

• Install Ruby
• Install Rails
(see http://www.wellho.net/solutions/ruby-ror-ruby-on-rails.html)
(That's Linux / Unix - I'm not a Windows Geek)

Pre-requisite knowledge: Ruby (see http://www.wellho.net/course/rpfull.html>[here]) and a little HTML.

And then ;-) ...

start in your home directory.

  rails sales {create the framework for your application}
  cd sales
  script/generate controller productlister {create controller framework}


edit app/controllers/productlister_controller.rb {define a controller}
to contain

  class ProductlisterController < ApplicationController
  def all
    end
  end


edit app/views/productlister/index.erb {define a view}
to contain

  <h1>Hello World</h1>

Run

  script/server -p 3053 -b 192.168.200.232 {Run the test server}

browse to http://192.168.200.232:3053/productlister/ {try it out}

• No Model
• No dynamics
• No forms
• not much really ;-)

But it should say "Hello World"

2. Add some dynamics

In the view at the top(ish):

  <%
  igot = ["bread","butter","marmite","cheese","onion","butter","bread"]
  sandwich = igot.join(" then ")
  %>


and where you want to to_s to be displayedin the html:

  <%= sandwich %>

And run server and browse.

You will see data generated by Ruby within the VIEW

3. From the Controller

In the view:

  <%= @main_result %>

In the Controller:

  def index
    @main_result = "Gypsy recomends the Squirrel Pie"
  end


And run server and browse.

You will see data generated by Ruby within the CONTROLLER

4. Set up and seed the database:

  script/generate model product pname:string unitprice:decimal stocklevel:integer

Edit the seed file db/seeds.rb to add

  Product.create(:pname => "Cream Tea", :unitprice => 5.00, :stocklevel => 10)
  Product.create(:pname => "Coffee", :unitprice => 1.5, :stocklevel => 8)
  Product.create(:pname => "Tea", :unitprice => 1.25, :stocklevel => 14)


Then run

  rake db:migrate VERSION=0 {un-neccasary first time - clear out old stuff)
  rake db:migrate {set up database tables}
  rake db:seed {seed them with intial values as just above


Now check that the data is in the database which has defaulted to SQLite

  cd db
  sqlite3 development.sqlite3
  sqlite> select * from products;
  1|Cream Tea|5|10|2012-06-21 13:44:26|2012-06-21 13:44:26
  2|Coffee|1.5|8|2012-06-21 13:44:26|2012-06-21 13:44:26
  3|Tea|1.25|14|2012-06-21 13:44:26|2012-06-21 13:44:26
  sqlite>


This step has purely set up the database. You will not see anything different if you browse to the page

5. Project the database records through the controller into the view

Add to the controller (the index method therein):

  stockdata = Product.all
  @info = ""
  for stock in stockdata
    @info += stock.pname + " ... "
  end


Add to the view

Pulled back from the Model: <b><%= @info %></b><br /><br />

Run the server:

  script/server -p 3053 -b 192.168.200.232 {Run the test server}

browse to http://192.168.200.232:3053/productlister/all {try it out}

You should see:



That includes the data generated by Ruby within the MODEL

6. Where are we now?

• We have installed Rails

• We have generated a view, a controller and a model
• We have seeded out model with data
• We have visited a URL that has displayed model (and controller and view) data

What have we NOT done?

• Ensured that data from the model is "clean" when going to the view / browser
• Tabulated and formatted model data as it goes to the browser
• Shared a template or parts of a template between different URLs
• Shared business logic between multiple controllers
• Provided a navigation route between multiple output pages

• Provided forms into which the user can enter values which the (next) controller will process
• Provided a controller which receives and validates form inputs
• Provide "sticky" fields on forms
• Had our controller modify the data stored within the model

• Connected multiple pages from the same user together, carrying on his context
(a.k.a. session data!)

• Written an application that uses multiple tables that are connected

Many of these are covered in another example / article [here].




Source files for this example are in Installing and setting up Rails resources:
View: index.erb
Controller: productlister_controller.rb
Model: product.rb (unchanged so far after our generate)
and the database see file: seeds.rb

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 2012-06-22, updated 2012-06-23)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
R202 - 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)
  [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)

R203 - Ruby - Installing and setting up Rails
  [2610] Cheat Sheet - what do you need for Ruby on Rails? - (2010-01-31)


Back to
Fine evening, country walk from Melksham - pictures
Previous and next
or
Horse's mouth home
Forward to
Ruby on the web - a simple example using CGI
Some other Articles
Some traps it's so easy to fall into in designing your web site
Alan Turing - 1912 to 1954
Melksham - a new dawn
Ruby on the web - a simple example using CGI
Hello World - Ruby on Rails - a checklist of each step
Fine evening, country walk from Melksham - pictures
Sample answers to training course exercises - available on our web site
Muttable v immutable and implications - Ruby
Melksham Chamber of Commerce - looking to our future shape. Pivotal meeting next Tuesday
How well do you know Melksham?
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., 2019: 404 The Spa • Melksham, Wiltshire • United Kingdom • SN12 6QL
PH: 01225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/3772_Hel ... -step.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat