Introduction to Ruby&Rails Yuri Veremeyenko Monica Verma.

16
Introduction to Ruby&Rails Yuri Veremeyenko Monica Verma

Transcript of Introduction to Ruby&Rails Yuri Veremeyenko Monica Verma.

Page 1: Introduction to Ruby&Rails Yuri Veremeyenko Monica Verma.

Introduction to Ruby&Rails

Yuri Veremeyenko

Monica Verma

Page 2: Introduction to Ruby&Rails Yuri Veremeyenko Monica Verma.

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

Page 3: Introduction to Ruby&Rails Yuri Veremeyenko Monica Verma.

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/

• Ruby in your web browser: http://tryruby.hobix.com/

Page 4: Introduction to Ruby&Rails Yuri Veremeyenko Monica Verma.

Ruby – syntax examples• Everything is an object

255.times {|i| puts "Hello #{i}" }

• Arraysa = [1, 'hi', 3.14, 1, 2]

a.reverse

a.each { |x| puts x }

• Hashesh = {: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]

Page 5: Introduction to Ruby&Rails Yuri Veremeyenko Monica Verma.

Ruby – syntax examples II

• Classes & Methodsclass Person

attr_accessor :name, :age

def initialize(name, age)

@name, @age = name, age

end

end

p = Person.new("jay", 29)

p.name

>> "jay”

•Exceptionsraise ArgumentError, "Illegal arguments!”...begin# Do somethingrescue# Handle exceptionend

Page 6: Introduction to Ruby&Rails Yuri Veremeyenko Monica Verma.

Ruby – syntax examples III • Returning multiple parameters

class MyClass < MyAbstractClass

def my_method

return 1, 2

end

end

a = MyClass.new

b, c = a.my_method

• Reflectiona = MyClass.new

a.public_methods

a.respond_to? "mymethod"

Page 7: Introduction to Ruby&Rails Yuri Veremeyenko Monica Verma.

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

Page 8: Introduction to Ruby&Rails Yuri Veremeyenko Monica Verma.

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

Page 9: Introduction to Ruby&Rails Yuri Veremeyenko Monica Verma.

What are typical activities for web application?

• Display form

• Validate form

• Display form errors

• Save form

• Show aggregated data (and more forms)

Page 10: Introduction to Ruby&Rails Yuri Veremeyenko Monica Verma.

What makes ROR a good choice?

• MVC

• ORM (ActiveRecord with associations)

• Data validation standardized

• Well structured application layout

• Generators for common tasks

• Testing support

Page 11: Introduction to Ruby&Rails Yuri Veremeyenko Monica Verma.

ROR in typical web request

Fig.1 Web request flow

Page 12: Introduction to Ruby&Rails Yuri Veremeyenko Monica Verma.

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

Page 13: Introduction to Ruby&Rails Yuri Veremeyenko Monica Verma.

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

Page 14: Introduction to Ruby&Rails Yuri Veremeyenko Monica Verma.

Inside Rails

Fig.3 Rails Model code

Page 15: Introduction to Ruby&Rails Yuri Veremeyenko Monica Verma.

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

Page 16: Introduction to Ruby&Rails Yuri Veremeyenko Monica Verma.

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