0
Ruby On Rails



     A brief tutorial

    Avik Sengupta




   © 2005 Avik Sengupta. All Rights Reserved.   1
Agenda

Introduce Rails via these slides
Show me the code!
  Code up a Rails application from scratch

(This tutorial assu...
Rails

     “Rails is a full-stack, open-source web
   framework in Ruby for writing real-world
applications with joy and ...
What is Rails

Full Stack
  Model (Object Relation Mapping) – Active Record
  View – Action View +ERb
  Controller – Actio...
The Rails request cycle




© 2005 Avik Sengupta. All Rights Reserved.                  5
Active Record

Automated mappings :

Given this table..
      CREATE TABLE entries (
         id int(11) NOT NULL auto_inc...
Active Record
You can do
     class Entry < ActiveRecord::Base
       # nothing here!
     end

     entry = Entry.new
   ...
Active Record

  Associations only need to be declared
class Firm < ActiveRecord::Base
   has_many :clients
   has_one :ac...
Action Controller

Action Controller
  Handles the entire request cycle
  Actions grouped into Controller
  Each action is...
Views

Templates in Embedded Ruby (like JSP or ASP)
Expression language is Pure Ruby
Helper classes provide common functio...
Action Mailer

Integrate e-mails from Rails applications
# send email with attachment
def report_notification(recipient)
 ...
Action Web Services

Server side WS and XML-RPC protocols
Export API's as Web Services




              © 2005 Avik Sengu...
Demo
     Build a web based cookbook, live, from scratch.

  For reference, some of the steps we will go through are
illus...
Downloads and Installation
Install Ruby
   On Windows you can use the One-Click installer. For other
   platforms, use you...
First Steps

Generate Application Structure
$ rails cookbook
Create Database
Generate Models and Controllers
Use scaffold ...
More code...

Flesh out the application
  Create Views
     Use partials and helpers
     Use layouts
  Implement Validati...
Advanced Stuff

Callbacks
Transactions
Caching
Rails makes AJAX easy
  Go to Vamsee Kankala's tutorial
Rails makes testing...
Questions?




Curiosity killed the cat... or did it?




       © 2005 Avik Sengupta. All Rights Reserved.               ...
Credits

This presentation inspired by
  Rolling with Rails (OnLamp) by Curt Hibbs
     http://www.onlamp.com/pub/a/onlamp...
More Info

Main Ruby Site
  http://www.ruby-lang.org
One Click Ruby Installer for Windows
  http://rubyinstaller.rubyforge...
Upcoming SlideShare
Loading in...5
×

Avik_RailsTutorial

252

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
252
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Avik_RailsTutorial"

  1. 1. Ruby On Rails A brief tutorial Avik Sengupta © 2005 Avik Sengupta. All Rights Reserved. 1
  2. 2. Agenda Introduce Rails via these slides Show me the code! Code up a Rails application from scratch (This tutorial assumes a very limited knowledge of Ruby ... did you attend Premshree or Yogi's talks?) © 2005 Avik Sengupta. All Rights Reserved. 2
  3. 3. Rails “Rails is a full-stack, open-source web framework in Ruby for writing real-world applications with joy and less code than most frameworks spend doing XML sit-ups” Rails Creator - David H. Hansson © 2005 Avik Sengupta. All Rights Reserved. 3
  4. 4. What is Rails Full Stack Model (Object Relation Mapping) – Active Record View – Action View +ERb Controller – Action Controller Mail integration – ActionMailer Web Services – Action Web services ..therefore, all layers work seamlessly (DRY) © 2005 Avik Sengupta. All Rights Reserved. 4
  5. 5. The Rails request cycle © 2005 Avik Sengupta. All Rights Reserved. 5
  6. 6. Active Record Automated mappings : Given this table.. CREATE TABLE entries ( id int(11) NOT NULL auto_increment, name varchar(255), comments varchar(255), PRIMARY KEY (id) ); © 2005 Avik Sengupta. All Rights Reserved. 6
  7. 7. Active Record You can do class Entry < ActiveRecord::Base # nothing here! end entry = Entry.new entry.name = "John" entry.comments = "What a great Web site!" entry.save entry = Entry.find(1) puts entry.name # output: "John" © 2005 Avik Sengupta. All Rights Reserved. 7
  8. 8. Active Record Associations only need to be declared class Firm < ActiveRecord::Base has_many :clients has_one :account belongs_to :conglomerate end firm.clients.each do |client| {...} firm.has_clients? © 2005 Avik Sengupta. All Rights Reserved. 8
  9. 9. Action Controller Action Controller Handles the entire request cycle Actions grouped into Controller Each action is method in the controller class Instance variables are passed to the view class GuestbookController < ActionController::Base def index @entries = Entries.find_all end end © 2005 Avik Sengupta. All Rights Reserved. 9
  10. 10. Views Templates in Embedded Ruby (like JSP or ASP) Expression language is Pure Ruby Helper classes provide common functionality for strings, dates, forms etc. <html> <body> <h1>Cookbook</h1> <% for e in @entries %> <p><%= e.name %>: <%= e.description %></p> <% end %> </body> </html> © 2005 Avik Sengupta. All Rights Reserved. 10
  11. 11. Action Mailer Integrate e-mails from Rails applications # send email with attachment def report_notification(recipient) recipients recipient.email_address_with_name subject "Your Report" from "owner@app.com" attachment :content_type => "image/gif", :body => File.read("logo.gif") attachment "image/gif" do |a| a.body = generate_chart() end end © 2005 Avik Sengupta. All Rights Reserved. 11
  12. 12. Action Web Services Server side WS and XML-RPC protocols Export API's as Web Services © 2005 Avik Sengupta. All Rights Reserved. 12
  13. 13. Demo Build a web based cookbook, live, from scratch. For reference, some of the steps we will go through are illustrated in the following slides. However, for the tutorial, we will write the code directly ourselves! © 2005 Avik Sengupta. All Rights Reserved. 13
  14. 14. Downloads and Installation Install Ruby On Windows you can use the One-Click installer. For other platforms, use your platform packages Install RubyGems (a ruby package management system) Download from http://rubyforge.org/projects/rubygems/ Unpack the downloaded archive and in that directory run the command: ruby install.rb Install Rails From the command line execute: From the command line execute: gem install rails © 2005 Avik Sengupta. All Rights Reserved. 14
  15. 15. First Steps Generate Application Structure $ rails cookbook Create Database Generate Models and Controllers Use scaffold to create basic app © 2005 Avik Sengupta. All Rights Reserved. 15
  16. 16. More code... Flesh out the application Create Views Use partials and helpers Use layouts Implement Validations Model complex relationships © 2005 Avik Sengupta. All Rights Reserved. 16
  17. 17. Advanced Stuff Callbacks Transactions Caching Rails makes AJAX easy Go to Vamsee Kankala's tutorial Rails makes testing easy Again, go to Vamsee's tutorial © 2005 Avik Sengupta. All Rights Reserved. 17
  18. 18. Questions? Curiosity killed the cat... or did it? © 2005 Avik Sengupta. All Rights Reserved. 18
  19. 19. Credits This presentation inspired by Rolling with Rails (OnLamp) by Curt Hibbs http://www.onlamp.com/pub/a/onlamp/2005/01/20/rails.html Presentation at Chicago ACM by Ryan Platte and John W. Long http://www.ruby-doc.org/docs/Get to the Point Development with Ru © 2005 Avik Sengupta. All Rights Reserved. 19
  20. 20. More Info Main Ruby Site http://www.ruby-lang.org One Click Ruby Installer for Windows http://rubyinstaller.rubyforge rubyforge.org Main Rails Site http://www.rubyonrails.org This presentation http://www.sengupta.net/talks/ © 2005 Avik Sengupta. All Rights Reserved. 20
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×