Ruby On Rails Siddhesh
-
Upload
siddhesh-bhobe -
Category
Technology
-
view
713 -
download
1
description
Transcript of Ruby On Rails Siddhesh
Ruby On Rails…
Siddhesh Bhobe
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
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
Why the hell do we need another language and another framework???
Well… What if you could develop a web application at least ten times
faster with Rails?
Let’s start with a demo…
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!
The Project Hierarchy – Convention over Configuration
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.
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
Overriding Scaffold Methods
1. Define your method (say, for the list functionality) in the controller
Overriding Scaffold Methods
2. Provide your own list.rhtml - simply an html file with Ruby code embedded within <% %> and <%= %> tags
And We Get..
Setting Relationships between Model Objects
Class Recipe < ActiveRecord::Base
belongs_to :category
End
Class Category < ActiveRecord::Base
has_many :recipes
end
Use Layouts
Provides common header/footer for all pages
Actual content goes in place of @content_for_layout
And We Get…
Hey, but this was too simple an application! Does it do anything
serious?
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/
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/
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/
And More..
Check out the screencasts at
http://www.rubyonrails.org/screencasts
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
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
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.
A Sample View File
The Corresponding Controller
And You Get…
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