You are on page 1of 55

Riding the Rails

Jorge Chao
University of New Orleans
Slides available online at www.cs.uno.edu/~jchao/
RailsIntro.pdf

Wednesday, April 27, 2011


Rails Vs. Seaside Vs.
Struts+Hibernate

Wednesday, April 27, 2011


What is Rails?
• Rails is an open source web application
framework.
• It follows the Model-View-Controller (MVC)
architectural pattern that is emphasized by
some Columbian Professors Who Shall
Remain Nameless.
• It also follows other buzzwordy design
philosophies such as DRY and “Convention
over Configuration”
• To achieve DRYness, ActiveRecord provides
an Object-Relational Mapping (O/RM) layer.
Wednesday, April 27, 2011
Web Applications

• “With server-based software, no one can


tell you what language to use, because you
control the whole system...Different
languages are good for different tasks”
- Paul Graham, Hackers & Painters

Wednesday, April 27, 2011


WEBrick/Mongrel
• Rails comes with two possibilities for
running your application server: WEBrick, a
simple server intended to get you started,
and Mongrel written by the brilliant Zed
Shaw (also author of Rails is a Ghetto)
• Rails apps can be run on many web servers
including thin, Lighttpd(“lighty”), Apache
(using Passenger or mod_ruby), nginx and
others.

Wednesday, April 27, 2011


$ rails new hotness
• Here is how every rails application begins

Wednesday, April 27, 2011


App Layout
• This is what you get from rails new
<app_name>

Wednesday, April 27, 2011


Starting the Server
• From the application directory, issue rails
server.

Wednesday, April 27, 2011


All Systems Go
• Navigate to localhost:3000 to confirm your
application was created successfully.

Wednesday, April 27, 2011


Okay, So What?

• Now we have a working rails application


that does nothing.
• The ‘rails new’ command is analogous to
creating a new project in Eclipse, all the
directories are laid out for you, nudging you
toward MVC.

Wednesday, April 27, 2011


Model-View-Controller
• Supported out of the box in Rails. In fact,
you have to try really hard to break MVC.

Wednesday, April 27, 2011


DRY
• Don’t Repeat Yourself.
• The DRY principle states: “Every piece of
knowledge must have a single,
unambiguous, authoritative representation
within a system.”
• Code is made more DRY in Rails using
ActiveRecord and the separation of view
code from business logic (MVC)

Wednesday, April 27, 2011


Active Record
• Active Record was first a design pattern
outlined by Martin Fowler in his book
Patterns of Enterprise Application Architecture
(Addison-Wesley Professional, 2002).
• The Active Record pattern is a way to
access data in a database. A database table
is wrapped with a class, so an object
instance is tied to a single row in the table.
Querying/modifying the table then behaves
like manipulating an object.
Wednesday, April 27, 2011
ActiveRecord
• The secret sauce of Rails. This is the
Object-Relational Mapping layer that ties
objects to similarly named tables in your
database. This is based directly on the
design pattern of the same name.
• It is database-agnostic, so the Rails code
you write to query some records, or make
table changes works across multiple
databases(these are called migrations).
Wednesday, April 27, 2011
Model Creation
• Scaffolding in rails will give you a model
with simple CRUD views and a basic
controller to start with.

Wednesday, April 27, 2011


Models
• Models are very spare at the beginning of
application development because of OR/M.
All of the attributes are stored in the table.
• I like annotating the models with their
attributes in comments via a plugin,
annotate_models.
• Methods for getting and setting are also
automatically created by rails for each
attribute (using :attr_accessors)
Wednesday, April 27, 2011
Post Model
• This is our newly created model, annotated
with schema information for clarity.

Wednesday, April 27, 2011


Convention V.
Configuration
• The good thing about all this generated
code is it gives you a general idea of where
things ought to go, and what models, views
and controllers should look like.
• Except for models. The Post model has
nothing in it! It gives no indication of
standard model configuration.

Wednesday, April 27, 2011


What to Put in a Model
• Model relationships such
as :has_one, :has_many, :belongs_to,
:has_and_belongs_to_many
• Validations: validates_presence_of,
validates_confirmation_of, or custom
validations
• Class or instance methods that don’t
belong in the controller.

Wednesday, April 27, 2011


Post Redux

Wednesday, April 27, 2011


A Final Project Example
• Here is how password authentication and
storage is being handled in a rails version of
the final project.

Wednesday, April 27, 2011


Migrations
• Migrations are a way of having a kind of
version control on your table structure/
schema.
• Rather than modify the schema directly,
migrations allow you to create the schema
using Ruby code.
• Migration generators create a file with two
methods self.up and self.down
• self.up makes the change, self.down reverts it.
Wednesday, April 27, 2011
Post Migration
• This is the migration that created our table.

