Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Ruby&Rails Yuri Veremeyenko Monica Verma.

Similar presentations


Presentation on theme: "Introduction to Ruby&Rails Yuri Veremeyenko Monica Verma."— Presentation transcript:

1 Introduction to Ruby&Rails Yuri Veremeyenko Monica Verma

2 Presentation Structure 1.Ruby –Overview –Syntax 2.Rails –Introduction –What makes Rails a good choice? –Typical Web Request Flow –ROR and MVC 3.On-spot project creation –Basic application (blog)‏ –Scaffold, Migrations, Routing, etc. 4.Conclusions

3 Ruby - overview Originally from Japan, first public version 1995 Multipurpose: from scripting to fully fledged OO applications Current Version : 1.8.7 Web Site: http://www.ruby-lang.org/http://www.ruby-lang.org/ Ruby in your web browser: http://tryruby.hobix.com/

4 Ruby – syntax examples Everything is an object 255.times {|i| puts "Hello #{i}" } Arrays a = [1, 'hi', 3.14, 1, 2] a.reverse a.each { |x| puts x } Hashes h = {:water => 'wet', :fire => 'hot'} h.each_pair do |key, value| puts "#{key} is #{value}"; end Ranges (0..2).each {|i| puts i} #=> [0, 1, 2]

5 Ruby – syntax examples II Classes & Methods class Person attr_accessor :name, :age def initialize(name, age)‏ @name, @age = name, age end p = Person.new("jay", 29)‏ p.name >> "jay” Exceptions raise ArgumentError, "Illegal arguments!”... begin # Do something rescue # Handle exception end

6 Ruby – syntax examples III Returning multiple parameters class MyClass < MyAbstractClass def my_method return 1, 2 end a = MyClass.new b, c = a.my_method Reflection a = MyClass.new a.public_methods a.respond_to? "mymethod"

7 Ruby on Rails “an open source web framework that optimizes for programmer happiness and sustainable productivity” This translates to: –Usage of MVC pattern –Scripts and generators for common actions –Use of Ruby's object nature –DRY (don't repeat yourself)‏ –Support for most Web 2.0 fancy stuff

8 ROR: installation Ruby –The ruby interpreter (1.8.6 recommended)‏ Gem –Gem is ruby package manager Database (postgres / mysql / sqlite)‏ –Install as you would normally Bind Ruby and your Database –Normally available as gem Rails and other Gems and libs, as required –Libopenssl-ruby, ruport, rdoc, irb, etc.. IDE

9 What are typical activities for web application? Display form Validate form Display form errors Save form Show aggregated data (and more forms)‏

10 What makes ROR a good choice? MVC ORM (ActiveRecord with associations)‏ Data validation standardized Well structured application layout Generators for common tasks Testing support

11 ROR in typical web request Fig.1 Web request flow

12 ROR and MVC Model : database logic, business data logic Controller : request processing, couples View and Model View : sets of templates to render HTML Fig.2 MVC in Rails

13 Generators and migrations Generators can create skeletons for models/views/controllers/ or scaffold >ruby script/generate scaffold MyEntity Migrations provide DB synchronization class CreatePosts < ActiveRecord::Migration def self.up create_table :posts do |t| { t.column :title, :string ; t.column :body, :text } end def self.down drop_table :posts ; end; end

14 Inside Rails Fig.3 Rails Model code

15 Example Application A sample blog application in 20 minutes Create Posts and Comments Scaffolding, migrations, rake Specifying relationships in Models Customizing generated Controllers Very basic HTML

16 Conclusions Ruby on Rails: Is an MVC framework for Web application Has efficient ActiveRecord ORM Supports migrations Has generators for common tasks Supports unit testing Provides AJAX support via prototype RESTful since v2.0


Download ppt "Introduction to Ruby&Rails Yuri Veremeyenko Monica Verma."

Similar presentations


Ads by Google