Ruby on Rails Presentation

5,482 views

Published on

A partial presentation about Ruby on Rails which was made in 2008/9 ish

Published in: Technology

Ruby on Rails Presentation

  1. 1. Ruby on RailsA Complete Introduction
  2. 2. Good Morning Welcome to Carsonified . a ve.. is is DTh Hi there!
  3. 3. Who am I? Adam Cooke I work at... which is part of ...I have developed and lots of other stuf f
  4. 4. ... and you are?
  5. 5. So, the plan...
  6. 6. IntroductionThe Rails BasicsBuilding a Blogging EngineMore AdvancementTestingWhen things go wrong!DeploymentsFinishing up
  7. 7. re ... a re he you1 Introduction What is Rails? The MVC Pattern Ruby Overview RubyGems Installing Rails Components of Rails
  8. 8. What is Rails?
  9. 9. David Heinemeier Hansson aka DHH & the res t of the R ails Core Team
  10. 10. [title] [sub title]
  11. 11. Who’s using Rails?
  12. 12. The MVC Pattern Model-view-controller
  13. 13. ControllerModel View
  14. 14. Rails routing happens here Controller Model ViewDatabase Resource Return to the browser
  15. 15. Ruby
  16. 16. SimpleEasy to write Elegant
  17. 17. Everything is an object Module String Hash Array Proc Fixnum Symbol Numeric
  18. 18. class Numeric def plus(x) self.+(x) endendy = 5.plus 10 #=> 15
  19. 19. 5.times { puts “Hello!” }
  20. 20. Ruby Objects
  21. 21. Variables For exampleAny plain, lowercase word a, my_variable and banana10 out...Try it>> blahNameError: undefined local variable or method `blah>> string = “Hello World!”=> “Hello World!”>> string=> “Hello World!”
  22. 22. Numbers For exampleIntegers - positive or negative 1, 41231 and -68835 out...Try it>> 5 + 10=> 15>> 10 * 10=> 100>> 3.1 + 1.55=> 4.65
  23. 23. Strings For exampleAnything 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!”
  24. 24. Symbols For exampleStart with a colon, look like words :a, :first_name and :abc123 out...Try it>> my_symbol = :complete=> :complete>> my_symbol=> :complete
  25. 25. Constants For exampleLike 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
  26. 26. Methods For exampleThe verbs! say_hello and close out...Try it>> def say_hello>> puts “Hello!”>> end>> say_helloHello!=> nil
  27. 27. Method Args For examplePassing 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
  28. 28. Method Args For examplePassing 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
  29. 29. Arrays For exampleA 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]
  30. 30. Hashes For exampleA 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’]
  31. 31. The Big One...
  32. 32. ClassesAnatomy of a classclass Person attr_accessor :first_name, :last_nameend p = Person.new p.first_name = ‘Dave’ p.last_name = ‘Jones’ p.first_name #=> “Dave”
  33. 33. 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(“ ”) endend p = Person.new(‘Dave’, ‘Jones’) p.first_name #=> “Dave” p.last_name #=> “Jones” p.full_name #=> “Dave Jones”
  34. 34. Ruby GemsYour Ruby Package Manager
  35. 35. 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)
  36. 36. gem install railsgem remove railsgem update rails
  37. 37. Useful Gems The Rails Gemsrails actionmailer actionpackmongrel_cluster activerecordcapistrano activeresourcemysql activesupport rails rake
  38. 38. Components of Rails Action Pack Active Support Active Record Action Mailer Active Resource
  39. 39. Action PackAll the view & controller logic
  40. 40. Active SupportCollection of utility classes and library extensions
  41. 41. Active Record The object relationship mapper
  42. 42. Action Mailer E-Mail Delivery
  43. 43. 1 Introduction What is Rails? The MVC Pattern Ruby Overview RubyGems Installing Rails Components of Rails ... are here you
  44. 44. re ... a re he you2 The Rails Basics Development Tools & Environment Generating an Application The Directory Structure Starting up the app “RESTful Rails” Routing & URLs
  45. 45. Active Resource Connect with REST web services
  46. 46. Editors & IDEs
  47. 47. Database Browsers
  48. 48. Generating an App. rails my_app_namerails my_app_name -d mysql
  49. 49. app Contains the majority of your application specific codeconfig Application config - routing map, database config etc...db Database schema, SQLite database files & migrationsdoc Generated HTML API documentation for the application or Railslib Application-specific libraries - anything which doesn’t belong in app/log Log files and web server PID filespublic Your webserver document root - contains images, JS, CSS etc...script Rails helper scripts for automation and generationtest Unit & functional tests along with any fixturestmp Application specific temporary filesvendor External libraries used in the application - gems, plugins etc...
  50. 50. app controllers Controllers named as posts_controller.rbconfig helpers View helpers named as posts_helper.rbdb models Models named as post.rbdoc views Controller template files named as posts/index.html.erb for thelib PostsController#index actionlog views/layouts Layout template files in the format of application.html.erb for anpublic application wide layout or posts.html.erbscript for controller specific layouts.testtmpvendor
  51. 51. Starting the App Running a Local Webserver script/server
  52. 52. “RESTful Rails” Representational State Transfer
  53. 53. HTTP MethodsGET POST PUT DELETEREAD CREATE UPDATE DESTROY
  54. 54. Resource: Customer/customers GET index POST create/customers/1234 GET show PUT update DELETE destroy/customers/new GET new/customers/1234/edit GET edit
  55. 55. Routing & URLs config/routes.rb
  56. 56. domain.com/my-pagemap.connect “my-page”, :controller => “pages”, :action => “my”domain.com/customers (as a resource)map.resources :customersdomain.com (the root domain)map.root :controller => “pages”, :action => “homepage”domain.com/pages/aboutmap.connect “pages/:action”, :controller => “pages”domain.com/pages/about/123map.connect “:controller/:action/:id”
  57. 57. Named Routes rake routes
  58. 58. 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
  59. 59. 2 The Rails Basics Development Tools & Environment Generating an Application The Directory Structure Starting up the app “RESTful Rails” Routing & URLs ... are here you

×