Ruby on Rails. What's Ruby  A programming language  Developed by Yukihiro Matsumoto (aka Matz) in the 1990s  A programming language  Developed by.

Slides:



Advertisements
Similar presentations
JQuery MessageBoard. Lets use jQuery and AJAX in combination with a database to update and retrieve information without refreshing the page. Here we will.
Advertisements

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 15 Introduction to Rails.
CakePHP. A framework for developing applications in PHP Inspired by Ruby on Rails Follows MVC design pattern Convention over configuration –No wheel reinventing.
The Librarian Web Page Carol Wolf CS396X. Create new controller  To create a new controller that can manage more than just books, type ruby script/generate.
Ruby on Rails Model of MVC. Model-View-Controller Paradigm A way of organizing a software system Benefits: Isolation of business logic from the user interface.
Chapter 15 © 2010 by Addison Wesley Longman, Inc Origins and Uses of Ruby - Designed by Yukihiro Matsumoto; released in Use spread rapidly.
Creating Web Services with Ruby on Rails Robert Thew Internet and Web Systems II.
Language of the Month If it’s December, it must be Ruby! Adam Coffman and Brent Beer.
Ruby on Rails Web development that doesn’t hurt By Mylore, Dung, Lee, Karen and Bryan.
Web Applications Development Using Coldbox Platform Eddie Johnston.
Ruby on Rails Tutorial Peter Mosca April, Ruby on Rails Tutorial Ruby History Invented 12 years ago in Japan by Yukihiro Matsumoto Spent first 5.
Ruby on Rails by Manik Juneja Ruby On Rails. Ruby on Rails by Manik Juneja Rails is a Web Application development framework. Based on the MVC pattern.
Creating Web Services with Ruby on Rails Robert Thew Internet and Web Systems II.
Ruby on Rails a popular web application framework, aimed to increase the speed and ease of web development Ruby on Rails, Tim Zappe.
Ruby on Rails Creating a Rails Application Carol E Wolf CS396X.
Part or all of this lesson was adapted from the University of Washington’s “Web Design & Development I” Course materials.
Overview of Framework by Ahamed Rifaudeen A. page - i Steps before entering into the Framework?  Basic knowledge of object-oriented programming (OOP)
Ruby on Rails: An Introduction JA-SIG Summer Conference 2007 Michael Irion The University of Tulsa.
| Lugano, Ruby on Rails.
A little engineering on Rails Robert W. Hasker. Goals Intro to the Rails framework ▫Basic concepts: MVC, Active Record ▫A bit of Ruby Using Rails to build.
RUBY ON RAILS Mark Zhang. In this talk  Overview of Ruby on Rails  Core ideas  Show a tiny bit of example code  Touch on several general web development/
Ruby on Rails Web development that doesn’t hurt. Ruby on Rails Framework written in the Ruby programming language for database- backed web applications.
Ruby on Rails Charles Severance Textbook: Build Your own Ruby on Rails Application by Patrick Lenz (ISBN: )
Ruby on Rails. What is Ruby on Rails? Ruby on Rails is an open source full-stack web framework. It is an alternative to PHP/MySQL. It can render templates,
1 Events Lect 8. 2 Event-driven Pages one popular feature of the Web is its interactive nature e.g., you click on buttons to make windows appear e.g.,
Ruby on Rails CSCI 6314 David Gaspar Jennifer Garcia Avila.
UFCEKG-20-2 Data, Schemas & Applications Lecture 4 Server Side Scripting & PHP.
Danielle Baldwin, ITS Web Services CMS Administrator Application Overview and Joomla 1.5 RC 1 Highlights.
+ Java vs. Javascript Jessi Style. + Java Compiled Can stand on its own Written once, run anywhere Two-stage debugging Java is an Object Oriented Programming.
Using Data Active Server Pages Objectives In this chapter, you will: Learn about variables and constants Explore application and session variables Learn.
1 Dr Alexiei Dingli Web Science Stream Models, Views and Controllers.
UC Berkeley Hello Rails. Review: MVC Goal: separate organization of data (model) from UI & presentation (view) by introducing controller –mediates user.
Ruby & rails by Nicholas Belotti. What is ruby Ruby is an object orientated scripting language. In Ruby...everything is an object! Ruby was released in.
NODEJS, THE JOOMLA FRAMEWORK, AND THE FUTURE IAN MACLENNAN.
Ajax and Ruby on Rails Session 9 INFM 603.
1 An Introduction to the Development of Web Applications using Ruby on Rails with Ajax Ansgar Berhorn, B.Sc. and Mike Rowe, Ph.D.
And other languages….  Also called introspection  Program can examine/modify its own state set variables call methods add new methods define new classes.
1 Dr Alexiei Dingli Web Science Stream Introducing Rails.
Ruby! Ronald L. Ramos. What is Ruby? Ruby is a scripting language designed by Yukihiro Matsumoto, also known as Matz. It runs on a variety of platforms,
Ruby on Rails Your first app. Rails files app/ Contains the controllers, models, views and assets for your application. You’ll focus on this folder for.
PHP + Framework + MVC CakePHP Lectures 1. What is Framework? CakePHP Lectures 2.
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
Mr. Justin “JET” Turner CSCI 3000 – Fall 2015 CRN Section A – TR 9:30-10:45 CRN – Section B – TR 5:30-6:45.
Arnold Geraldo Designing and Making of The Online Auction Website Using CakePHP Framework.
The Active Record Paradigm Databases in Database-Centric Web Site Development.
Ruby on Rails WTF? Created by Matz in 1993 Inspired by Smalltalk Rubygems = CSPAN Rake = ant Rdoc = javadoc.
Mr. Justin “JET” Turner CSCI 3000 – Fall 2015 CRN Section A – TR 9:30-10:45 CRN – Section B – TR 5:30-6:45.
Rails & Ajax Module 5. Introduction to Rails Overview of Rails Rails is Ruby based “A development framework for Web-based applications” Rails uses the.
Ruby/Ruby on Rails Yasushi Osonoi Open Dream corporation
Chapter 2: Variables, Functions, Objects, and Events JavaScript - Introductory.
Chapter 15 © 2013 by Pearson Overview of Rails - Rails is a development framework for Web-based applications - Based on MVC architecture for applications.
David Lawrence 7/8/091Intro. to PHP -- David Lawrence.
CS562 Advanced Java and Internet Application Introduction to the Computer Warehouse Web Application. Java Server Pages (JSP) Technology. By Team Alpha.
Introduction to Ruby&Rails Yuri Veremeyenko Monica Verma.
Date : 3/04/2010 Web Technology Solutions Class: PHP Web Application Frameworks.
IS2803 Developing Multimedia Applications for Business (Part 2) Lecture 2: Introduction to IS2803 Rob Gleasure
Scripting Languages Info derived largely from Programming Language Pragmatics, by Michael Scott.
Ruby on Rails. Web Framework for Ruby Designed to make it easier to develop, deploy, and maintain web applications Design with Model-View-Controller –almost.
CGS 3066: Web Programming and Design Spring 2016 PHP.
1 Rails for the Ruby-Impaired John Paul Ashenfelter CTO/Transitionpoint.
Web Technology Solutions
Ruby Reflection and other languages….
Introduction to Dynamic Web Programming
Scripting Languages Info derived largely from Programming Language Pragmatics, by Michael Scott.
Ruby: An Introduction Created by Yukihiro Matsumoto in 1993 (named after his birthstone) Pure OO language (even the number 1 is an instance of a class)
ASP MVP Web applications and Razor
Haritha Dasari Josue Balandrano Coronel -
Ruby on Rails by Manik Juneja
Ruby on Rails by Manik Juneja
Chapter 15 Introduction to Rails.
Presentation transcript:

