Beginners' guide to Ruby on Rails
-
Upload
victor-porof -
Category
Technology
-
view
4.637 -
download
5
Transcript of Beginners' guide to Ruby on Rails
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
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!
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
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)
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
Model snippet
class Person < ActiveRecord::Basevalidates_presence_of :namehas_many :wifes
end
Person.create(:name => ”Muhammad Ali”).valid? # => truePerson.create(:name => nil).valid? # => false
Migration snippet
class AddReceiveNewsletterToUsers < ActiveRecord::Migrationdef self.upchange_table :users do |t|t.boolean :receive_newsletter, :default => false
endUser.update_all ["receive_newsletter = ?", true]
end
def self.downremove_column :users, :receive_newsletter
endend
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
View snippet
<!-- app/views/items/new.rhtml -->
<%= form_tag :action => “create” %>Name: <%= text_field “item”, “name”%><br/>Email: <%= text_field “item”, ”email”%><br/>Password: <%= hidden_field “item”, “password”%><br/><%= submit_tag %><%= end_form_tag %>
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”
Controller snippet
class ItemsController < ApplicationControllerdef edit@item = Item.find(params[:id])
if request.post? @item.update_attributes(params[:item])redirect_to :action => 'edit', :id => @item.id
endend
end
MVC on Rails
To summarize:
1. Keep your view code skinny
2. Keep your controller code skinny
3. The fatness is in the model
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)
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:string hashed_password:string
#controller generator (general)
ruby script/generate controller controller_name method_name(s) (v2.3)
rails generate controller controller_name method_name(s) (v3)
#controller generator (example)
rails generate controller store index
RoR servers
Mongrel
Webbrick
Thin
Apache (mod_passanger)
..so it’s very scalable!
Famous projects on Rails
Twitter (microblogging)
Crunchbase (companies database)
BaseCamp (project management)
Hulu (online tv)
Yellowpages.com (phone database)
Xing (business network)
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)
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
Creating a blog with