1 / 16

Introduction to Ruby&Rails

Yuri Veremeyenko Monica Verma. Introduction to Ruby&Rails. Presentation Structure. Ruby Overview Syntax Rails Introduction What makes Rails a good choice? Typical Web Request Flow ROR and MVC On-spot project creation Basic application (blog)‏ Scaffold, Migrations, Routing, etc.

hcaldwell
Download Presentation

Introduction to Ruby&Rails

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Yuri Veremeyenko Monica Verma Introduction to Ruby&Rails

  2. Presentation Structure • Ruby • Overview • Syntax • Rails • Introduction • What makes Rails a good choice? • Typical Web Request Flow • ROR and MVC • On-spot project creation • Basic application (blog)‏ • Scaffold, Migrations, Routing, etc. • 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/ • 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 • Exceptions • raise ArgumentError, • "Illegal arguments!” • ... • begin • # Do something • rescue • # Handle exception • end • Classes & Methods class Person attr_accessor :name, :age def initialize(name, age)‏ @name, @age = name, age end end p = Person.new("jay", 29)‏ p.name >> "jay”

  6. 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 • 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

More Related