Ruby on Rails

What's Ruby  A programming language  Developed by Yukihiro Matsumoto (aka Matz) in the 1990s  A programming language  Developed by Yukihiro Matsumoto (aka Matz) in the 1990s

What's Rails  Initially developed by David Heinemeier Hansson, out of his work on Basecamp, a project management system  It is a framework of scripts in ruby that provide for rapid development of web applications, esp those with a database back end  Rails can build the skeleton of an application, including the database tables, in just a few commands  Initially developed by David Heinemeier Hansson, out of his work on Basecamp, a project management system  It is a framework of scripts in ruby that provide for rapid development of web applications, esp those with a database back end  Rails can build the skeleton of an application, including the database tables, in just a few commands

Ruby

Syntax  Ruby is largely and loosely based on perl (hence the name, according to lore)  Completely object oriented  Ruby is largely and loosely based on perl (hence the name, according to lore)  Completely object oriented

Some Important differences  Unlike PHP, scope in variables is defined by the leading sigil  the $ sign denotes global scope, not a variable  represents local scope within an object instance  represents local scope within a class  A capitalized name is a constant  Unlike PHP, scope in variables is defined by the leading sigil  the $ sign denotes global scope, not a variable  represents local scope within an object instance  represents local scope within a class  A capitalized name is a constant

Historical Differences  Javascript--born of the competition between two companies  PHP--created by a varied community  Ruby--the vision of a single person  Rails--the vision of another single person  When you compare these, you can see how the starting point influences the process of development  Javascript--born of the competition between two companies  PHP--created by a varied community  Ruby--the vision of a single person  Rails--the vision of another single person  When you compare these, you can see how the starting point influences the process of development

