CSCE 740 Software Engineering
Lecture 11
Rails
Topics
• SaaS
Readings: SaaS book Ch
4.1-4.5
February 24 2014
Tools Last Time
Ruby Basics
Ruby Regexp
New
Test 1
Revisit Chapter 2
Classes
Objects
Rails
xxx
Next Time:
–2–
CSCE 740 Spring 2014
http://gorails.com/setup/ubuntu/13.10
github.com:saasbook/courseware
–3–
CSCE 740 Spring 2014
Rails exposes the client-server, three-tier
architecture, and model–view–controller patterns, all
of which are common in SaaS apps.
Rails’ ActiveRecord package uses Ruby’s
metaprogramming and convention over
configuration to free you from writing any code at all
to perform the basic Create, Read, Update and Delete
(CRUD) operations on your models, as long as you
follow certain conventions about naming classes and
variables.
–4–
Engineering Software as a Service, Patterson & Fox
CSCE 740 Spring 2014
Rails’ ActionView and ActionController packages
provide help for creating Web pages, dealing with fillin forms, and setting up the routes that map URIs to
controller actions (code in your app).
A properly-constructed Rails app can be easily
adapted to work in a service-oriented architecture,
communicating with external services rather than
with a human using a browser.
Debugging SaaS requires understanding the
different places something could go wrong during
the flow of a SaaS request, and making that
information visible to the developer.
–5–
Engineering Software as a Service, Patterson & Fox
CSCE 740 Spring 2014
4.1 Rails Basics: From Zero to CRUD
Rails is a SaaS application framework that defines a
particular structure for organizing your application’s
code and provides an interface to a Rails application
server such as Rack.
The app server waits for a Web browser to contact your
app and maps every incoming request (URI and
HTTP method) to a particular action in one of your
app’s controllers.
Three main modules make up the heart of Rails’
support for MVC:
ActiveRecord for creating models,
ActionView for creating views, and
ActionController for creating controllers.
–6–
Engineering Software as a Service, Patterson & Fox
CSCE 740 Spring 2014
Basic Rails application with a single
model
1. Creating the skeleton of a new app
2. Routing
3. The database and migrations
4. Models and Active Record
5. Controllers, views, forms, and CRUD
–7–
Engineering Software as a Service, Patterson & Fox
CSCE 740 Spring 2014
Rails command
apt-get install rails or aptitude install rails to install
rails –help
rails –version
rails new myrottenpotatoes -T
–8–
Engineering Software as a Service, Patterson & Fox
CSCE 740 Spring 2014
Ruby libraries are called Rubygems
Rubygems is a system for managing external usercontributed Ruby libraries or gems.
Bundler, a gem, looks for a Gemfile in the app’s root
directory that specifies not only what gems your app
depends on, but what versions of those gems
rails is itself a gem
–9–
Engineering Software as a Service, Patterson & Fox
CSCE 740 Spring 2014
Configuring your Gemfile
# use Haml for templates
gem 'haml'
# use Ruby debugger
group :development, :test do
gem 'debugger'
end
Then
bundle install --without production,
– 10 –
Engineering Software as a Service, Patterson & Fox
CSCE 740 Spring 2014
automation for repeatability
automation for repeatability:
rather than manually installing the gems your app
needs,
listing them in the Gemfile and letting Bundler install
them automatically ensures that the task can be
repeated consistently in a variety of environments,
– 11 –
Engineering Software as a Service, Patterson & Fox
CSCE 740 Spring 2014
RESTful routes
routes – specify the mapping from HTTP method to
controller action
rake routes – prints the routes
RESTful routes specify self-contained requests of what
operation to perform and what entity, or resource, to
perform it on
Rails shortcut that creates RESTful routes for the four
basic CRUD actions (Create, Read, Update, Delete)
on a model
http://guides.rubyonrails.org/routing.html#connectin
g-urls-to-code
– 12 –
Engineering Software as a Service, Patterson & Fox
CSCE 740 Spring 2014
log/development.log
log/development.log is where you look to find detailed
error information when something goes wrong.
– 13 –
Engineering Software as a Service, Patterson & Fox
CSCE 740 Spring 2014
Edit config/routes.rb, replace with
Myrottenpotatoes::Application.routes.draw do
resources :movies
root :to => redirect('/movies')
end
run rake routes again
– 14 –
Engineering Software as a Service, Patterson & Fox
CSCE 740 Spring 2014
Using convention over configuration
Rails will expect this controller’s actions to be
defined in the class MoviesController,
if that class isn’t defined at application start time,
Rails will try to load it from the file
app/controllers/movies_controller.rb.
Sure enough, if you now reload the page
http://localhost:3000/movies in your browser, you
should see a different error: uninitialized constant
MoviesController.
– 15 –
Engineering Software as a Service, Patterson & Fox
CSCE 740 Spring 2014
The root route ’/’, RottenPotatoes’ “home page,” will
take us to the main Movie listings page by a
mechanism we’ll soon see called an HTTP redirect.
– 16 –
Engineering Software as a Service, Patterson & Fox
CSCE 740 Spring 2014
Summary:
commands to set up a new Rails app:
– 17 –
rails new sets up the new app; the rails command also has
subcommands to run the app locally with WEBrick (rails
server) and other management tasks.
Rails and the other gems your app depends on (we added
the Haml templating system and the Ruby debugger) are
listed in the app’s Gemfile, which
Bundler uses to automate the process of creating a
consistent environment for your app whether in
development or production mode.
To add routes in config/routes.rb, the one-line resources
method provided by the Rails routing system allowed us to
set up a group of related routes for CRUD actions on a
RESTful resource.
The log files in the log directory collect error information
when something goes wrong.
Engineering Software as a Service, Patterson & Fox
CSCE 740 Spring 2014
ELABORATION: Automatically
reloading the app
After changing routes.rb, you don’t have to stop and
restart the app in order for the changes to take effect.
In development mode, Rails reloads all of the app’s
classes on every new request, so that your changes
take effect immediately.
In production this would cause serious performance
problems, so Rails provides ways to change various
app behaviors between development and production
mode,
– 18 –
Engineering Software as a Service, Patterson & Fox
CSCE 740 Spring 2014
Non-resource based routes
Route: get ’:controller/:action/:id’ or
get ’photos/preview/:id’
Example URI: /photos/preview/3
Behavior: call PhotosController#preview params[]:
{:id=>3}
Route: get ’photos/preview/ :id’
Example URI: /photos/look/3?color=true
Behavior: no route will match (look doesn’t match
preview)
– 19 –
Engineering Software as a Service, Patterson & Fox
CSCE 740 Spring 2014
Route: get ’photos/:action/:id’
Example URI: /photos/look/3?color=true
Behavior: call PhotosController#look (look matches
:action) params[]: {:id=>3, :color=>’true’}
Route: get ’:controller/:action/:vol/:num’
Example URI:
/magazines/buy/3/5?newuser=true&discount=2
Behavior: call MagazinesController#buy params[]:
{:vol=>3, :num=>5, :newuser=>’true’, :discount=>’2’
– 20 –
.
Engineering Software as a Service, Patterson & Fox
CSCE 740 Spring 2014
Rails Routing from the Outside In
This guide covers the user-facing features of Rails
routing.
After reading this guide, you will know:
How to interpret the code in routes.rb.
How to construct your own routes, using either the
preferred resourceful style or the match method.
What parameters to expect an action to receive
How to automatically create paths and URLs using
route helpers.
Advanced techniques such as constraints and Rack
endpoints.…
– 21 –
http://guides.rubyonrails.org/routing.html
CSCE 740 Spring 2014
Databases and Migrations
persistence tier uses relational DBMS
sqlite3 for development
(note WEBrick is webserver-lite)
Each environment has its own separate DB
specified in config/database.yml
testing DB only for automated tests (hands off)
The production environment should
be a higher performance DB (postgress)
mirror changes to the development DB
– 22 –
Engineering Software as a Service, Patterson & Fox
CSCE 740 Spring 2014
Migrations
So how do we update the production DB in same way
as the development DB?
Remember DRY?
A migration is a portable script for changing the DB
schema in a consistent and repeatable way
– 23 –
Engineering Software as a Service, Patterson & Fox
CSCE 740 Spring 2014
Migration a 3-Step Process
1. Create a migration describing what changes to
make. As with rails new, Rails provides a migration
generator that gives you the boilerplate code, plus
various helper methods to describe the migration.
2. Apply the migration to the development database.
Rails defines a rake task for this.
3. Assuming the migration succeeded, update the test
database’s schema by running rake db:test:prepare.
4. Run your tests, and if all is well, apply the migration
to the production database and deploy the new code
to production.
– 24 –
The process for applying migrations in production
depends on the deployment environment;
Engineering Software as a Service, Patterson & Fox
CSCE 740 Spring 2014
Migration Management
db/migrate
name = time-created + user-supplied-name
meaningful to both you and rails
– 25 –
Engineering Software as a Service, Patterson & Fox
CSCE 740 Spring 2014
– 26 –
Engineering Software as a Service, Patterson & Fox
CSCE 740 Spring 2014
– 27 –
Engineering Software as a Service, Patterson & Fox
CSCE 740 Spring 2014
– 28 –
Engineering Software as a Service, Patterson & Fox
CSCE 740 Spring 2014
– 29 –
Engineering Software as a Service, Patterson & Fox
CSCE 740 Spring 2014
– 30 –
Engineering Software as a Service, Patterson & Fox
CSCE 740 Spring 2014
– 31 –
Engineering Software as a Service, Patterson & Fox
CSCE 740 Spring 2014
© Copyright 2026 Paperzz