Ruby On Rails Ruby on Rails by Manik Juneja

Ruby On Rails
Ruby on Rails by Manik Juneja
About Rails
• Rails is a Web Application development framework.
• Based on the MVC pattern
• Gives a pure Ruby development environment
• Created by DHH of 37signals.
• Is open source
• Some sites running on RoR are basecamp, tadalist,
writeboard, Campfire, 43things, odeo, Blinksale,
Bubbleshare, Calendarhub, Nativetext, Numsum,
Sproutit, Suprglu
• Some open source applications built on RoR are typo,
hieraki2
Ruby on Rails by Manik Juneja
Rail’s Philosophy
– DRY( Don’t Repeat Yourself)
•Every piece of knowledge should be expressed in just one place
– Convention over configuration
•Sensible defaults for just about every aspect of your application. If you
need to overwrite the convention Rails makes that easy too.
The MVC architecture
Ruby on Rails by Manik Juneja
What is a Model?
Model stores the state of the application
Sometime this state is
• Transient (Session)
• Permanent (database)
Model = data + business rules that apply to it.
Ruby on Rails by Manik Juneja
Model: Object Relational Mapping
DB is relational
Ruby is Object Oriented
Table
Class
Row
Object
Columns
Attributes
Table Level operations
Class Methods
Row Level operations
Object Methods
ActiveRecord Module provides ORM in Rails.
Ruby on Rails by Manik Juneja
ActiveRecord: Getting Started
1. Specify database access details in config/database.yml file
2. Create a subclass of ActiveRecord::Base class
3. Give it a name. (By default, Active Record assumes that the name of the
table is the plural form of the name of the class. By default id is the primary
key )
Ruby on Rails by Manik Juneja
ActiveRecord: Class methods
Class Methods ( = Table level operations)
Ruby on Rails by Manik Juneja
ActiveRecord: Instance methods
Ruby on Rails by Manik Juneja
ActiveRecord Relationships: one to many
Ruby on Rails by Manik Juneja
ActiveRecord Relationships: many to many
Ruby on Rails by Manik Juneja
ActiveRecord: Other Features
• Transaction Handling
• Acts as (list, tree)
• Aggregation
• Validation
• Callback
• etc
Ruby on Rails by Manik Juneja
ActionPack : where the action happens
Action Pack lies at the heart of Rails applications.
It consists of two Ruby modules, ActionController
and ActionView. Together, they provide support for
processing incoming requests and generating
outgoing responses.
Ruby on Rails by Manik Juneja
ActionController
Controller orchestrates the application
• Receives external input
• Processes internal action
• Responds to user
Receives external input
In the form of a url like http://mysite.com/user/show/1
File config/routes.rb, is used to map external requests to internal actions.
controller
action
id
user
show
1
Ruby on Rails by Manik Juneja
ActionController: action
Controller looks for a public instance method with the same name as the
incoming action
Controller has access to the execution environment via objects such as request,
params, response, cookies, session, header
Ruby on Rails by Manik Juneja
ActionController : filter
Filters enable wrapping of controllers’ actions — you can write a chunk of code once
and have called before or after any number of actions in your controller
This is very useful in authentication, logging etc.
Ruby on Rails by Manik Juneja
ActionView
The ActionView module encapsulates the functionality needed to render templates.
A Template is text embedded with ruby code ( in erb tags <% ….. %>
All instance variables of the controller are available in the template.
Ruby on Rails by Manik Juneja
Layouts, Partials, Components
Honoring DRY
Many pages share the same header, footer, sidebar (use layouts)
Multiple pages may contain same snippet of rendered HTML (use partials)
Same functionality may appear on multiple pages ( use components )
Ruby on Rails by Manik Juneja
Pagination
Rails has inbuilt pagination support at both controller and view level
Ruby on Rails by Manik Juneja
Ajax on Rails
Rails has inbuilt support for Ajax
1. It has prototype, effects, drag drop and control JavaScript libraries built in.
2. JavaScriptHelper module which wrap JavaScript access in ruby code.
Ruby on Rails by Manik Juneja
Must Mention, before I Thank You
•Action Mailer
•Action Web Service
•Testing Framework
•Rake
Ruby on Rails by Manik Juneja
THANK YOU
Ruby on Rails by Manik Juneja