Wednesday, April 27, 2011


Running Migrations
• $ rake db:migrate
• This will run all migrations that have not
been applied to the database, changing you
schema accordingly.
• $ rake db:migrate VERSION=n
• This will rollback your schema to the n-th
version specified.

Wednesday, April 27, 2011


Schema
• How rails creates the database schema.

Wednesday, April 27, 2011


SQL Console View
• Here is yet another view of the database so
far.

Wednesday, April 27, 2011


Cooking With YAML
• Rails uses YAML files to manage database
connectivity.
• YAML stands for Yet Another Markup
Language, or YAML Ain’t Markup Language,
depending on who you ask.
• YAML is a human-readable data serialization
format that takes concepts from programming
languages such as C, Perl, and Python, and ideas
from XML.

Wednesday, April 27, 2011


YAML File
• This is the auto generated YAML for our DB.

Wednesday, April 27, 2011


Free CRUD!

• CRUD is shorthand for Create, Read,


Update, and Delete, the most common
actions that are performed on database
records.
• Scaffolding generates the CRUD controller
methods and views for us.

Wednesday, April 27, 2011


New CRUD
• Creating a blog post using our new view.

Wednesday, April 27, 2011


Show View
• This is our newly created blog post.

Wednesday, April 27, 2011


Rails Console

• To query the database in rails we use the


rails console.
• The console is an instance of irb with the
application environment loaded so we can
test our application informally in addition
to querying for data.

Wednesday, April 27, 2011


Rails Console

Wednesday, April 27, 2011


Controller
• In the controller for the model, all of the
methods are controller actions that
correspond to views (new, edit, show, list,
etc.)
• Controllers are where your view logic
should go such as pagination, or page
access.

Wednesday, April 27, 2011


Controller Actions

Wednesday, April 27, 2011


More Actions

Wednesday, April 27, 2011


Create and Update

Wednesday, April 27, 2011


Views

• Views in rails are HTML documents with


embedded ruby, such as new.html.erb.
• View files with the same name as a
controller action (method) get rendered
when you navigate to the URL
• Ex: http://localhost:3000/posts/new

Wednesday, April 27, 2011


Partials
• If there is a part of a view that is repeated
in multiple views, its common practice to
put it in a partial.
• Partials are located in the same folder as
the view file with the convention that they
start with an underscore.
• Ex: app/views/posts/_links.html.erb
Wednesday, April 27, 2011
Show View
• Default show view generated by scaffold.

Wednesday, April 27, 2011


New View
• Scaffolding gives you very DRY views.

Wednesday, April 27, 2011


Edit View
• Since new and edit are basically the same
view, the common attributes have been
refactored into a partial called form.

Wednesday, April 27, 2011


Form Partial

Wednesday, April 27, 2011


Routing
• The routes.rb file in the config directory
manages routing URLs to controller
actions.
• The entire routing interface has been
redesigned in Rails 3.
• See http://edgeguides.rubyonrails.org/
routing.html for more details

Wednesday, April 27, 2011


Routing Example
• From edgerails:

Wednesday, April 27, 2011


Routing Helper
Methods
• Routing helper methods are created for
use in views.

Wednesday, April 27, 2011


Blog Routes
• Since we only have one model, we only
have one resource route.

Wednesday, April 27, 2011


Layouts

• To reproduce a standard site layout across


all views, layouts are used.
• The default layout is app/views/layouts/
application.html.erb
• Layout files use ruby yield statements to
insert desired content inside the site layout.

Wednesday, April 27, 2011


Layouts
• Our application.html.erb file.

Wednesday, April 27, 2011


Rake
• Ruby Make.
• Rake is a software build tool like make or
Ant written in Ruby.
• Applications out of the box come with
many rake tasks, and tasks can be created
for things such as testing, data importing,
new server deployments, etc.

Wednesday, April 27, 2011


Included Rake Tasks

Wednesday, April 27, 2011


Importing Course Info

Wednesday, April 27, 2011


Gems
• Gems are ruby packages that are freely
available on the web.
• Some examples of gems are: The mysql
gem for mysql connectivity, the BCrypt gem
for data encryption, devise for robust user
authentication and session management.
• Visit rubygems.org for a complete list of
gems (and to get the gem package itself).

Wednesday, April 27, 2011


links.each do |link|
• For more in-depth information there are a
ton of great resources on the web.
• http://www.railscasts.org
• http://api.rubyonrails.org/
• http://edgeguides.rubyonrails.org/
3_0_release_notes.html

Wednesday, April 27, 2011


Thanks For Listening

• Now go make a Twitter that doesn’t have


the same stability problems as Twitter with
your newfound rails knowledge.
• Make lots of money.

Wednesday, April 27, 2011