21
Ruby on Rails by Manik Juneja Ruby On Rails

Ruby on Rails by Manik Juneja Ruby On Rails. Ruby on Rails by Manik Juneja Rails is a Web Application development framework. Based on the MVC pattern

Embed Size (px)

Citation preview

Ruby on Rails by Manik Juneja

Ruby On Rails

Ruby on Rails by Manik Juneja

• 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

About Rails

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

Model stores the state of the application

Sometime this state is

• Transient (Session)

• Permanent (database)

Model = data + business rules that apply to it.

What is a Model?

Ruby on Rails by Manik Juneja

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.

Model: Object Relational Mapping

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

Class Methods ( = Table level operations)

ActiveRecord: Class methods

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

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.

ActionController

controller user

action show

id 1

Ruby on Rails by Manik Juneja

Controller looks for a public instance method with the same name as the incoming action

ActionController: 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 : filterFilters 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

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.

ActionView

Ruby on Rails by Manik Juneja

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 )

Layouts, Partials, Components

Ruby on Rails by Manik Juneja

Rails has inbuilt pagination support at both controller and view level

Pagination

Ruby on Rails by Manik Juneja

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.

Ajax on Rails

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