ruby and rubyonrails
DESCRIPTION
TRANSCRIPT
Ruby and RubyonRails
17th of November, 2007Hyderabad, India
We appreciate
Outline● Ruby
● RubyonRails
− RubyonRails: What is it?
− MVC Pattern
− Scaffolding
− Active Record
− Action View
− Action Controller
− Rails and Ajax
Outline− Action Mailer
− Web Service
− IDE support
● Conclusion
What is Ruby?● ObjectOriented scripting language
● Conceptual similarities to Smalltalk
● Many system and text manipulation features like Perl
● Emphasis on simplicity and design elegance
Ruby is a Dynamic Language● Completely ObjectOriented
− 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
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
More Language Features● Portable
− OS independent threading
− Can load extension libraries dynamically
● Library support
− RubyGems package manager
− Tons of highquality open source libraries available at RubyForge (similar to CPAN for Perl)
Ruby ToolsThe following tools are included with the Ruby distribution
− debugger
− irb – interactive ruby shell
− benchmark
− profiler
− rdoc
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
What Is Rails?Full Stack Framework
− Includes everything needed to create a databasedriven web application using the ModelViewController pattern.
− Being a fullstack 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.
What is RailsConvention over Configuration
− Rails shuns configuration files in favor of conventions, reflection and dynamic runtime extensions
Configure your application by making it
− Your code and database schema are the configuration!
− No compilation phase● Make a change, see it work.
ModelViewController (MVC)
User Web browser
ControllerViews
View
ModelsModel
1
2
3, 4
5, 6
7
end
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
Rails ModelsActiveRecord Model Classes
− Encapsulate persistence logic
− Contain business rules
− Tightly coupled to database tables
− Declare relationships to each other
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)
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
ERB Template Example
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
Controller Basics● Extend ActionController:Base
● Request parameters in ‘params’
− Naming conventions mean Rails can translate paramaters into a hashtable
− Handles multidimensional data in forms pretty easily● Web session in ‘session’ hash
● Redirect scope available in ‘flash’ hash for next request only
AJAX<head>
<%= javascript_include_tag :defaults %>
</head>
Inserts JavaScript AJAX libraries supplied with every Rails application. No additional scripting is necessary to use Rails AJAXbased functions.
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.
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.
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
Web Services RESTful Services
− Uses HTTP itself as transport mechanism
− respond_to helps to convert simple action methods to RESTful action methods
IDEs for Rails● Eclipse
− RadRails
● Netbeans
− Netbeans 6 has in built support using JRuby
● CodeGear
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.
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/RubyonRails/1/author/