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
R203 - Ruby - Installing and setting up Rails [2610] Cheat Sheet - what do you need for Ruby on Rails? - (2010-01-31)
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)
[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)
[4010] Really Simple Rails - (2013-02-17)
[4013] Web Frameworks - nested templates - (2013-02-22)
Some other Articles
Some traps it's so easy to fall into in designing your web siteAlan Turing - 1912 to 1954Melksham - a new dawnRuby on the web - a simple example using CGIHello World - Ruby on Rails - a checklist of each stepFine evening, country walk from Melksham - picturesSample answers to training course exercises - available on our web siteMuttable v immutable and implications - RubyMelksham Chamber of Commerce - looking to our future shape. Pivotal meeting next TuesdayHow well do you know Melksham?