Title: Ruby on Rails
1Ruby on Rails
- Presenter Michael Mayer
- 30-March-2006
- Network Programming _at_ RPI
- This presentation was adapted from a ThoughtWise
presentation - By Obie Fernandez originally presented on
10-March-2005.
2Presentation Agenda
- Very brief overview of Ruby
- Description of Rails framework
- Questions and Answers
- Rails demo (if time allows)
3Why Ruby?
- Write understandable code in few lines
meta-programming - Free (Very open license)
- Extensible
- Truly object oriented there are no primitives
- Why not Ruby?
- Lack of support for casting
- Lack of documentation
- Relatively small community
4Some Coding Conventions
- Method Chainingprint array.uniq.sort.reverse
- (now thats meta-programming!)
- Method Names include ! and ?ary.sort!
- Iterators and Blocks vs. Loopsfiles.each
file process(file) - Case usage
- Class names begin with a Capital letter
- Constants are ALL_CAPS
- Everything else - method call or a local variable
- under_score instead of camelCase
5Enough About Ruby!
- Time to talk about Ruby on Rails!
6Rails in a Nutshell
- It is a rapid development framework a framework
is simply a set of best practices and
abstractions often accompanied by automation
scripts that, in combination, make SW development
with specific technologies easier. - Create database-driven web applications according
to the Model-View-Control pattern of separation. - Favors Convention over Configuration!
- Mostly written by David H. Hannson
- A 37signals.com principal
- Maintained by an active OS community!
7Rails Architecture
8Model View Controller
- Model classes are the "smart" domain objects
(e.g. Product) that hold business logic and know
how to persist themselves to a database - Views are HTML/XML templates
- Controllers handle incoming requests (e.g. Update
Product) by manipulating the model and directing
data to the view
9ActiveRecord API
- Object/Relational Mapping Framework
- Model classes use this API
- Automatic mapping between columns and class
attributes - Declarative configuration via macros
- Dynamic finders
- Associations, Aggregations, Tree and List
Behaviors - Locking
- Validation rules
- Et cetera
10ActiveRecord Associations
- Macro-like class methods for tying objects
together through foreign keys - Each adds a number of methods to the class
- In combination with migrations, allows your
database to be DBMS agnostic
11ActionController API
- Controllers defined as classes that execute and
then either render a template or redirects - An action is a public method on the controller
- Getting data in and out of controllers
- Request parameters available in the _at_params hash
(and can be multidimensional) - Web session exposed as _at_session hash
- Cookies exposed as _at_cookies hash
- Redirect scope provided by _at_flash hash (unique to
Rails)
12From Controller to View
- Rails gives you many rendering options
- Default template rendering follow naming
conventions and magic happens - Explicitly render to particular action
- Redirect to another action
- Render a string response (or no response)
13Approaches to View Templating
- ERB Embedded Ruby
- Similar to JSPs lt and lt syntax or PHPs lt?
syntax - Easy to learn and teach to designers
- Execute in scope of controller
- Denoted with .rhtml extension
- XmlMarkup Programmatic View Construction
- Great for writing xml content
- Denoted with .rxml extension
- Embeddable in ERB templates
14ERB Example
15XmlMarkup Example
16Layouts and Partials
- Templates in app/views/layouts/ with the same
name as a controller will be automatically set as
that controllers layout unless explicitly told
otherwise - Partials are sub-templates that render a single
object - Partial template files follow convention
- Easy API support for rendering collections of
partials.
17The Quest for Pretty URLs
- The responsibility of URL parsing is handled by
Rails itself. Why? - Not all webservers support rewriting
- Allows Rails to function out of the box on
almost all webservers - Facilitates definition of custom URLs
- Linked to URL helper methods such as url_for,
link_to, and redirect_to - Routing rules defined in config/routes.rb
18routes.rb
ActionControllerRoutingRoutes.draw do map
Priority based on creation first created -gt
highest priority Keep in mind you can assign
values other than controller and action
You can have the root of your site routed by
hooking up '' just remember to delete
public/index.html. map.connect '', controller
gt "bakery" map.connect 'query/guid',
controller gt global", actiongt"query"
Allow downloading Web Service WSDL as a file with
an extension map.connect 'controller/service.ws
dl', action gt 'wsdl' map.connect
'controller/action/id Default end
19Rails to the Rescue
- Actions that fail to perform as expected throw
exceptions - Exceptions can be rescued
- for public view (with a nice user-friendly
explanation) - for developers view (with tons of debugging
information) - By default, requests from localhost get
developers view
20Rake
- Rubys Build System
- Familiar to Ant users
- Your build file is a written in Ruby
- Basic build script provided with Rails project
21The Rails Community
- The developer community around Rails is very
helpful and excited - Rails Wiki - http//wiki.rubyonrails.com/
- Rails Mailing List very active
- IRC Get instant answers to most questions.
David and other Rails commiters are all regularly
present
22What did I leave out?
- ActionMailer API
- ActionWebService API
- ActiveSupport API
- Scaffolding
- Scripts and Generators
- Webrick web server
- Et cetera
23Questions / Comments?
- Thanks for listening!
- Feel free to email me. I am one of your TAs after
all - mayerm_at_cs.rpi.edu
- Original presentation
- http//rubyforge.org/docman/view.php/251/158/Ruby
20On20Rails.ppt
24Demo