Playing on the Command Line  Ruby is an interpreter, just like php or bash: Avatar:~ hays$ ruby print "howdy world!" ^d  Or, use ruby -e "command": ruby -e 'puts "hello\n"'  Or, you can just use irb, which is easier: Avatar:~ hays$ irb >> print "howdy world!" howdy world!=> nil >>  Ruby is an interpreter, just like php or bash: Avatar:~ hays$ ruby print "howdy world!" ^d  Or, use ruby -e "command": ruby -e 'puts "hello\n"'  Or, you can just use irb, which is easier: Avatar:~ hays$ irb >> print "howdy world!" howdy world!=> nil >>

Object Oriented  Truly  Not a prototyping language like javascript  Nor a procedural language with OOP bolted on  Truly  Not a prototyping language like javascript  Nor a procedural language with OOP bolted on

Classes  A class is a kind of master object  Can contain constants and methods  Instances of object can be created from a class, inheriting the traits of the class  A class is a kind of master object  Can contain constants and methods  Instances of object can be created from a class, inheriting the traits of the class

A simple class class Cat end (but this class doesn't do or mean anything) class Cat end (but this class doesn't do or mean anything) the class examples are derived from

cat class  I want four attributes for a cat; name, color, type, and attribute class Cat # must be capitalized attr_accessor :name, :type, :color, :attribute def initialize(name, type, color, = = = = attribute end

creating a new cat  Now, I can create an instance of the cat class: gc = Cat.new("GC", "short hair", "black", "gimpy") lc = Cat.new("LC", "short hair", "black", "little")

add a method  I'd like to be able to describe my cats easily  So I add a method to the cat class: def + " is a " + " " + " who is " + ".\n" end  I'd like to be able to describe my cats easily  So I add a method to the cat class: def + " is a " + " " + " who is " + ".\n" end

eliminating con-cat-ination  The concatenation is a bit awkward  Like php, ruby has a structure for calling variables within a string: is a who is  The concatenation is a bit awkward  Like php, ruby has a structure for calling variables within a string: is a who is

calling the method  If I call a cat with the describe method attached, I can get the description of that cat: my_string= gc.describe puts my_string  or: puts gc.describe  If I call a cat with the describe method attached, I can get the description of that cat: my_string= gc.describe puts my_string  or: puts gc.describe

finding cats by name  A second method, find_by_name: def self.find_by_name(name) found = nil ObjectSpace.each_object(Cat) { |o| found = o if o.name == name } found end  A second method, find_by_name: def self.find_by_name(name) found = nil ObjectSpace.each_object(Cat) { |o| found = o if o.name == name } found end

Access Control  Methods in a class are public by default  Private methods are known only to the individual object  Protected methods can only be called by members of the class in which is was defined  Methods in a class are public by default  Private methods are known only to the individual object  Protected methods can only be called by members of the class in which is was defined

Variables  In ruby, vars are references to objects, not objects themselves  So: a = "my value" b = a a[0] = "n" will change both a and b--but if you reassign a, eg a="new value", a is linked to a new object (this might bite you, but it's not likely)  In ruby, vars are references to objects, not objects themselves  So: a = "my value" b = a a[0] = "n" will change both a and b--but if you reassign a, eg a="new value", a is linked to a new object (this might bite you, but it's not likely)

Arrays  Create an array by assignment: my_array = [ "one", "two", 3, 4 ]  Referencing the array: puts "my_array[0] is: #{my_array[0]}\n"  The brackets are methods of the array class…  Create an array by assignment: my_array = [ "one", "two", 3, 4 ]  Referencing the array: puts "my_array[0] is: #{my_array[0]}\n"  The brackets are methods of the array class…

Hashes  What in php is called an associative array is called a hash in ruby  Creating a hash by assignment: my_hash = { 'tree' => 'pine', 'bird' => 'mocking'} puts "\n" puts "my_hash['tree'] is: #{my_hash['tree']}\n" puts "my_hash['bird'] is: #{my_hash['bird']}\n"  Notice that the syntax is different  What in php is called an associative array is called a hash in ruby  Creating a hash by assignment: my_hash = { 'tree' => 'pine', 'bird' => 'mocking'} puts "\n" puts "my_hash['tree'] is: #{my_hash['tree']}\n" puts "my_hash['bird'] is: #{my_hash['bird']}\n"  Notice that the syntax is different

walking a hash or array  use the each method: a = 1 my_hash.each do |key, value| puts "#{a} #{key} is: #{value}" a = a +1 end

conditional  much like php and javascript, but simpler syntax: a = 1 my_hash.each do |key, value| if key == "tree" puts "#{a} #{key} is: #{value}" end a = a +1 end

