2. Points to cover ..
❏ What is Rails?
❏ Rails Background
❏ Rails Philosophy
❏ Who uses Rails?
❏ Rails App Structure
❏ MVC Pattern
❏ Rails Components
❏ What is Rake?
❏ What is a Gem?
❏ Useful Links
❏ Demo ( Blog Application )
❏ Assignment
3. What is Rails?
● Rails is a web application development
framework written in the Ruby
language. It is designed to make
programming web applications easier by
making assumptions about what every
developer needs to get started.
4. Rails Background
● David Hanson => BaseCamp, Ruby on Rails
● Hanson released Rails as open source in
July 2004.
● December 2005, version 1 was released.
● Became very popular after Apple decided to
bundle and ship it with their Mac OS X,
Leopard, in October 2007.
10. Rails Components
● Models
○ Active Record Basics
○ Rails Database Migrations
○ Active Record Validations
○ Active Record Callbacks
○ Active Record Associations
○ Active Record Query
Interface
● Views
○ Layouts and Rendering in
Rails
○ Action View Form Helpers
● Controllers
○ Action Controller overview
○ Rails Routing from the
outside in
12. What is Active Record?
● Provides an interface and
binding between the tables in a
relational database and of the
Ruby program code that
manipulates database records
● Allows simple designs and
straightforward mappings
between database tables and
application objects
13. Active Record Migrations
A convenient way to alter your database schema over time
in a consistent and easy way. They use a Ruby DSL so that
you don't have to write SQL by hand, allowing your schema
and changes to be database independent.
14. Active Record Validations
As you can see, our validation lets us know that our Person
is not valid without a name attribute. The second Person
will not be persisted to the database.
name field must
have value
16. Active Record Callbacks
Callbacks are methods that get called at certain
moments of an object's life cycle. With
callbacks it is possible to write code that will run
whenever an ActiveRecord object is created,
saved, updated, deleted, validated, or loaded
from the database.
32. Layouts and Rendering
Controller automatically render
➢ views with names corresponding to valid
routes.
➢ {action_name}.html.erb
33. Layouts and Rendering
for HTML, just use the defaults:
get the view that is named the same as
the action, and render it
app/controllers/posts_controller.rb
37. Finding Layouts
Rails first looks for a file in app/views/layouts
with the same base name as the controller.
Current Layout
Application Layout
Controller Layout
Inheritance
43. A Controller
● RESTful applications
● Like an orchestra conductor
● As a middleman between models & views
44. REST (URI with HTTP methods)
GET /books HTTP/1.1
#index
GET /books/new HTTP/1.1
#new
GET /books/1 HTTP/1.1
#show
POST /books HTTP/1.1
#create
PUT /books/1 HTTP/1.1
#update
GET /books/1/edit HTTP/1.1
#edit
DELETE /books/1 HTTP/1.1 #destroy
45. Routing
$ rake routes CONTROLLER=books
named routes HTTP verbs resource URI Controllers & Actions
46. Methods & Actions
● A “class” has methods
● A controller < ApplicationController
● public methods => “action” <= routing
Actions
47. Rake
Rake a standalone Ruby utility
● Create a file .rake
● Run this rake file with the command rake
my:alarm
48. What is a Gem?
● Extending functionalities with already packaged
functions.
● i.e user authentication, message system, asset
handlers, geolocation, pagination system, linking to
exterior services such as Amazon AWS, and last but not
least Rails itself) are called Ruby Gems
● Gems are ruby software packages
● Lots of gems can be found in github, but its funner to
search for gems via ruby-gems or ruby-toolbox
49. Useful Links
➔ Ruby on Rails Guides
➔ Ruby on Rails Screencasts
➔ Rails Best Practices
➔ Rails Style Guide
➔ Testing Rails Applications
➔ Rails Tutorial Ebook by Michael Hartl
51. Assignment!
Add the ability to comment on any post.
While listing created comments for each
post & a link to remove any comment
Hint:
Use Active Record Associations