“X” Things, to Understand about Ruby on Rails
May 23, 2006 § Leave a comment
Rails:
“X” Things You Need To
Know
Ruby on
Rails is perhaps the most innovative web
application framework since WebObjects ,
with developers claiming 10x boosts in productivity over J2EE. But, what is it
“really”? And why should I care? These notes and sample code complement the enormous number of
Ruby and Rails tutorials available elsewhere by exploring the key design
principles, patterns and techniques from the perspective of a long-time Cocoa
developer. The goal is to help you make an informed decision about why,
whether, and how to use Rails to accelerate your next web development
project.
Things
To Understand
About Ruby on Rails
available
understand: Ruby Gems
1.1.2
0a.
Examples
one-click rails on Mac OS X
Davidson
on Rails on Oracle: A Simple
Tutorial
1. What is
Rails?
“An opinionated toolset for
lightweight SQL-based web
apps”
a.
Opinionated
“Our way or the hard
way.”
Configuration
Pick one problem and solve it
well
(TSTTW)
Real(tm)
b.
Toolset
“Minimalist yet
complete.”
ActiveSupport, ActionMailer
script/*
login, wiki, etc.
adaptors
migrations
Mongrel
time-savers
configuration
d.
SQL-based
back-end
rows
really
need to
et
al, but not a great match
Data
e. Web
apps
JavaScript)
services
a testing/proxy HTTP server
2.
Why NOT use Rails?
Might not be ideal if
you want:
datastores
corporations
infrastructure (i.e., it is not WebObjects or J2EE)
strategies
Database
pages
language
your
way
3. Why
Ruby?
“Smalltalk with a friendly
syntax”
Perl : C++ (IMHO)
Everything
is an object (ints, classes, functions, methods, modules, etc.)
told not to; then just warn)
Freud
than Python)
worldview
fundamental constructs)
go
interactive
3a.
Examples
browser
(2003)
2003)
About Ruby (2005)
stiff‘s (Poignant) guide to Ruby (book)
2006)
(October 2005)
Program, using Ruby (January 2006)
Postgres, Oracle, etc.
(harder to natively optimize)
(wrapping)
vs. model-driven
(mapping)
(auto-create, at least
SQLite)
4a.
Example
xthings # creates directory
infrastructure
!$ # do everything from within that
directory
config/database.yml # point to that database
adapter: sqlite3?ÊÊ dbfile:
db/xthings_development.db
5.
Generators & Migrations
“Code
generation done right”
(enabling
human editing)
script/generate model Thing # model
class
app/models/thing.rb # Look –
code!
db/migrate/001_create_things.rb # How Ruby
manages databases
:string
:default => “damned”
migrate # runs
migration
to create “Thing” table with those columns
db/xthings_development.db '.dump things' #
Note plural
script/generate scaffold
Thing #
controller + view templates
app/views/layouts/things.rhtml # What, HTML +
Ruby in one file?
app/controllers/things_controller.rb # Ah,
real code at last
6.
Deployment
“Web apps need a web
server”
WEBrick:
built-in, just works, pure Ruby; slooooow
FastCGI:
easy install, works with Apache or lighttpd
lighttpd:
optimized for FastCGI, will auto-detect and use
Apache:
works great; non-trivial configuration, awkward FastCGI
Mongrel:
“mostly Ruby”, fast but flexible, separate install (gem)
SCGI:
simpler FastCGI; overtaken by Mongrel + HTTP
proxy
6a.
Example
config/routes.rb # show me my
Things!
“things”
public/index{,.old}.html # hide default page
from webserver
console
script/console
“#1”, :state => “Cursed”})
Thing.find_all
7. Active
Record
Object-Relational
Wrapping
Class
Object
Property
default)
Hat # Create another table
db/migrate/002_create_hats.rb # Add fields
linking “hats” to “things” ? t.column “thing_id”,
:integer
=> :false
migrate
'.dump hats'
app/models/hat.rb # edit model to capture
relationships in table
containing a foreign key
:thing
“color”
app/models/thing.rb
Hat.find_by_thing_id(id).update_attribute(‘thing_id’,NIL)
Thing.find_all[0]
Hat.new({:color=>”Blue”, :thing_id=>thing.id})
thing0.hat.color
8. Templates
and Controllers
“Make it easy to do
good, not hard to do wrong.”
generation
text
URLs
output
8a. Example
$ script/generate scaffold Hat
$ mate
app/views/things/edit.rhtml
<% if @thing.hat.nil? %>
<%= link_to "[New Hat]", :controller =>
"hats", :action => "new", :thing_id => @thing.id %>
<% else %>
<%=h @thing.hat.color %> hat
<%= link_to '[Edit Hat]', :controller =>
"hats", :action => "edit", :id => @thing.hat.id %>
<%= link_to '[Destroy Hat]', {:controller
=> "hats", :action => 'destroy', :id => @thing.hat.id }, :confirm =>
'Really destroy hat?', :post => true %>
<% end %>
$ mate
app/controllers/hats_controller.rb
def new
@hat = Hat.new
@hat.thing_id =
params[:thing_id]
end
redirect_to :controller => ‘things’,
:action => ‘list’
harnesses
objects
objects
database
9a.
Example
config/database.yml # point to that database
sqlite3?ÊÊ dbfile: db/xthings_test.db
tests/fixtures/hats.yml # point to that
database
first:
? id:
1
? color:
“red”
?
another:
? id:
2
? color:
“green”
test
10.
Resources
“But wait, there’s
more!”
Flex
2.0
Sheet and QuickRef
Sheet
Studio
Feldpost
Leave a Reply