SlideShare a Scribd company logo
1 of 60
Ruby on Rails
A Complete Introduction
Good Morning
                                Welcome to Carsonified

                .
         a ve..
  is is D
Th


                    Hi there!
Who am I?
                         Adam Cooke


       I work at...               which is part of




                 ...
I have developed

                                                     and lots of other stuf
                                                                            f
... and you are?
So, the plan...
Introduction
The Rails Basics
Building a Blogging Engine
More Advancement
Testing
When things go wrong!
Deployments
Finishing up
re   ...
                         a re he
                   you

1   Introduction            What is Rails?
                            The MVC Pattern
                            Ruby Overview
                            RubyGems
                            Installing Rails
                            Components of Rails
What is Rails?
David Heinemeier Hansson
                  aka DHH
          & the res
                    t of the R
                               ails Core
                                         Team
[title]
 [sub title]
Who’s using Rails?
The MVC Pattern
   Model-view-controller
Controller




Model                View
Rails routing happens here
                      Controller




      Model                                       View


Database   Resource
                                   Return to the browser
Ruby
Simple
Easy to write

          Elegant
Everything is an object
 Module                String
            Hash

    Array                 Proc
              Fixnum
 Symbol                 Numeric
class Numeric
    def plus(x)
        self.+(x)
    end
end

y = 5.plus 10 #=> 15
5.times { puts “Hello!” }
Ruby Objects
Variables                                  For example

Any plain, lowercase word                  a, my_variable and banana10


       out...
Try it


