ruby and rubyonrails

29
Ruby and Ruby-on-Rails 17 th of November, 2007 Hyderabad, India

Upload: sampetruda

Post on 19-Jan-2015

521 views

Category:

Documents


0 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Ruby and RubyonRails

Ruby and Ruby­on­Rails

17th of November, 2007Hyderabad, India

Page 2: Ruby and RubyonRails

We appreciate 

Page 3: Ruby and RubyonRails

Outline● Ruby

● Ruby­on­Rails

− Ruby­on­Rails: What is it?

− MVC Pattern

− Scaffolding

− Active Record

− Action View

− Action Controller

− Rails and Ajax

Page 4: Ruby and RubyonRails

Outline− Action Mailer

− Web Service

− IDE support

● Conclusion

Page 5: Ruby and RubyonRails

What is Ruby?● Object­Oriented scripting language

● Conceptual similarities to Smalltalk

● Many system and text manipulation features like Perl

● Emphasis on simplicity and design elegance

Page 6: Ruby and RubyonRails

Ruby is a Dynamic Language● Completely Object­Oriented

− All data is an object, no exceptions

− Operators are methods

● Dynamic Behavior

− Possible to add new classes, add or redefine methods at runtime

− An instance of one class can behave differently than another instance of the same class at runtime

Page 7: Ruby and RubyonRails

Powerful Features● Single Inheritance, but…

− Modules provide namespaces and allow ‘mixin’ capability

− A module is a collection of methods and constants

● Ruby has blocks

− Code surrounded by do…end or { … }

− They are true closures; get variable bindings

− Are passed to methods quite often

Page 8: Ruby and RubyonRails

More Language Features● Portable

− OS independent threading

− Can load extension libraries dynamically

● Library support

− RubyGems package manager

− Tons of high­quality open source libraries available at RubyForge (similar to CPAN for Perl)

Page 9: Ruby and RubyonRails

Ruby ToolsThe following tools are included with the Ruby distribution

− debugger

− irb – interactive ruby shell

− benchmark

− profiler

− rdoc

Page 10: Ruby and RubyonRails

Ruby­on­Rails

Page 11: Ruby and RubyonRails

What Is Rails?Short Answer:

− An extremely productive web application framework that is written in Ruby by David Heinemeier Hansson.

Long Answer:

− Lets see the slides

Page 12: Ruby and RubyonRails

What Is Rails?Full Stack Framework

− Includes everything needed to create a database­driven web application using the Model­View­Controller pattern.

− Being a full­stack framework means that all layers are built to work seamlessly together.

− That way you Don’t Repeat Yourself (DRY)

Less Code

− Requires fewer total lines of code than other frameworks spend setting up their XML configuration files.

Page 13: Ruby and RubyonRails

What is RailsConvention over Configuration

− Rails shuns configuration files in favor of conventions, reflection and dynamic run­time extensions

Configure your application by making it

− Your code and database schema are the configuration!

− No compilation phase● Make a change, see it work.

Page 14: Ruby and RubyonRails

Model­View­Controller (MVC)

User Web browser

ControllerViews

View

ModelsModel

1

2

3, 4

5, 6

7

end

Page 15: Ruby and RubyonRails

Rails Components● Model

− Active Record ­ SQL database access● View

− Action View ­ template rendering control− eRB ­ HTML templates− XML::Builder ­ XML templates

● Controller

− Action Controller ­ request processing

Page 16: Ruby and RubyonRails

Rails ModelsActiveRecord Model Classes

− Encapsulate persistence logic

− Contain business rules

− Tightly coupled to database tables

− Declare relationships to each other

Page 17: Ruby and RubyonRails

ActiveRecord Basics● Extend ActiveRecord::Base

● Don’t declare properties

● Declare relationships to other models with the following ‘macros’

− belongs_to

− has_many

− has_and_belongs_to_many (joins)

Page 18: Ruby and RubyonRails

Rails ViewsUser interface done with templates

− HTML with Ruby snippets in .rhtml files

− Easy XML generation in .rxml files

− Lots of HTML and AJAX helper methods

− Sophisticated layout and ‘partials’ functionality

Page 19: Ruby and RubyonRails

ERB Template Example

Page 20: Ruby and RubyonRails

Rails Controllers

Process requests via action methods that map to URL

− Interact with model classes

− Set any data needed by view as field variables

− Select view to render or redirect

Page 21: Ruby and RubyonRails

Controller Basics● Extend ActionController:Base

● Request parameters in ‘params’

− Naming conventions mean Rails can translate paramaters into a hashtable

− Handles multi­dimensional data in forms pretty easily● Web session in ‘session’ hash

● Redirect scope available in ‘flash’ hash for next request only

Page 22: Ruby and RubyonRails

AJAX<head>

<%= javascript_include_tag :defaults %>

</head>

Inserts JavaScript AJAX libraries supplied with every Rails application.  No additional scripting is necessary to use Rails AJAX­based functions.

Page 23: Ruby and RubyonRails

AJAX Functions● form_remote_tag / link_to_remote: Do an action with AJAX, 

and put the response into a specified DOM element.

● observe_field / observe_form: Do an action with AJAX when a field or form changes, and put the response into a DOM element.

● periodically_call_remote: Do an action at intervals.

Page 24: Ruby and RubyonRails

AJAX Functions 2● sortable_element: Make an element sortable by the user 

and call an action when it changes.● draggable_element / drop_receiving_element: Drag 

and drop.● visual_effect: Use in a callback to do fades, zooms, etc.

Page 25: Ruby and RubyonRails

AJAX Functions 3● :complete => evaluate_remote_response in your AJAX 

call, and…

● update_javascript_function in your template to update multiple elements with the return from a single AJAX call

Page 26: Ruby and RubyonRails

Web Services RESTful Services

− Uses HTTP itself as transport mechanism

− respond_to helps to convert simple action methods to RESTful action methods

Page 27: Ruby and RubyonRails

IDEs for Rails● Eclipse

− RadRails

● Netbeans

− Netbeans 6 has in built support using JRuby

● CodeGear

Page 28: Ruby and RubyonRails

Summary / Take home

•Ruby is a Very High Level language that supports concepts such as meta programming and reflection and makes using them natural instead of adopting a peripheral approach.

•Rails builds upon the simplicity, flexibility and power of Ruby to deliver a framework that not only provides the complete application stack but also eases the way of working with it.

Page 29: Ruby and RubyonRails

Resources● Main Site                                                           

http://www.rubyonrails.org//

● Start up Tutorial                                                          http://www.onlamp.com/pub/a/onlamp/2005/01/20/rails.html/

● Devarticles Tutorials                                      http://www.devarticles.com/c/b/Ruby­on­Rails/1/author/