beginners' guide to ruby on rails
TRANSCRIPT
![Page 1: Beginners' guide to Ruby on Rails](https://reader034.vdocument.in/reader034/viewer/2022051617/55a58b9f1a28ab3d488b459d/html5/thumbnails/1.jpg)
![Page 2: Beginners' guide to Ruby on Rails](https://reader034.vdocument.in/reader034/viewer/2022051617/55a58b9f1a28ab3d488b459d/html5/thumbnails/2.jpg)
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
![Page 3: Beginners' guide to Ruby on Rails](https://reader034.vdocument.in/reader034/viewer/2022051617/55a58b9f1a28ab3d488b459d/html5/thumbnails/3.jpg)
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!
![Page 4: Beginners' guide to Ruby on Rails](https://reader034.vdocument.in/reader034/viewer/2022051617/55a58b9f1a28ab3d488b459d/html5/thumbnails/4.jpg)
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
![Page 5: Beginners' guide to Ruby on Rails](https://reader034.vdocument.in/reader034/viewer/2022051617/55a58b9f1a28ab3d488b459d/html5/thumbnails/5.jpg)
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)
![Page 6: Beginners' guide to Ruby on Rails](https://reader034.vdocument.in/reader034/viewer/2022051617/55a58b9f1a28ab3d488b459d/html5/thumbnails/6.jpg)
![Page 7: Beginners' guide to Ruby on Rails](https://reader034.vdocument.in/reader034/viewer/2022051617/55a58b9f1a28ab3d488b459d/html5/thumbnails/7.jpg)
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
![Page 8: Beginners' guide to Ruby on Rails](https://reader034.vdocument.in/reader034/viewer/2022051617/55a58b9f1a28ab3d488b459d/html5/thumbnails/8.jpg)
Model snippet
class Person < ActiveRecord::Basevalidates_presence_of :namehas_many :wifes
end
Person.create(:name => ”Muhammad Ali”).valid? # => truePerson.create(:name => nil).valid? # => false
![Page 9: Beginners' guide to Ruby on Rails](https://reader034.vdocument.in/reader034/viewer/2022051617/55a58b9f1a28ab3d488b459d/html5/thumbnails/9.jpg)
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
![Page 10: Beginners' guide to Ruby on Rails](https://reader034.vdocument.in/reader034/viewer/2022051617/55a58b9f1a28ab3d488b459d/html5/thumbnails/10.jpg)
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
![Page 11: Beginners' guide to Ruby on Rails](https://reader034.vdocument.in/reader034/viewer/2022051617/55a58b9f1a28ab3d488b459d/html5/thumbnails/11.jpg)
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 %>
![Page 12: Beginners' guide to Ruby on Rails](https://reader034.vdocument.in/reader034/viewer/2022051617/55a58b9f1a28ab3d488b459d/html5/thumbnails/12.jpg)
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”
![Page 13: Beginners' guide to Ruby on Rails](https://reader034.vdocument.in/reader034/viewer/2022051617/55a58b9f1a28ab3d488b459d/html5/thumbnails/13.jpg)
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
![Page 14: Beginners' guide to Ruby on Rails](https://reader034.vdocument.in/reader034/viewer/2022051617/55a58b9f1a28ab3d488b459d/html5/thumbnails/14.jpg)
MVC on Rails
To summarize:
1. Keep your view code skinny
2. Keep your controller code skinny
3. The fatness is in the model
![Page 15: Beginners' guide to Ruby on Rails](https://reader034.vdocument.in/reader034/viewer/2022051617/55a58b9f1a28ab3d488b459d/html5/thumbnails/15.jpg)
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)
![Page 16: Beginners' guide to Ruby on Rails](https://reader034.vdocument.in/reader034/viewer/2022051617/55a58b9f1a28ab3d488b459d/html5/thumbnails/16.jpg)
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
![Page 17: Beginners' guide to Ruby on Rails](https://reader034.vdocument.in/reader034/viewer/2022051617/55a58b9f1a28ab3d488b459d/html5/thumbnails/17.jpg)
RoR servers
Mongrel
Webbrick
Thin
Apache (mod_passanger)
..so it’s very scalable!
![Page 18: Beginners' guide to Ruby on Rails](https://reader034.vdocument.in/reader034/viewer/2022051617/55a58b9f1a28ab3d488b459d/html5/thumbnails/18.jpg)
Famous projects on Rails
Twitter (microblogging)
Crunchbase (companies database)
BaseCamp (project management)
Hulu (online tv)
Yellowpages.com (phone database)
Xing (business network)
![Page 19: Beginners' guide to Ruby on Rails](https://reader034.vdocument.in/reader034/viewer/2022051617/55a58b9f1a28ab3d488b459d/html5/thumbnails/19.jpg)
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)
![Page 20: Beginners' guide to Ruby on Rails](https://reader034.vdocument.in/reader034/viewer/2022051617/55a58b9f1a28ab3d488b459d/html5/thumbnails/20.jpg)
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
![Page 21: Beginners' guide to Ruby on Rails](https://reader034.vdocument.in/reader034/viewer/2022051617/55a58b9f1a28ab3d488b459d/html5/thumbnails/21.jpg)
![Page 22: Beginners' guide to Ruby on Rails](https://reader034.vdocument.in/reader034/viewer/2022051617/55a58b9f1a28ab3d488b459d/html5/thumbnails/22.jpg)
Creating a blog with
![Page 23: Beginners' guide to Ruby on Rails](https://reader034.vdocument.in/reader034/viewer/2022051617/55a58b9f1a28ab3d488b459d/html5/thumbnails/23.jpg)
![Page 24: Beginners' guide to Ruby on Rails](https://reader034.vdocument.in/reader034/viewer/2022051617/55a58b9f1a28ab3d488b459d/html5/thumbnails/24.jpg)