Ruby On Rails Siddhesh

28
Ruby On Rails… Siddhesh Bhobe

description

Introduction to Ruby on Rails

Transcript of Ruby On Rails Siddhesh

Page 1: Ruby On Rails Siddhesh

Ruby On Rails…

Siddhesh Bhobe

Page 2: Ruby On Rails Siddhesh

What is Ruby?

An OO language! … successfully combines Smalltalk's

conceptual elegance, Python's ease of use and learning, and Perl's pragmatism…

… originated in Japan in the early 1990s

Page 3: Ruby On Rails Siddhesh

What is Rails?

Rails is an open source Ruby framework for developing database-backed web applications

Rail's guiding principles: less software and convention over configuration

Page 4: Ruby On Rails Siddhesh

Why the hell do we need another language and another framework???

Page 5: Ruby On Rails Siddhesh

Well… What if you could develop a web application at least ten times

faster with Rails?

Page 6: Ruby On Rails Siddhesh

Let’s start with a demo…

Page 7: Ruby On Rails Siddhesh

Demo Steps

1. Install Instant Rails which includes Rails, MySQL and an in-built web server – directory copy, no installation required

2. Create a Rails project 3. Create a database with sample table4. Generate the model and controller in Rails5. Provide CRUD functionality for the table on the webIN TEN MINUTES

Check http://instantrails.rubyforge.org/tutorial/index.html to try this on your own!

Page 8: Ruby On Rails Siddhesh

The Project Hierarchy – Convention over Configuration

Page 9: Ruby On Rails Siddhesh

Main Modules of a Rails Application

The controllers subdirectory is where Rails looks to find controller classes. A controller handles a web request from the user.

The views subdirectory holds the display templates to fill in with data from our application, convert to HTML, and return to the user's browser.

The models subdirectory holds the classes that model and wrap the data stored in our application's database.

The helpers subdirectory holds any helper classes used to assist the model, view, and controller classes.

Page 10: Ruby On Rails Siddhesh

Notes

The “scaffold” provides basic CRUD functionality, as well as basic views for the data – this can be overridden by you

URLs are simple and straightforward Smartly maps models to table names -

Recipe to recipes, Person to people and Country to countries

Page 11: Ruby On Rails Siddhesh

Overriding Scaffold Methods

1. Define your method (say, for the list functionality) in the controller

Page 12: Ruby On Rails Siddhesh

Overriding Scaffold Methods

2. Provide your own list.rhtml - simply an html file with Ruby code embedded within <% %> and <%= %> tags

Page 13: Ruby On Rails Siddhesh

And We Get..

Page 14: Ruby On Rails Siddhesh

Setting Relationships between Model Objects

Class Recipe < ActiveRecord::Base

belongs_to :category

End

Class Category < ActiveRecord::Base

has_many :recipes

end

Page 15: Ruby On Rails Siddhesh

Use Layouts

Provides common header/footer for all pages

Actual content goes in place of @content_for_layout

Page 16: Ruby On Rails Siddhesh

And We Get…

Page 17: Ruby On Rails Siddhesh

Hey, but this was too simple an application! Does it do anything

serious?

Page 18: Ruby On Rails Siddhesh

Some Real Applications

Basecamp is a web-based tool that lets you manage projects (or simply ideas) and quickly create client/project extranets. It lets you and your clients (or just your own internal team) keep your conversations, ideas, schedules, to-do lists, and more in a password-protected central location.

http://www.basecamphq.com/

Page 19: Ruby On Rails Siddhesh

Some Real Applications

43 Things is a goal-setting social software web application. It currently has 6,000 registered users and hundreds of thousands of unregistered visitors. 43 Things has 4,500 lines of code that were developed in three months by three full-time developers.

http://www.43things.com/

Page 20: Ruby On Rails Siddhesh

Some Real Applications

Ta-da Lists is a free online service that implements simple, sharable to-do lists. It features a highly responsive user interface that uses XMLHttpRequest to minimize waiting for the server. Ta-da Lists came from one developer using one week of development time producing 579 lines of code.

http://www.tadalist.com/

Page 21: Ruby On Rails Siddhesh

And More..

Check out the screencasts at

http://www.rubyonrails.org/screencasts

Page 22: Ruby On Rails Siddhesh

Cool Rails Features

Caching Page (as HTML), Action (Similar to Page, but applies

filters like authentication), Fragment Caches calculations, renderings and database calls Sweeper – cleans the cache based on changes to the

model objects Data Validation

validates_length_of :category, :within => 1..20 Callbacks (like before and after triggers) – very useful

for cache sweeping, for example Transactions – save and destroy are already covered Custom Code Generators

Page 23: Ruby On Rails Siddhesh

Ajax on Rails

Ajax (Asynchronous JavaScript and XML) works on top of XMLHttpRequest

Ajax (through XMLHttpRequest) lets browser-side JavaScript communicate with the web server in the background without requiring the browser to display a new web page

Libraries like DWR and Ajax.NET provide high-level services for Ajax that hide browser-specific differences

Rails has built-in support for Ajax

Page 24: Ruby On Rails Siddhesh

How it Works in Rails

1. A trigger action occurs. This could be the user clicking on a button or link, the user making changes to the data on a form or in a field, or just a periodic trigger.

2. Data associated with the trigger (a field or an entire form) is sent asynchronously to an action handler on the server via XMLHttpRequest.

3. The server-side action handler takes some action based on the data, and returns an HTML fragment as its response.

4. The client-side JavaScript (created automatically by Rails) receives the HTML fragment and uses it to update a specified part of the current page's HTML, often the content of a <div> tag.

Page 25: Ruby On Rails Siddhesh

A Sample View File

Page 26: Ruby On Rails Siddhesh

The Corresponding Controller

Page 27: Ruby On Rails Siddhesh

And You Get…

Page 28: Ruby On Rails Siddhesh

References

Article on which this presentation is based: http://instantrails.rubyforge.org/tutorial/index.html

Ruby Homepage:

http://www.ruby-lang.org/en/ Ruby on Rails Homepage:

http://www.rubyonrails.com/ Four Days on Rails (great to start with):

http://rails.homelinux.org/ Ruby on Rails How Tos:

http://wiki.rubyonrails.com/rails/pages/Howtos