Ruby on Rails SITC Workshop Series American University of Nigeria FALL 2017 1
Evolution of Web Web 1.x Web 1.0: user interaction == server roundtrip Other than filling out form fields Every user interaction causes server roundtrip Every roundtrip causes full page redraw Web 1.5: user interactions without contacting server e.g. form validation before submit e.g. selecting something from menu A causes contents of menu B to change But every roundtrip still causes full page redraw 2
Evolution of Web Web 2.x Separation of server roundtrip from page rendering Initial load of page & draw page User interaction causes background roundtrip to server Response from server captured and passed to a programmer-defined JavaScript function That function can redraw part of the page in place (using same mechanisms as Web 1.5) Result: desktop-like responsive UI s that can contact server Auto completion Lazy fetch of complicated parts of page etc 3
What is Ruby on Rails (RoR)? Ruby on Rails is an open-source, full-stack framework for developing databasebacked web applications according to the Model-View-Control pattern. Ruby on Rails is a gem (plug-in) to Ruby language. Created by David Heinemeier Hansson in 2004 4
What is Ruby on Rails (RoR)? The amazing productivity claims of Rails is the current buzz in the web development community. If your task is to create or manage web applications that capture and manipulate relational database from a web-based user interface, then Ruby on Rails may be the solution you ve been looking for to make your web development life easier. 5
Who is using Rails? Development framework for web applications written in Ruby Used by some of your favorite sites! 6
Who else is using Rails? Penny Arcade AListApart Gusto Shopify Strongspace Yakima-Herald Telegram Subtopic 7
Overview of Ruby on Rails Ruby is the programming language used to manipulate the framework Rails is the framework that provides the necessary infrastructure Rails is written in Ruby 8
When to use Rails New web-to-database systems Migrations from existing PHP, ASP, or Perl systems An alternative to non-enterprise J2EE systems Systems in that gap between simple scripts and complex enterprise systems 9
When NOT to use Rails Simple scripts Non-web projects Complex systems with legacy databases Possible to do but you will lose productivity gains 10
Ruby Features Ruby is a pure object-oriented programming language with a super clean syntax that makes programming elegant and fun. In Ruby, everything is an object Ruby is an interpreted scripting language, just like Perl, Python and PHP. 11
Ruby Features Ruby successfully combines Smalltalk's conceptual elegance, Python's ease of use and learning and Perl's pragmatism. Ruby originated in Japan in 1993 by Yukihiro matz Matsumoto, and has started to become popular worldwide in the past few years as more English language books and documentation have become available. 12
Rails Philosophy Convention over configuration (no XML) Don t Repeat Yourself (DRY) Rails is opinionated 13
Rails Strengths It s all about Scaffolding Productivity You often create temporary code in the early stages of development to help get an application up quickly and see how major components work together. Rails automatically creates much of the scaffolding you'll need. 14
Rails Strengths Write code, not configuration Convention over configuration Most Web development frameworks for.net or Java force you to write pages of configuration code. If you follow suggested naming conventions, Rails doesn't need much configuration. Naming your data model class with the same name as the corresponding database table id as the primary key name 15
Rails Strengths Write code, not configuration Rails introduces the Active Record framework, which saves objects to the database. Based on a design pattern cataloged by Martin Fowler, the Rails version of Active Record discovers the columns in a database schema and automatically attaches them to your domain objects using metaprogramming. This approach to wrapping database tables is simple, elegant, and powerful. 16
Rails Strengths Full Stack Web Framework Rails implements the model-view-controller (MVC) architecture. The MVC design pattern separates the component parts of an application 17
Rails Strengths Full Stack Web Framework 18
Rails Strengths Full Stack Web Framework 19
Rails Architecture Rails applications are implemented using the Model-View-Controller (MVC) Model ActiveRecord View ActionView Controller ActionController MVC pattern allows rapid change and evolution of the user interface and controller separate from the data model 20
Models Provide access to database tables Contain business rules Record CRUD (Create, Read, Update, Delete) An instance represents a row in a table Grade model instance => one row from grades table Models also define validations and associations with other models (see next slide). 21
Models 22
Controllers Handle user requests Retrieve data from models Invoke methods on models Send views and responses to users Should contain no business logic Only flow control 23
Rails-specific MVC components Helpers Partials Layouts 24
Helpers Place to put presentation logic Available to Views and controllers Rails has hundreds of built-in helpers for your views Easy to make your own 25
Partials Pieces of view code that can be reused Can be applied to a collection of data! Designed to be shared across multiple views Examples Form fields for Create and Update pages Search results Tables Table rows 26
Layouts Your templates are here. Global or controller-specific No need for header and footer separation Rails reads the template last and places your application within the content region Easy for designers to create layouts without knowing Rails 27
Migrations Database-independent schema definition Incrementally define your database Makes creation and recreation easy! I m platformindependent! 28
Scaffolding Generation of controllers and views based on model definition Not meant for production use! Needs tweaking first 29
Demo We will create simple CRUD-like application. Help me to choose one: Bookmarks (title, url) Article (title, body) helloworld (saying hello to the world) Student (lastname,firstname,id,degree_expected) 30
Deployment Linux is the best for deployment Windows is supported Shared Hosting providers supporting Rails Dreamhost (Great for personal stuff) BlueHost RailsPlayground High-availability providers RailsMachine EngineYard Heroku 31
Summary Rail s two guiding principles: Less software (Don t Repeat Yourself - DRY) Convention over Configuration (Write code not configuration files) 32
Resources - Books Learn to Program (Chris Pine) Agile Web Development with Rails (Dave Thomas and David Heinemeier Hansson) Ruby for Rails, Ruby techniques for Rails developers (David Black) Programming Ruby (Dave Thomas) Rails Recipes (Chad Fowler) 33
Resources Web Sites www.rubyonrails.org wiki.rubyonrails.com api.rubyonrails.org www.uwec.edu/webdev/ror for additional resources 34