Main Content

Hello World - Ruby on Rails - a checklist of each step

Archive - Originally posted on "The Horse's Mouth" - 2012-06-22 07:46:56 - Graham Ellis

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