>> blah
NameError: undefined local variable or method `blah'

>>    string = “Hello World!”
=>    “Hello World!”
>>    string
=>    “Hello World!”
Numbers                           For example

Integers - positive or negative   1, 41231 and
                                  -68835

       out...
Try it

>>    5 + 10
=>    15
>>    10 * 10
=>    100
>>    3.1 + 1.55
=>    4.65
Strings                         For example

Anything surrounded by quotes   “Dave”, “123”and “My name
                                is...”

       out...
Try it

>>    my_quote = “My name is Dave!”
=>    “My name is Dave!”
>>    my_quote
=>    “My name is Dave!”
Symbols                               For example

Start with a colon, look like words   :a, :first_name and :abc123


       out...
Try it

>>    my_symbol = :complete
=>    :complete
>>    my_symbol
=>    :complete
Constants                                      For example

Like variables, with a capital                Hash, Monkey and Dave_The_Frog


       out...
Try it

>> MyMonkey = “James”
                                               Yo
                                                 us
=> “James”                                              hou
                                                              ldn
                                                                    ’t
>> MyMonkey = “Michael”                                                  ch
                                                                           an
(irb):1: warning: already initialized constant MyMonkey                         ge
                                                                                   it,a
=> “Michael”                                                                            ft
                                                                                          er
                                                                                             it   ’s
                                                                                                       be
                                                                                                         en
                                                                                                              se
                                                                                                                t
Methods            For example

The verbs!         say_hello and close


       out...
Try it

>> def say_hello
>> puts “Hello!”
>> end
>> say_hello
Hello!
=> nil
Method Args               For example

Passing data to methods   say_hello(name)


       out...
Try it

>> def say_hello(name, age)
>> puts “Hello #{name}!”
>> puts “You are #{age}!”
>> end
>> say_hello(‘Keir’, 45)
Hello Keir!
You are 45!
=> nil
Method Args               For example

Passing data to methods   say_hello(name)


       out...
Try it

>> def say_hello(name, age)
>> puts “Hello #{name}!”
>> puts “You are #{age}!”
>> end
>> say_hello(‘Keir’, 45)
Hello Keir!
You are 30!
=> nil
Arrays                                 For example

A list surrounded by square brackets   [1,2,3] and [‘A’,‘B’,‘C’]


       out...
Try it

>>    a = [1,2,3,4,5]
=>    [1,2,3,4,5]
>>    a
=>    [1,2,3,4,5]
>>    a[1]
=>    2
>>    a[1, 3]
=>    [2,3,4]
Hashes                              For example

A list surrounded by curly braces   {1=>2, 3=>4} and
                                    {:a => ‘Ant’,
                                     :b => ‘Badger’}
       out...
Try it

>>    h = {:a => ‘Good’, :b => ‘Bad’}
=>    {:a => ‘Good’, :b => ‘Bad’}
>>    h(:a)
=>    ‘Good’
>>    h.keys
=>    [:a, :b]
>>    h.values
=>    [‘Good’, ‘Bad’]
The Big One...
Classes
Anatomy of a class
class Person
    attr_accessor :first_name, :last_name
end

             p = Person.new
             p.first_name = ‘Dave’
             p.last_name = ‘Jones’
             p.first_name #=> “Dave”
class Person
   attr_accessor :first_name, :last_name

      def initialize(first, last)
          self.first_name = first
          self.last_name = last
      end

      def full_name
         [self.first_name, self.last_name].join(“ ”)
      end
end
                p = Person.new(‘Dave’, ‘Jones’)
                p.first_name #=> “Dave”
                p.last_name   #=> “Jones”
                p.full_name   #=> “Dave Jones”
Ruby Gems
Your Ruby Package Manager
user@dev01:~#                          gem list
*** LOCAL GEMS ***

abstract (1.0.0)
actionmailer (2.1.0, 2.0.2, 1.3.6, 1.3.3)
actionpack (2.1.0, 2.0.2, 1.13.6, 1.13.3)
actionwebservice (1.2.6, 1.2.3)
activerecord (2.1.0, 2.0.2, 1.15.6, 1.15.3)
activeresource (2.1.0, 2.0.2)
activesupport (2.1.0, 2.0.2, 1.4.4, 1.4.2)
acts_as_ferret (0.4.1)
aws-s3 (0.4.0)
builder (2.1.2)
capistrano (2.3.0, 1.4.0)
cgi_multipart_eof_fix (2.5.0, 2.2)
cheat (1.2.1)
chronic (0.2.3)
codebase-gem (1.0.3)
daemons (1.0.10, 1.0.9, 1.0.7)
dnssd (0.6.0)
erubis (2.5.0)
gem install rails
gem remove rails
gem update rails
Useful Gems
                   The Rails Gems

rails              actionmailer
                    actionpack
mongrel_cluster    activerecord
capistrano        activeresource
mysql             activesupport
                       rails
                       rake
Components of Rails
        Action Pack
      Active Support
       Active Record
       Action Mailer
      Active Resource
Action Pack
All the view & controller logic
Active Support
Collection of utility classes and library extensions
Active Record
 The object relationship mapper
Action Mailer
   E-Mail Delivery
1   Introduction   What is Rails?
                   The MVC Pattern
                   Ruby Overview
                   RubyGems
                   Installing Rails
                   Components of Rails          ...
                                        are here
                                  you
re   ...
                          a re he
                    you

2   The Rails Basics Development Tools & Environment
                     Generating an Application
                     The Directory Structure
                     Starting up the app
                     “RESTful Rails”
                     Routing & URLs
Active Resource
  Connect with REST web services
Editors & IDEs
Database Browsers
Generating an App.
    rails my_app_name

rails my_app_name -d mysql
app      Contains the majority of your application specific code
config   Application config - routing map, database config etc...
db       Database schema, SQLite database files & migrations
doc      Generated HTML API documentation for the application or Rails
lib      Application-specific libraries - anything which doesn’t belong in app/
log      Log files and web server PID files
public   Your webserver document root - contains images, JS, CSS etc...
script   Rails helper scripts for automation and generation
test     Unit & functional tests along with any fixtures
tmp      Application specific temporary files
vendor   External libraries used in the application - gems, plugins etc...
app      controllers     Controllers named as posts_controller.rb
config   helpers         View helpers named as posts_helper.rb
db       models          Models named as post.rb
doc      views           Controller template files named as
                         posts/index.html.erb for the
lib                      PostsController#index action
log      views/layouts   Layout template files in the format of
                         application.html.erb for an
public                   application wide layout or posts.html.erb
script                   for controller specific layouts.
test
tmp
vendor
Starting the App
   Running a Local Webserver

   script/server
“RESTful Rails”
 Representational State Transfer
HTTP Methods
GET     POST     PUT      DELETE


READ    CREATE   UPDATE   DESTROY
Resource: Customer

/customers             GET   index   POST   create




/customers/1234        GET   show    PUT    update   DELETE   destroy



/customers/new         GET   new




/customers/1234/edit   GET   edit
Routing & URLs
    config/routes.rb
domain.com/my-page
map.connect “my-page”, :controller => “pages”, :action => “my”


domain.com/customers (as a resource)
map.resources :customers


domain.com (the root domain)
map.root :controller => “pages”, :action => “homepage”


domain.com/pages/about
map.connect “pages/:action”, :controller => “pages”


domain.com/pages/about/123
map.connect “:controller/:action/:id”
Named Routes
    rake routes
URL Helpers can use named routes (link_to, form for...)
<%=link_to ‘Homepage’, root_path%>


<%=link_to ‘Customer List’, customers_path%>


<%=link_to ‘View this Customer’, customer_path(1234)%>


<%=link_to ‘Edit this Customer’, edit_customer_path(1234)%>


<%form_for :customer, :url => customers_path do |f|...%>


                                           A POST request - so will call the ‘create’ action
2   The Rails Basics Development Tools & Environment
                     Generating an Application
                     The Directory Structure
                     Starting up the app
                     “RESTful Rails”
                     Routing & URLs
                                                 ...
                                         are here
                                   you

More Related Content

What's hot

What's hot (20)

Introduction to Ruby on Rails
Introduction to Ruby on RailsIntroduction to Ruby on Rails
Introduction to Ruby on Rails
 
Introduction to java
Introduction to javaIntroduction to java
Introduction to java
 
JavaScript Tutorial
JavaScript  TutorialJavaScript  Tutorial
JavaScript Tutorial
 
Php technical presentation
Php technical presentationPhp technical presentation
Php technical presentation
 
Ruby Presentation
Ruby Presentation Ruby Presentation
Ruby Presentation
 
Angularjs PPT
Angularjs PPTAngularjs PPT
Angularjs PPT
 
Angular js PPT
Angular js PPTAngular js PPT
Angular js PPT
 
ASP.NET MVC Presentation
ASP.NET MVC PresentationASP.NET MVC Presentation
ASP.NET MVC Presentation
 
Introduction to Node.js
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.js
 
React JS - A quick introduction tutorial
React JS - A quick introduction tutorialReact JS - A quick introduction tutorial
React JS - A quick introduction tutorial
 
Introduction to React JS
Introduction to React JSIntroduction to React JS
Introduction to React JS
 
Tomcat
TomcatTomcat
Tomcat
 
Node.js Basics
Node.js Basics Node.js Basics
Node.js Basics
 
React js
React jsReact js
React js
 
Php mysql ppt
Php mysql pptPhp mysql ppt
Php mysql ppt
 
Php with MYSQL Database
Php with MYSQL DatabasePhp with MYSQL Database
Php with MYSQL Database
 
React workshop
React workshopReact workshop
React workshop
 
Ppt full stack developer
Ppt full stack developerPpt full stack developer
Ppt full stack developer
 
Event In JavaScript
Event In JavaScriptEvent In JavaScript
Event In JavaScript
 
ReactJS presentation.pptx
ReactJS presentation.pptxReactJS presentation.pptx
ReactJS presentation.pptx
 

Similar to Ruby on Rails Presentation

Ruby -the wheel Technology
Ruby -the wheel TechnologyRuby -the wheel Technology
Ruby -the wheel Technology
ppparthpatel123
 
Ruby 程式語言入門導覽
Ruby 程式語言入門導覽Ruby 程式語言入門導覽
Ruby 程式語言入門導覽
Wen-Tien Chang
 

Similar to Ruby on Rails Presentation (20)

Ruby from zero to hero
Ruby from zero to heroRuby from zero to hero
Ruby from zero to hero
 
An introduction to Ruby
An introduction to RubyAn introduction to Ruby
An introduction to Ruby
 
Introduction to CoffeeScript
Introduction to CoffeeScriptIntroduction to CoffeeScript
Introduction to CoffeeScript
 
Fog City Ruby - Triple Equals Black Magic with speaker notes
Fog City Ruby - Triple Equals Black Magic with speaker notesFog City Ruby - Triple Equals Black Magic with speaker notes
Fog City Ruby - Triple Equals Black Magic with speaker notes
 
Learning Ruby
Learning RubyLearning Ruby
Learning Ruby
 
Code for Startup MVP (Ruby on Rails) Session 2
Code for Startup MVP (Ruby on Rails) Session 2Code for Startup MVP (Ruby on Rails) Session 2
Code for Startup MVP (Ruby on Rails) Session 2
 
Test First Teaching
Test First TeachingTest First Teaching
Test First Teaching
 
Ruby -the wheel Technology
Ruby -the wheel TechnologyRuby -the wheel Technology
Ruby -the wheel Technology
 
Introduction to Ruby
Introduction to RubyIntroduction to Ruby
Introduction to Ruby
 
Ruby: Beyond the Basics
Ruby: Beyond the BasicsRuby: Beyond the Basics
Ruby: Beyond the Basics
 
Ruby and rails - Advanced Training (Cybage)
Ruby and rails - Advanced Training (Cybage)Ruby and rails - Advanced Training (Cybage)
Ruby and rails - Advanced Training (Cybage)
 
Ruby
RubyRuby
Ruby
 
Ruby — An introduction
Ruby — An introductionRuby — An introduction
Ruby — An introduction
 
Ruby 程式語言入門導覽
Ruby 程式語言入門導覽Ruby 程式語言入門導覽
Ruby 程式語言入門導覽
 
Playfulness at Work
Playfulness at WorkPlayfulness at Work
Playfulness at Work
 
A linguagem de programação Ruby - Robson "Duda" Sejan Soares Dornelles
A linguagem de programação Ruby - Robson "Duda" Sejan Soares DornellesA linguagem de programação Ruby - Robson "Duda" Sejan Soares Dornelles
A linguagem de programação Ruby - Robson "Duda" Sejan Soares Dornelles
 
Ruby Programming Language
Ruby Programming LanguageRuby Programming Language
Ruby Programming Language
 
Ruby
RubyRuby
Ruby
 
A limited guide to intermediate and advanced Ruby
A limited guide to intermediate and advanced RubyA limited guide to intermediate and advanced Ruby
A limited guide to intermediate and advanced Ruby
 
Rails OO views
Rails OO viewsRails OO views
Rails OO views
 

Recently uploaded

Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Recently uploaded (20)

AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 

Ruby on Rails Presentation

  • 1. Ruby on Rails A Complete Introduction
  • 2. Good Morning Welcome to Carsonified . a ve.. is is D Th Hi there!
  • 3. Who am I? Adam Cooke I work at... which is part of ... I have developed and lots of other stuf f
  • 4. ... and you are?
  • 6. Introduction The Rails Basics Building a Blogging Engine More Advancement Testing When things go wrong! Deployments Finishing up
  • 7. re ... a re he you 1 Introduction What is Rails? The MVC Pattern Ruby Overview RubyGems Installing Rails Components of Rails
  • 9. David Heinemeier Hansson aka DHH & the res t of the R ails Core Team
  • 12.
  • 13. The MVC Pattern Model-view-controller
  • 15. Rails routing happens here Controller Model View Database Resource Return to the browser
  • 16. Ruby
  • 18. Everything is an object Module String Hash Array Proc Fixnum Symbol Numeric
  • 19. class Numeric def plus(x) self.+(x) end end y = 5.plus 10 #=> 15
  • 20. 5.times { puts “Hello!” }
  • 22. Variables For example Any plain, lowercase word a, my_variable and banana10 out... Try it >> blah NameError: undefined local variable or method `blah' >> string = “Hello World!” => “Hello World!” >> string => “Hello World!”
  • 23. Numbers For example Integers - positive or negative 1, 41231 and -68835 out... Try it >> 5 + 10 => 15 >> 10 * 10 => 100 >> 3.1 + 1.55 => 4.65
  • 24. Strings For example Anything surrounded by quotes “Dave”, “123”and “My name is...” out... Try it >> my_quote = “My name is Dave!” => “My name is Dave!” >> my_quote => “My name is Dave!”
  • 25. Symbols For example Start with a colon, look like words :a, :first_name and :abc123 out... Try it >> my_symbol = :complete => :complete >> my_symbol => :complete
  • 26. Constants For example Like variables, with a capital Hash, Monkey and Dave_The_Frog out... Try it >> MyMonkey = “James” Yo us => “James” hou ldn ’t >> MyMonkey = “Michael” ch an (irb):1: warning: already initialized constant MyMonkey ge it,a => “Michael” ft er it ’s be en se t
  • 27. Methods For example The verbs! say_hello and close out... Try it >> def say_hello >> puts “Hello!” >> end >> say_hello Hello! => nil
  • 28. Method Args For example Passing data to methods say_hello(name) out... Try it >> def say_hello(name, age) >> puts “Hello #{name}!” >> puts “You are #{age}!” >> end >> say_hello(‘Keir’, 45) Hello Keir! You are 45! => nil
  • 29. Method Args For example Passing data to methods say_hello(name) out... Try it >> def say_hello(name, age) >> puts “Hello #{name}!” >> puts “You are #{age}!” >> end >> say_hello(‘Keir’, 45) Hello Keir! You are 30! => nil
  • 30. Arrays For example A list surrounded by square brackets [1,2,3] and [‘A’,‘B’,‘C’] out... Try it >> a = [1,2,3,4,5] => [1,2,3,4,5] >> a => [1,2,3,4,5] >> a[1] => 2 >> a[1, 3] => [2,3,4]
  • 31. Hashes For example A list surrounded by curly braces {1=>2, 3=>4} and {:a => ‘Ant’, :b => ‘Badger’} out... Try it >> h = {:a => ‘Good’, :b => ‘Bad’} => {:a => ‘Good’, :b => ‘Bad’} >> h(:a) => ‘Good’ >> h.keys => [:a, :b] >> h.values => [‘Good’, ‘Bad’]
  • 33. Classes Anatomy of a class class Person attr_accessor :first_name, :last_name end p = Person.new p.first_name = ‘Dave’ p.last_name = ‘Jones’ p.first_name #=> “Dave”
  • 34. class Person attr_accessor :first_name, :last_name def initialize(first, last) self.first_name = first self.last_name = last end def full_name [self.first_name, self.last_name].join(“ ”) end end p = Person.new(‘Dave’, ‘Jones’) p.first_name #=> “Dave” p.last_name #=> “Jones” p.full_name #=> “Dave Jones”
  • 35. Ruby Gems Your Ruby Package Manager
  • 36. user@dev01:~# gem list *** LOCAL GEMS *** abstract (1.0.0) actionmailer (2.1.0, 2.0.2, 1.3.6, 1.3.3) actionpack (2.1.0, 2.0.2, 1.13.6, 1.13.3) actionwebservice (1.2.6, 1.2.3) activerecord (2.1.0, 2.0.2, 1.15.6, 1.15.3) activeresource (2.1.0, 2.0.2) activesupport (2.1.0, 2.0.2, 1.4.4, 1.4.2) acts_as_ferret (0.4.1) aws-s3 (0.4.0) builder (2.1.2) capistrano (2.3.0, 1.4.0) cgi_multipart_eof_fix (2.5.0, 2.2) cheat (1.2.1) chronic (0.2.3) codebase-gem (1.0.3) daemons (1.0.10, 1.0.9, 1.0.7) dnssd (0.6.0) erubis (2.5.0)
  • 37. gem install rails gem remove rails gem update rails
  • 38. Useful Gems The Rails Gems rails actionmailer actionpack mongrel_cluster activerecord capistrano activeresource mysql activesupport rails rake
  • 39. Components of Rails Action Pack Active Support Active Record Action Mailer Active Resource
  • 40. Action Pack All the view & controller logic
  • 41. Active Support Collection of utility classes and library extensions
  • 42. Active Record The object relationship mapper
  • 43. Action Mailer E-Mail Delivery
  • 44. 1 Introduction What is Rails? The MVC Pattern Ruby Overview RubyGems Installing Rails Components of Rails ... are here you
  • 45. re ... a re he you 2 The Rails Basics Development Tools & Environment Generating an Application The Directory Structure Starting up the app “RESTful Rails” Routing & URLs
  • 46. Active Resource Connect with REST web services
  • 49. Generating an App. rails my_app_name rails my_app_name -d mysql
  • 50. app Contains the majority of your application specific code config Application config - routing map, database config etc... db Database schema, SQLite database files & migrations doc Generated HTML API documentation for the application or Rails lib Application-specific libraries - anything which doesn’t belong in app/ log Log files and web server PID files public Your webserver document root - contains images, JS, CSS etc... script Rails helper scripts for automation and generation test Unit & functional tests along with any fixtures tmp Application specific temporary files vendor External libraries used in the application - gems, plugins etc...
  • 51. app controllers Controllers named as posts_controller.rb config helpers View helpers named as posts_helper.rb db models Models named as post.rb doc views Controller template files named as posts/index.html.erb for the lib PostsController#index action log views/layouts Layout template files in the format of application.html.erb for an public application wide layout or posts.html.erb script for controller specific layouts. test tmp vendor
  • 52. Starting the App Running a Local Webserver script/server
  • 54. HTTP Methods GET POST PUT DELETE READ CREATE UPDATE DESTROY
  • 55. Resource: Customer /customers GET index POST create /customers/1234 GET show PUT update DELETE destroy /customers/new GET new /customers/1234/edit GET edit
  • 56. Routing & URLs config/routes.rb
  • 57. domain.com/my-page map.connect “my-page”, :controller => “pages”, :action => “my” domain.com/customers (as a resource) map.resources :customers domain.com (the root domain) map.root :controller => “pages”, :action => “homepage” domain.com/pages/about map.connect “pages/:action”, :controller => “pages” domain.com/pages/about/123 map.connect “:controller/:action/:id”
  • 58. Named Routes rake routes
  • 59. URL Helpers can use named routes (link_to, form for...) <%=link_to ‘Homepage’, root_path%> <%=link_to ‘Customer List’, customers_path%> <%=link_to ‘View this Customer’, customer_path(1234)%> <%=link_to ‘Edit this Customer’, edit_customer_path(1234)%> <%form_for :customer, :url => customers_path do |f|...%> A POST request - so will call the ‘create’ action
  • 60. 2 The Rails Basics Development Tools & Environment Generating an Application The Directory Structure Starting up the app “RESTful Rails” Routing & URLs ... are here you