Web Development with Ruby on Rails, MyGOSSCON 2007

4,606 views

Published on

I prepared these slides for my talk at the Malaysian Government OSS Conference 2007 at the Putrajaya International Convention Center from 6-7th December. It's not comprehensive, more meant to whet the appetite of people new to it. The examples are very Rails 1.2.x-ish because it's not worth explaining REST yet :)

Published in: Technology
2 Comments
1 Like
Statistics
Notes
No Downloads
Views
Total views
4,606
On SlideShare
0
From Embeds
0
Number of Embeds
47
Actions
Shares
0
Downloads
189
Comments
2
Likes
1
Embeds 0
No embeds

No notes for slide

Web Development with Ruby on Rails, MyGOSSCON 2007

  1. 1. Web Development with Ruby on Rails MyGOSSCON 2007 Kamal Fariz Mahyuddin RSB
  2. 2. A full-stack framework for developing database-backed web applications according to the Model-View-Controller pattern.
  3. 3. A full-stack framework for developing database-backed web applications according to the Model-View-Controller A bunch of stuff that makes web developers happy! pattern. - David Heinemeier Hansson http://flickr.com/photos/pragdave/174964316/
  4. 4. 2004 6 7 5
  5. 5. > 1 million downloads
  6. 6. Hundreds of plugins
  7. 7. ~10k people on rubyonrails-talk
  8. 8. DB2 SybaseASA Informix Firebird MySQL SQLite Oracle PostgreSQL SQL Server
  9. 9. Ruby on Rails Model-View-Controller
  10. 10. MVC is like a bakery shop.
  11. 11. ActionController Model-View-Controller
  12. 12. ActiveRecord Model-View-Controller
  13. 13. ActionView Model-View-Controller
  14. 14. ActiveRecord Model-View-Controller
  15. 15. class Department < ActiveRecord::Base belongs_to :ministry has_many :officers has_many :projects end File: apps/models/department.rb
  16. 16. departments id name ministry_id 1 Jabatan Lanskap dan Taman 1 2 Jabatan Pembangunan Bandar 1 3 Jabatan Imigresen Malaysia 2
  17. 17. ministries id name 1 Kementerian Wilayah Persekutuan 2 Kementerian Hal Ehwal Dalam Negeri
  18. 18. dept = Department.find(1) => #<Department id: 1, name: 'Jabatan Lanskap dan Taman'> SELECT * FROM departments WHERE id = 1
  19. 19. dept.ministry => #<Ministry id: 1, name: 'Kementerian Wilayah Persekutuan'> SELECT * FROM ministries WHERE id = 1
  20. 20. class Ministry < ActiveRecord::Base has_many :departments end File: apps/models/ministry.rb
  21. 21. ministries id name 1 Kementerian Wilayah Persekutuan 2 Kementerian Hal Ehwal Dalam Negeri
  22. 22. departments id name ministry_id 1 Jabatan Lanskap dan Taman 1 2 Jabatan Pembangunan Bandar 1 3 Jabatan Imigresen Malaysia 2
  23. 23. ministry = Ministry.find(1) => #<Ministry id: 1, name: 'Kementerian Wilayah Persekutuan'> ministry.departments => [#<Department id: 1, name: 'Jabatan Lanskap dan Taman'>, #<Department id: 2, name: 'Jabatan Pembangunan Bandar'>] SELECT * FROM departments WHERE ministry_id = 1
  24. 24. Convention over Configuration
  25. 25. ActiveRecord Conventions Model names are singular, e.g. Ministry. Table names are plural, e.g. ministries. Foreign-key columns are named model_id, e.g. ministry_id. Foreign key column goes onto the model that belongs_to another model, e.g. departments table has a column named ministry_id.
  26. 26. ActionController Model-View-Controller
  27. 27. http://mampu.gov.my/departments/show/1
  28. 28. http://mampu.gov.my/departments/show/1 http://mampu.gov.my/controller/action/id
  29. 29. /departments/show/1 /controller/action/id class DepartmentsController < ApplicationController def show @dept = Department.find(params[:id]) end end File: apps/controllers/departments_controller.rb
  30. 30. Remember this? Department.find(1)
  31. 31. /departments/show/1 /controller/action/id class DepartmentsController < ApplicationController def show @dept = Department.find(params[:id]) end end File: apps/controllers/departments_controller.rb
  32. 32. Convention over Configuration
  33. 33. ActionController Conventions First segment of URL is the controller name, e.g. /departments/show/1, the controller is DepartmentsController. Second segment of URL is the controller action, e.g. /departments/show/1, the action is show in DepartmentsController. Third segment of URL is the ID, e.g. /departments/show/1, the value is accessible from params[:id].
  34. 34. ActionView Model-View-Controller
  35. 35. Remember this? class DepartmentsController < ApplicationController def show @dept = Department.find(params[:id]) end end
  36. 36. http://localhost/departments/show/1 Department: Jabatan Lanskap dan Taman Ministry: Kementerian Wilayah Persekutuan Projects: • Next Generation Network • e-Perolehan Integration
  37. 37. <html> <body> <h1>Department: <%= @dept.name %></h1> <h2>Ministry: <%= @dept.ministry.name %></h2> <h2>Projects</h2> <ul> <%= render :partial => @dept.projects %> </ul> </body> </html> File: apps/views/departments/show.html.erb
  38. 38. <li><%= project.title %></li> File: apps/views/projects/_project.html.erb
  39. 39. http://localhost/departments/show/1 Department: Jabatan Lanskap dan Taman Ministry: Kementerian Wilayah Persekutuan Projects: • Next Generation Network • e-Perolehan Integration
  40. 40. Convention over Configuration
  41. 41. ActionView Conventions Instance variables set in controller action are automatically available in view, e.g. @dept in previous example. Values are displayed in view by enclosing it in <%= %>. Loops can be extracted into partials. Use Helpers as much as possible to code even less, e.g. link_to, form_for
  42. 42. Resources
  43. 43. Malaysia.rb Malaysia Ruby Brigade
  44. 44. ~65 people in Google Groups ~28 people in Facebook Group
  45. 45. Meets every third Thursday of the month
  46. 46. Malaysia.rb 3rd Meetup July 2007
  47. 47. http://groups.google.com/group/malaysia-rb
  48. 48. Local Companies on Rails
  49. 49. RSB Freelance Developers
  50. 50. Local Ruby on Rails Sites
  51. 51. http://www.mampu.gov.my http://www.ravejoint.com http://www.geotude.com http://www.haze.net.my
  52. 52. Sneak Preview of Time Together
  53. 53. Your Department’s Next Internal Web Application

×