In summary  Ruby's syntax is pretty  Ruby is all about structure  Classes are easy to work with, if you're new, start with simple examples  Ruby's syntax is pretty  Ruby is all about structure  Classes are easy to work with, if you're new, start with simple examples

Rails

Model View Controller (MVC)  Layering again  MVC allows a project team to work on different aspects of the application without stepping on each other's toes quite so often  Note that neither PHP nor Javascript encourage this, but it can be done in PHP (not so much in Javascript)  Rails enforces MVC  Layering again  MVC allows a project team to work on different aspects of the application without stepping on each other's toes quite so often  Note that neither PHP nor Javascript encourage this, but it can be done in PHP (not so much in Javascript)  Rails enforces MVC

Model  Contains the data of the application  Transient  Stored (eg Database)  Enforces "business" rules of the application  Attributes  Work flow  Contains the data of the application  Transient  Stored (eg Database)  Enforces "business" rules of the application  Attributes  Work flow

Views  Provides the user interface  Dynamic content rendered through templates  Three major types  Ruby code in erb (embedded ruby) templates  xml.builder templates  rjs templates (for javascript, and thus ajax)  Provides the user interface  Dynamic content rendered through templates  Three major types  Ruby code in erb (embedded ruby) templates  xml.builder templates  rjs templates (for javascript, and thus ajax)

Controllers  Perform the bulk of the heavy lifting  Handles web requests  Maintains session state  Performs caching  Manages helper modules  Perform the bulk of the heavy lifting  Handles web requests  Maintains session state  Performs caching  Manages helper modules

Convention over Configuration  Notion that coding is reduced if we adopt a standard way of doing things  Eg., if we have a class "Pet" in our model that defines the characteristic of domestic animal, in rails, the database table created for us will be named "pets"  Other chunks of code look for each other by their common names  Notion that coding is reduced if we adopt a standard way of doing things  Eg., if we have a class "Pet" in our model that defines the characteristic of domestic animal, in rails, the database table created for us will be named "pets"  Other chunks of code look for each other by their common names

Action Pack  Since views and controllers interact so tightly, in rails they are combined in Action Pack  Action pack breaks a web request into view components and controller compoents  So an action usually involves a controller request to create, read, update, or delete (CRUD) some part of the model, followed by a view request to render a page  Since views and controllers interact so tightly, in rails they are combined in Action Pack  Action pack breaks a web request into view components and controller compoents  So an action usually involves a controller request to create, read, update, or delete (CRUD) some part of the model, followed by a view request to render a page

Processing URLs  The basic url used to access a controller is of the form:  The controller will be one you generate, and the action will be one you've defined in your controller  So if you have a controller named "filer" and that controller has an action named "upload", the url will be something like  The basic url used to access a controller is of the form:  The controller will be one you generate, and the action will be one you've defined in your controller  So if you have a controller named "filer" and that controller has an action named "upload", the url will be something like

The View  The controller will have a folder in app/view named after it, and in that will be the view templates associated with the action methods  These templates are usually html with some inserted ruby code  While code can be executed in these templates, keep that simple--any data controls should be made in the controller's files  The controller will have a folder in app/view named after it, and in that will be the view templates associated with the action methods  These templates are usually html with some inserted ruby code  While code can be executed in these templates, keep that simple--any data controls should be made in the controller's files

Creating a basic site  Three commands rails demo cd demo ruby script/generate controller Bark  This creates the framework  Three commands rails demo cd demo ruby script/generate controller Bark  This creates the framework

Making it say something  A def in the app/controller/bark_controller.rb file: def hello end  And some html in the app/views/bark folder, hello.html.erb:  A def in the app/controller/bark_controller.rb file: def hello end  And some html in the app/views/bark folder, hello.html.erb: Howdy

Directory Structure  app: most of your code lives here  config: information environment and database link  database.yml  development, test and production versions  doc, log, tmp  lib: your code, just a place to stick things that don't have a good home elsewhere  app: most of your code lives here  config: information environment and database link  database.yml  development, test and production versions  doc, log, tmp  lib: your code, just a place to stick things that don't have a good home elsewhere

Directory Structure  public: images, javascripts, stylesheets go here  script: script that rails uses, most of these are short and reference files in the lib dir for rails  vendor: 3rd party code  public: images, javascripts, stylesheets go here  script: script that rails uses, most of these are short and reference files in the lib dir for rails  vendor: 3rd party code

Generating a database site  Magic rails temp cd temp rake db:create:all ruby script/generate scaffold Person lname:string fname:string string rake db:migrate ruby script/server

Sources     development-with-rails-third-edition development-with-rails-third-edition  /22/ruby-class-tutorial/ /22/ruby-class-tutorial/     development-with-rails-third-edition development-with-rails-third-edition  /22/ruby-class-tutorial/ /22/ruby-class-tutorial/