call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
Ruby On Rails Basics
1. Ruby On Rails
Basics
Amit Solanki
http://amitsolanki.com
2. Introduction
Web-application framework - includes everything needed to create
database-backed web applications according to the Model-View-
Control(MVC) pattern
Built on Ruby - Language of the year 2006
Extracted by David Heinemeier Hansson(DHH) from his work on
Basecamp, a project management tool by 37signals
Released as open source in July 2004 - more than 1400 contributors
4. Framework - Why do we need it?
Consider following Python Code:
#!/usr/bin/env python
import MySQLdb
print "Content-Type: text/htmln"
print "<html><head><title>Books</title></head>"
print "<body>"
print "<h1>Books</h1>"
print "<ul>"
connection = MySQLdb.connect(user='mysql_user', passwd='mysql_pass', db='my_database')
cursor = connection.cursor()
cursor.execute("SELECT name FROM books ORDER BY pub_date DESC LIMIT 10")
for row in cursor.fetchall():
print "<li>%s</li>" % row[0]
print "</ul>"
print "</body></html>"
connection.close()
5. Framework - Why do we need it?
What happens when multiple parts of your application need to connect to the
database?
- database-connecting code need to be duplicated in each individual CGI script.
Instead, we could refactor it into a shared function
Should a developer really have to worry about printing the “Content-Type”
line and remembering to close the database connection?
- this reduces programmer productivity and introduces opportunities for mistakes. These
setup- and teardown-related tasks would best be handled by some common
infrastructure
What happens when this code is reused in multiple environments, each with a
separate database and password?
- some environment-specific configuration becomes essential
What happens when a Web designer who has no experience coding Python
wishes to redesign the page?
- one wrong character could crash the entire application. Ideally, the logic of the page — the
retrieval of book titles from the database — would be separate from the HTML display of
the page, so that a designer could edit the latter without affecting the former.
6. Framework - Why do we need it?
The MVC Implementation:
# models.py (the database tables)
from django.db import models
class Book(models.Model):
name = models.CharField(max_length=50)
pub_date = models.DateField()
# views.py (the business logic)
from django.shortcuts import render_to_response
from models import Book
def latest_books(request):
book_list = Book.objects.order_by('-pub_date')[:10]
return render_to_response('latest_books.html', {'book_list': book_list})
# urls.py (the URL configuration)
from django.conf.urls.defaults import *
import views
urlpatterns = patterns('',
(r'^latest/$', views.latest_books),
)
# latest_books.html (the template)
<html><head><title>Books</title></head>
<body>
<h1>Books</h1>
<ul>
{% for book in book_list %}
<li>{{ book.name }}</li>
{% endfor %}
</ul>
</body></html>
7. Framework - Why do we need it?
The models.py file contains a description of the database table, represented by
a Python class. This class is called a model. Using it, you can create, retrieve,
update and delete records in your database using simple Python code rather
than writing repetitive SQL statements
The views.py file contains the business logic for the page. The latest_books()
function is called a view
The urls.py file specifies which view is called for a given URL pattern. In this
case, the URL /latest/ will be handled by the latest_books() function. In other
words, if your domain is example.com, any visit to the URL http://
example.com/latest/ will call the latest_books() function
The latest_books.html file is an HTML template that describes the design of
the page. It uses a template language with basic logic statements — e.g., {%
for book in book_list %}
8. Running Rails on your machine
Install ruby - http://ruby-lang.org/
Windows: One click installer
Linux: Installer tools such as apt-get and yum
Mac OS X: Macports
Best practice is to install by compiling from source
Install rubygems - http://rubyforge.org
Install rails gem - http://rubyonrails.org/download
gem install rails
may require sudo access on unix/linux based OS
Editors: TextMate, VIM, Emacs, jEdit, SciTE
IDE: RadRails, RubyMine, 3rd Rail, NetBeans, Komodo
9. Features
Convention over Configuration
Don't Repeat Yourself (DRY)
Agile
Individuals and interactions over processes and tools
Working software over comprehensive documentation
Customer collaboration over contract negotiation
Responding to change over following a plan
Easy integration with features with AJAX and RESTful
Connects to most of the databases - just install DB driver
Latest stable release is 2.3 => 3.0 Coming soon
Rails is FUN
11. Framework Structure
ActiveRecord
an object relationship mapping (ORM) system for database access
ActiveResource
provides web services
before 2.0 => ActionWeb
ActionPack
ActionController
ActionView
ActiveSupport
Utility classes and standard library extensions from Rails
ActionMailer
Plugins to extend capability
12. Directory Structure
.
|-- README Installation and usage information
|-- Rakefile Build script
|-- app Model, view and controller files go here
| |-- controllers
| |-- helpers
| |-- models
| |-- views
|-- config Configuration and database connection parameters
| |-- boot.rb
| |-- database.yml
| |-- environment.rb
| |-- environments
| |-- initializers
| `-- routes.rb
|-- db Schema and migration information
| |-- migrate
|-- doc Autogenerated documentation
|-- lib Shared code
|-- log Log files produced by your application
|-- public Web-acccessible directory. Your application runs from here
|-- script Utility scripts
|-- test Unit, functional, and integration tests, fixtures, and mocks
|-- tmp Runtime temporary files
|-- vendor Imported code
`-- plugins
14. config/
Every environment has a database configuration in database.yml
Global configuration file - environment.rb
Individual configuration file under config/environments
production.rb
development.rb
test.rb
Easy to add custom environments
e.g. for a staging server create staging.rb
Start server by passing RAILS_ENV
ruby script/server RAILS_ENV=‘production’
mongrel_rails start
17. Some Quotes
“Rails is the most well thought-out web development framework I’ve ever used.
And that’s in a decade of doing web applications for a living. I’ve built my
own frameworks, helped develop the Servlet API, and have created more than a
few web servers from scratch. Nobody has done it like this before.”
-James Duncan Davidson, Creator of Tomcat and Ant
“Ruby on Rails is a breakthrough in lowering the barriers of entry to
programming. Powerful web applications that formerly might have taken
weeks or months to develop can be produced in a matter of days.”
-Tim O'Reilly, Founder of O'Reilly Media
“It is impossible not to notice Ruby on Rails. It has had a huge effect both in
and outside the Ruby community... Rails has become a standard to which
even well-established tools are comparing themselves to.”
-Martin Fowler, Author of Refactoring, PoEAA, XP Explained
“What sets this framework apart from all of the others is the preference for
convention over configuration making applications easier to develop and
understand.”
-Sam Ruby, ASF board of directors
18. Some Quotes (contd.)
“Before Ruby on Rails, web programming required a lot of verbiage, steps and
time. Now, web designers and software engineers can develop a website much
faster and more simply, enabling them to be more productive and effective in
their work.”
-Bruce Perens, Open Source Luminary
“After researching the market, Ruby on Rails stood out as the best choice. We
have been very happy with that decision. We will continue building on Rails
and consider it a key business advantage.”
-Evan Williams, Creator of Blogger, ODEO, and Twitter
“Ruby on Rails is astounding. Using it is like watching a kung-fu movie,
where a dozen bad-ass frameworks prepare to beat up the little newcomer only
to be handed their asses in a variety of imaginative ways.”
-Nathan Torkington, O'Reilly Program Chair for OSCON
“Rails is the killer app for Ruby.”
Yukihiro Matsumoto, Creator of Ruby