Introduction to Ruby on Rails

11,834 views

Published on

An introduction to the RoR framework prepared for the Kraków Ruby Users Group (http://www.ruby.org.pl)

Published in: Technology
5 Comments
31 Likes
Statistics
Notes
No Downloads
Views
Total views
11,834
On SlideShare
0
From Embeds
0
Number of Embeds
113
Actions
Shares
0
Downloads
785
Comments
5
Likes
31
Embeds 0
No embeds

No notes for slide

Introduction to Ruby on Rails

  1. 1. Introduction to Ruby on Rails Agnieszka Figiel blog.agnessa.eu Kraków Ruby Users Group May 19th 2007
  2. 2. Design Principles MVC architecture Agile Development Usability and Success Stories Community and Resources Ruby on Rails quot;Rails is a full-stack framework for developing database-backed web applications according to the Model-View-Control pattern.quot; www.rubyonrails.org - Ruby on Rails official site 2005 David Heinemeier Hansson opinionated software: quot;it’s a very pragmatic, very targeted framework with a strong sense of direction. You might not share its vision, but it undeniably has one.quot; DHH for Linux Journal (http://www.linuxjournal.com/article/8686) Introduction to Ruby on Rails
  3. 3. Design Principles MVC architecture Agile Development Usability and Success Stories Community and Resources Outline Design Principles MVC architecture Agile Development Usability and Success Stories Community and Resources Introduction to Ruby on Rails
  4. 4. Design Principles MVC architecture Agile Development Usability and Success Stories Community and Resources Design Principles MVC architecture Agile Development Usability and Success Stories Community and Resources Introduction to Ruby on Rails
  5. 5. Design Principles MVC architecture Agile Development Usability and Success Stories Community and Resources Model - View - Controller separate data (model) from user interface (view) Model data access and business logic independent of the view and controller View data presentation and user interaction read-only access to the model Controller handling events operating on model and view Introduction to Ruby on Rails
  6. 6. Design Principles MVC architecture Agile Development Usability and Success Stories Community and Resources Database Persistance OR mapping Active Record design pattern migrations incremental schema management multiple db adapters MySQL, PostgreSQL, SQLite, SQL Server, IBM DB2, Informix, Oracle, Firebird/Interbase, LDAP, SybaseASA Introduction to Ruby on Rails
  7. 7. Design Principles MVC architecture Agile Development Usability and Success Stories Community and Resources Full Stack Framework MVC suite built-in webserver default db adapter integrated logger AJAX, web services, email test framework plugins Introduction to Ruby on Rails
  8. 8. Design Principles MVC architecture Agile Development Usability and Success Stories Community and Resources Convention over Configuration fixed directory structure everything has its place – source files, libs, plugins, database files, documentation etc file naming conventions e.g. camel case class name, underscore file name database naming conventions table names, primary and foreign keys standard configuration files e.g. database connections, environment setting definitions (development, production, test) Introduction to Ruby on Rails
  9. 9. Design Principles MVC architecture Agile Development Usability and Success Stories Community and Resources DRY - Don’t Repeat Yourself reusing code e.g. view elements reusing data e.g. no need to declare table field names – can be read from the database making each line of code work harder e.g. mini languages for specific domains, like object-relational mapping metaprogramming dynamically created methods Introduction to Ruby on Rails
  10. 10. Design Principles MVC architecture Agile Development Usability and Success Stories Community and Resources Design Principles MVC architecture Agile Development Usability and Success Stories Community and Resources Introduction to Ruby on Rails
  11. 11. Design Principles MVC architecture Agile Development Usability and Success Stories Community and Resources Model - ActiveRecord conventions: name based OR mapping: mice db table -> Mouse class primary key: auto-incremented numeric field called quot;idquot; foreign key: quot;[singular_of_foreign_table_name]_idquot;, e.g. quot;cat_idquot; dynamic getters, setters, finders dynamic means the existance of a field in db is enough to manipulate it, e.g. dynamic getter like: mouse.name abstracted db operations - create, update, destroy, find, ... possible to use raw sql, but usually unnecessary, e.g.: Mouse.find(:all, :include => {:mouse_holes}, :conditions => quot;location = ’shed’quot;, :order => quot;namequot;, :limit => 10) Introduction to Ruby on Rails
  12. 12. Design Principles MVC architecture Agile Development Usability and Success Stories Community and Resources Model - ActiveRecord support for basic entity relations (1:1, 1:n, n:n) with dynamic accessors backyard_mouse_hole.mice #=> Array of Mouse objects parametrised queries against SQL injection, e.g. find all mice whose name is sth entered by the user: Mouse.find(:all, :conditions => [quot;name = ?quot;, mouse_name) validators: an object won’t be saved in db unless it passes all validation rules, e.g. uniqueness of a given field: validates_uniqueness_of :login object life cycle callbacks, e.g. before_create, before_save Single Table Inheritance, polymorphic associations Introduction to Ruby on Rails
  13. 13. Design Principles MVC architecture Agile Development Usability and Success Stories Community and Resources View - ActionView multiple template types oldest and basic: erb (embedded ruby), similar to e.g. jsp remote javascript templates xml templates easy reuse of view elements file inclusion – layouts, templates, partials multiple standard quot;helpersquot; – common html element generators (e.g. form elements, paginators) ridiculously easy AJAX integration – prototype, scriptaculous Introduction to Ruby on Rails
  14. 14. Design Principles MVC architecture Agile Development Usability and Success Stories Community and Resources Controller - ActionController all actions grouped logically in controller objects (actions are public methods of the controller class) conventions name based url – action mapping e.g. url: myhost.com/mice/show/1 action: ’show’ action in MiceController, params: id=1 name based action - template file mapping e.g. action ’show’ in MiceController - template file app/views/mice/show.rhtml can be organised in an inheritance hierarchy reduces code duplication and simplifies code action callbacks – before_filter, after_filter, around_filter built-in mechanism of url rewrites – routes easy access to request data, session, cookies Introduction to Ruby on Rails
  15. 15. Design Principles MVC architecture Agile Development Usability and Success Stories Community and Resources Design Principles MVC architecture Agile Development Usability and Success Stories Community and Resources Introduction to Ruby on Rails
  16. 16. Design Principles MVC architecture Agile Development Usability and Success Stories Community and Resources Rapid Development built-in webserver generators – save the fingers scaffold – kick-off start plugins, libraries, tons of contributed code Introduction to Ruby on Rails
  17. 17. Design Principles MVC architecture Agile Development Usability and Success Stories Community and Resources Debugging verbose log output breakpoint debugger script/console Introduction to Ruby on Rails
  18. 18. Design Principles MVC architecture Agile Development Usability and Success Stories Community and Resources Testing test database + fixtures unit tests - tests for models functional - tests for controllers integration - tests for workflow testing directly in browser - Selenium Introduction to Ruby on Rails
  19. 19. Design Principles MVC architecture Agile Development Usability and Success Stories Community and Resources Agile Project Heartbeat test coverage - rcov continuous integration iterative db schema control - migrations automated deployment - capistrano Introduction to Ruby on Rails
  20. 20. Design Principles MVC architecture Agile Development Usability and Success Stories Community and Resources Design Principles MVC architecture Agile Development Usability and Success Stories Community and Resources Introduction to Ruby on Rails
  21. 21. Design Principles MVC architecture Agile Development Usability and Success Stories Community and Resources Platform Independence win, lin, mac Apache, Lighttpd, mongrel gem package manager Introduction to Ruby on Rails
  22. 22. Design Principles MVC architecture Agile Development Usability and Success Stories Community and Resources IDEs textmate vim RadRails, Eclipse + RDT, Aptana plugins for Idea, NetBeans SCiTe jEdit Introduction to Ruby on Rails
  23. 23. Design Principles MVC architecture Agile Development Usability and Success Stories Community and Resources Scaling it scales – a must-see slideshow about scaling twitter (600 req/s!): http://www.slideshare.net/Blaine/scaling-twitter view caching page caching action caching fragment caching sql caching memcached shared nothing architecture - multiplying application servers Introduction to Ruby on Rails
  24. 24. Design Principles MVC architecture Agile Development Usability and Success Stories Community and Resources Hosting http://wiki.rubyonrails.org/rails/pages/RailsWebHosts US - a variety of shared hosting offers Poland - poor shared hosting options hosted.pl Root VPS Introduction to Ruby on Rails
  25. 25. Design Principles MVC architecture Agile Development Usability and Success Stories Community and Resources Success Stories several ror powered high traffic sites http://twitter.com/ - community site http://www.odeo.com/ - music sharing http://www.43things.com/, http://www.43places.com/, http://www.43people.com/ 37Signals: BaseCamp, BackPack, Ta-Da List, Writeboard, CampFire http://www.strongspace.com/ - storing and sharing files Introduction to Ruby on Rails
  26. 26. Design Principles MVC architecture Agile Development Usability and Success Stories Community and Resources Design Principles MVC architecture Agile Development Usability and Success Stories Community and Resources Introduction to Ruby on Rails
  27. 27. Design Principles MVC architecture Agile Development Usability and Success Stories Community and Resources Books in English: ruby: quot;Programming Ruby 2nd Editionquot; (the Pickaxe) Dave Thomas, with Chad Fowler and Andy Hunt quot;Agile Web Development with Railsquot; Dave Thomas and David Heinemeier Hansson quot;Rails Recipesquot; Chad Fowler many, many more: http://www.rubyonrails.org/books in Polish: quot;Programowanie w jezyku Ruby. Wydanie IIquot; Dave Thomas, ˛ Chad Fowler, Andy Hunt quot;Rails. Przepisyquot; Chad Fowler quot;Ruby on Rails. Wprowadzeniequot; Bruce A. Tate, Curt Hibbs ´ quot;Ruby on Rails. Cwiczeniaquot; Michał Sobczak Introduction to Ruby on Rails
  28. 28. Design Principles MVC architecture Agile Development Usability and Success Stories Community and Resources Links main site: http://www.rubyonrails.org PL site: http://www.rubyonrails.pl/ general mailing list: http://groups.google.com/group/rubyonrails-talk wiki: http://wiki.rubyonrails.org/rails api: api.rubyonrails.org 74 Quality Ruby on Rails Resources and Tutorials: http://www.softwaredeveloper.com/features/74-ruby-on- rails-resources-tutorials-050207/ Introduction to Ruby on Rails
  29. 29. Design Principles MVC architecture Agile Development Usability and Success Stories Community and Resources Events Rails Conf Rails Day local communities & meetings Ruby on Rails workshops: http://rubyonrailsworkshops.com/ academic presence Introduction to Ruby on Rails
  30. 30. Questions? pics: http://www.midcoast.com/ holo/Seth%20in%20Thailand/pages/Rail%20tracks.html, http://mfrost.typepad.com/cute_overload/2007/05/this_site_is_dr.html

×