What's New in Teams Calling, Meetings and Devices March 2024
Beginners' guide to Ruby on Rails
1. Ruby on Rails Server-side web development VladStoian Victor Porof
2. What we’ll talk about What is Ruby on Rails? A brief history MVC architecture in a nutshell Distinctive framework features Why Ruby? Why Rails? Demo
3. What is RoR? Open Source Abstraction providing generic server-side functionality Used for web application development It’s a framework for the Ruby language ..so don’t confuse it with plain Ruby!
4. A brief history Originates in David Heinemeier Hansson’s work First released in July 2004 Apple ships RoR with Mac OS X Leopard since 2007 Major new developments with v2.3 in 2009 Templates Generate skeleton applications Custom gems and configurations Latest: v3.0.5, 27 February 2011
5. MVC architecture RoR is based on the Model-View-Controller design It’s an architectural development pattern Widely used in many other frameworks: Oracle Application Framework, Cocoon, JSF (Java) ASP.NET (C#) SproutCore, JavascriptMVC (Javascript) Django, Pylons (Python) CakePHP(PHP) PureMVC (many languages)
6.
7. MVC on Rails Models are Ruby classes, used to store and validate data Handles migrations They talk to databases MySQL SQLite PostgreSQL NoSQL MongoDB Cassandra “Chubby guy in the back room” crunching the numbers
8. Model snippet class Person < ActiveRecord::Base validates_presence_of :name has_many :wifes end Person.create(:name => ”Muhammad Ali”).valid? # => true Person.create(:name => nil).valid? # => false
9. Migration snippet class AddReceiveNewsletterToUsers < ActiveRecord::Migration defself.up change_table :users do |t| t.boolean :receive_newsletter, :default => false end User.update_all ["receive_newsletter = ?", true] end defself.down remove_column :users, :receive_newsletter end end
10. MVC on Rails What the user sees HTML, CSS, XML, Javascript (jQuery) JSON, RSS, Atom Automatically generated “view-puppets” Visual representation of data Does not have direct access to the model! It shouldn’t do lots of processing or calculation
12. MVC on Rails Gateway between the model and the view Handles user requests Does parsing and data submissions Takes care of sessions, cookies Works out what data to show and what views to render ”The best controller: it gives orders without knowing (or caring) how it gets done”
13. Controller snippet classItemsController < ApplicationController def edit @item = Item.find(params[:id]) ifrequest.post? @item.update_attributes(params[:item]) redirect_to :action => 'edit', :id => @item.id end end end
14. MVC on Rails To summarize: Keep your view code skinny Keep your controller code skinny The fatness is in the model
15. Framework features Code generators Awesome built-in functions => rapid development Tons of “gems” to choose from Cross-platform compatibility Automated operation (CRUD) Create, Retrieve, Automate and Delete Simplified testing (Rake script)
16. Code generator snippet #model generator (general) ruby script/generate model model_name (v2.3) rails generate model model_name (v3) #model generator (example) rails generate model user name:stringhashed_password:string #controller generator (general) ruby script/generate controller controller_namemethod_name(s) (v2.3) rails generate controller controller_namemethod_name(s) (v3) #controller generator (example) rails generate controller store index
18. Famous projects on Rails Twitter (microblogging) Crunchbase (companies database) BaseCamp (project management) Hulu (online tv) Yellowpages.com (phone database) Xing (business network)
19. Why Ruby? Interpreted language => greater flexibility Provides JIT (just in time compilation) Garbage collection Able to generate code on the fly Cleaner syntax (no more “Verbose verbose is too verbose for verbose”) Many implementations: Jruby, IronRuby, Rubinius (Ruby & C++), MacRuby (ObjC)
20. Why Rails? Based on Ruby Easy to implement CGI scripts Rapid web-application development Designed to make programming work easier Less coding, more functionality Thousands of plugins Don’t reinvent the wheel