SlideShare a Scribd company logo
Ruby on Rails: An Introduction
JA-SIG Summer Conference 2007
Michael Irion
The University of Tulsa
What is Ruby on Rails (RoR)?
• Ruby on Rails is an open-source, full-stack
framework for developing database-backed web
applications according to the Model-View-Control
pattern
Overview of Ruby on Rails
• Ruby is the programming language used to
manipulate the framework
• Rails is the framework that provides the necessary
infrastructure
• Rails is written in Ruby
Ruby Features
• Ruby is an interpreted language (No compile step
necessary)
• Ruby is an Object Oriented language.
• Everything is an object (No primitives)
• Ruby draws from Perl, Smalltalk, and Lisp
Duck Typing in Ruby
• Objects are dynamic, and their types are determined
at runtime
• The type of a Ruby object is much less important
than it’s capabilities
• If a Ruby object walks like a duck and talks like a
duck, then it can be treated as a duck
Rails Philosophy
• Convention over configuration (no XML)
• Don’t Repeat Yourself (DRY)
• Rails is opinionated
Rails Architecture
• Rails applications are implemented using the Model-
View-Controller (MVC)
• Model - ActiveRecord
• View - ActionView
• Controller - ActionController
Starting the Bookmarks Application
• Generate application
> rails bookmarks
Directory Layout
• Rails applications have a common directory structure
• /app - the MVC core
/controllers
/helpers - provides extra functionality for views
/models
/views/nameofcontroller - templates for controller
actions
Directory Layout
/components - will be deprecated
/config - database, route and environment configuration
/db - database schema and migrations
/lib - functions that don’t map to MVC
/log
/public - static web resources (html, css, javascript etc.)
/script - rails utilities
/test - tests and fixtures
/tmp
/vendor - 3rd party plugins
Rails Environment Modes
• Rails runs in different modes, depending on the
parameters given to the server on startup. Each
mode defaults to it’s own database schema
• Development (verbose logging and error messages)
• Test
• Production
Starting Rails
• > cd /directorypath/bookmarks
• Start the server
> ruby script/server start
• Default environment is development
• Default port is 3000
• http://127.0.0.1:3000
Welcome Aboard - Now What?
• Hello world seems appropriate
>ruby script/generate controller hello
exists app/controllers/
exists app/helpers/
create app/views/hello
exists test/functional/
create app/controllers/hello_controller.rb
create test/functional/hello_controller_test.rb
create app/helpers/hello_helper.rb
Mapping URLs to Controllers and Actions
• http://127.0.0.1:3000/hello/sayit
• http://127.0.0.1:3000 - address and port of the
webserver
• hello - name of the controller
• sayit - name of the action (method in controller)
Editing the Controller
def sayit
render :text => "<h2>Hello World!</h2>"
end
Now for an actual Bookmark
• Edit config/database.yml
development:
adapter: mysql
database: bookmarks_development
username: username
password: password
host: localhost
Create the Database
• This step depends on the database and dba tool of
your choice. Create a new schema/dbname for
bookmarks_development, and assign rights for the
user you listed in database.yml.
Bookmark Model
• Our bookmark model will (initially) need two
properties
• URL
• Title
Scaffolding for Bookmarks
• Rails can generate all the basic CRUD operations
for simple models via scaffolding.
• Scaffolding is temporary way to get applications
wired quickly.
> ruby script/generate scaffold_resource bookmark
url:string title:string
Migrations
• Rails uses migrations to version the database.
• Rails tries to minimize SQL at every opportunity
• Migrations are automatically created whenever you
generate a new model
• Migration files are located in db/migrations
• The version number is stored in a table called
schema_info
Bookmarks Migration
located in db/migrate/001_create_bookmarks.rb
class CreateBookmarks < ActiveRecord::Migration
def self.up
create_table :bookmarks do |t|
t.column :url, :string
t.column :title, :string
end
end
def self.down
drop_table :bookmarks
end
end
Running the Migration
• Rake is the general purpose build tool for rails,
much like make, or ant. It has many functions, one
of which is to control migrations.
>rake db:migrate
• Now the table has been created
Bookmarks Table ID
• Bookmarks table has the following fields - id, url,
and title
• Where did the id field come from?
• Convention of configuration - Rails automatically
creates an id field for each new table and uses it as
the primary key
Bookmarks Controller
The /app/controllers/bookmarks.rb default action:
def index
@bookmarks = Bookmark.find(:all)
respond_to do |format|
format.html # index.rhtml
format.xml { render :xml => @bookmarks.to_xml }
end
End
Mapping URLs to Controllers and Actions
• http://127.0.0.1:3000/bookmarks/
• http://127.0.0.1:3000 - address and port of the
webserver
• hello - name of the controller
• / - name of the action (blank maps to the index
action)
Bookmarks Model - Don’t Repeat Yourself
• No getters/setters. Rails uses information from the
database.
class Bookmark < ActiveRecord::Base
end
Bookmarks View
Located in views/bookmarks/index.rhtml
<% for bookmark in @bookmarks %>
<tr>
<td><%=h bookmark.url %></td>
<td><%=h bookmark.title %></td>
<td><%=h bookmark.description %></td>
<td><%= link_to 'Show', bookmark_path(bookmark) %></td>
<td><%= link_to 'Edit', edit_bookmark_path(bookmark) %></td>
<td><%= link_to 'Destroy', bookmark_path(bookmark), :confirm => 'Are you
sure?', :method => :delete %></td>
</tr>
<% end %>
Convention over Configuration
• Bookmark model automatically looks for a table in
the database with a plural form of it’s name.
(bookmarks)
• The Bookmarks controller automatically renders the
template located in views according to the controller
name and the action (views/bookmarks/index.rhtml)
Ajax Helpers
• Rails allows you to program many AJAX calls in
ruby, instead of writing javascript directly
• Script.aculo.us and Prototype libraries are included
• A quick example. Autocomplete for text boxes
AJAX Autocomplete
Add to Bookmarks controller
auto_complete_for :bookmarks, :url
Add to views/bookmarks/edit.rhtml
<%= text_field_with_auto_complete :bookmark, :url %>
In views/layouts/bookmarks.rhtml, add
<%= javascript_include_tag :defaults %>
Validations
• Rails has a number of validation helpers that can be
added to the model.
class Bookmark < ActiveRecord::Base
validates_presence_of :url, :title
end
Validations
• validates_presence_of
• validates_length_of
• validates_acceptance_of
• validates_confirmation_of
• validates_uniqueness_of
• validates_format_of
• validates_numericality_of
• validates_inclusion_in
• validates_exclusion_of
• validates_associated :relation
Associations - Adding Categories to Bookmarks
• The bookmarks are working great, but it would be
nice if we could group them by category
• Rails uses associations to build relationships
between tables
• Associations are independent of database foreign
key constraints
Types of Associations
• has_one
• belongs_to
• has_many
• has_and_belongs_to_many
• has_many :model1, :through => :model2
Changes to the Database
• A new categories table needs to be created
• A category_id field needs to be added to the bookmarks
table
> ruby script/generate scaffold_resource category name:string
• This creates the all the scaffolding and the migration
db/migrate/002_create_categories.rb
• Note the category table is pluralized as categories.
>ruby script/generate migration alter_bookmarks_add_category_id
• This creates
db/migrate/003_alter_bookmarks_add_category_id.rb
Alter Bookmarks Migration
def self.up
add_column :bookmarks, :category_id, :integer
end
def self.down
remove_column :bookmarks, :category_id
end
>rake db:migrate
Types of Associations
• has_one
• belongs_to
• has_many
• has_and_belongs_to_many
• has_many :model1, :through => :model2
Database Relationships
• Parent (or Master) models that have collections of
other models use the has_many relationship
• Child (or Detail) models contain the id field of their
parent record and they use the belongs_to
relationship
Associations Model Code
class Bookmark < ActiveRecord::Base
validates_presence_of :url, :title
belongs_to :category
end
class Category < ActiveRecord::Base
has_many :bookmarks
end
Associations Controller code
def edit
@bookmark = Bookmark.find(params[:id])
@categories = Category.find(:all)
end
Associations View Code
<p>
<b>Category</b><br />
<%= collection_select('bookmark', 'category_id',
@categories, 'id', 'name') %>
</p>
Tools
• Textmate (Mac OS X)
• RadRails (Eclipse plugin) www.radrails.org
• Other commercial and opensource IDEs are being
made available
Resources
• Programming Ruby: The Pragmatic Programmers'
Guide - Second Edition
• Agile Web Development with Rails—Second
Edition
• Rails main site http://www.rubyonrails.com
• My email: michael-irion@utulsa.edu

More Related Content

What's hot

Laravel Eloquent ORM
Laravel Eloquent ORMLaravel Eloquent ORM
Laravel Eloquent ORM
Ba Thanh Huynh
 
Skillwise - Advanced web application development
Skillwise - Advanced web application developmentSkillwise - Advanced web application development
Skillwise - Advanced web application development
Skillwise Group
 
Vibe Custom Development
Vibe Custom DevelopmentVibe Custom Development
Vibe Custom Development
GWAVA
 
Aspose pdf
Aspose pdfAspose pdf
Aspose pdf
Jim Jones
 
Ei cakephp
Ei cakephpEi cakephp
Ei cakephp
eiei lay
 
Cakeph pppt
Cakeph ppptCakeph pppt
Cakeph pppt
Wizard Rider
 
Brief Introduction to Ember
Brief Introduction to EmberBrief Introduction to Ember
Brief Introduction to Ember
Vinay B
 
Software Development with Open Source
Software Development with Open SourceSoftware Development with Open Source
Software Development with Open Source
OpusVL
 
Introduction to CouchDB
Introduction to CouchDBIntroduction to CouchDB
Introduction to CouchDB
OpusVL
 
Web : Request-Response Lifecycle (Ruby on Rails)
Web : Request-Response Lifecycle (Ruby on Rails)Web : Request-Response Lifecycle (Ruby on Rails)
Web : Request-Response Lifecycle (Ruby on Rails)
Rakesh Jha
 
Rails on HBase
Rails on HBaseRails on HBase
Rails on HBase
EffectiveUI
 
Rails on HBase
Rails on HBaseRails on HBase
Rails on HBase
zpinter
 
FITC - Exploring Art-Directed Responsive Images
FITC - Exploring Art-Directed Responsive ImagesFITC - Exploring Art-Directed Responsive Images
FITC - Exploring Art-Directed Responsive Images
Rami Sayar
 
Deep Dive: Alfresco Core Repository (... embedded in a micro-services style a...
Deep Dive: Alfresco Core Repository (... embedded in a micro-services style a...Deep Dive: Alfresco Core Repository (... embedded in a micro-services style a...
Deep Dive: Alfresco Core Repository (... embedded in a micro-services style a...
J V
 
Apache Jackrabbit
Apache JackrabbitApache Jackrabbit
Apache Jackrabbit
elliando dias
 
Display earthquakes with Akka-http
Display earthquakes with Akka-httpDisplay earthquakes with Akka-http
Display earthquakes with Akka-http
Pierangelo Cecchetto
 
KeyValue Stores
KeyValue StoresKeyValue Stores
KeyValue Stores
Mauro Pompilio
 
Powershell For Developers
Powershell For DevelopersPowershell For Developers
Powershell For Developers
Ido Flatow
 
Oracle APEX Nitro
Oracle APEX NitroOracle APEX Nitro
Oracle APEX Nitro
Marko Gorički
 
Speed Up Your APEX Apps with JSON and Handlebars
Speed Up Your APEX Apps with JSON and HandlebarsSpeed Up Your APEX Apps with JSON and Handlebars
Speed Up Your APEX Apps with JSON and Handlebars
Marko Gorički
 

What's hot (20)

Laravel Eloquent ORM
Laravel Eloquent ORMLaravel Eloquent ORM
Laravel Eloquent ORM
 
Skillwise - Advanced web application development
Skillwise - Advanced web application developmentSkillwise - Advanced web application development
Skillwise - Advanced web application development
 
Vibe Custom Development
Vibe Custom DevelopmentVibe Custom Development
Vibe Custom Development
 
Aspose pdf
Aspose pdfAspose pdf
Aspose pdf
 
Ei cakephp
Ei cakephpEi cakephp
Ei cakephp
 
Cakeph pppt
Cakeph ppptCakeph pppt
Cakeph pppt
 
Brief Introduction to Ember
Brief Introduction to EmberBrief Introduction to Ember
Brief Introduction to Ember
 
Software Development with Open Source
Software Development with Open SourceSoftware Development with Open Source
Software Development with Open Source
 
Introduction to CouchDB
Introduction to CouchDBIntroduction to CouchDB
Introduction to CouchDB
 
Web : Request-Response Lifecycle (Ruby on Rails)
Web : Request-Response Lifecycle (Ruby on Rails)Web : Request-Response Lifecycle (Ruby on Rails)
Web : Request-Response Lifecycle (Ruby on Rails)
 
Rails on HBase
Rails on HBaseRails on HBase
Rails on HBase
 
Rails on HBase
Rails on HBaseRails on HBase
Rails on HBase
 
FITC - Exploring Art-Directed Responsive Images
FITC - Exploring Art-Directed Responsive ImagesFITC - Exploring Art-Directed Responsive Images
FITC - Exploring Art-Directed Responsive Images
 
Deep Dive: Alfresco Core Repository (... embedded in a micro-services style a...
Deep Dive: Alfresco Core Repository (... embedded in a micro-services style a...Deep Dive: Alfresco Core Repository (... embedded in a micro-services style a...
Deep Dive: Alfresco Core Repository (... embedded in a micro-services style a...
 
Apache Jackrabbit
Apache JackrabbitApache Jackrabbit
Apache Jackrabbit
 
Display earthquakes with Akka-http
Display earthquakes with Akka-httpDisplay earthquakes with Akka-http
Display earthquakes with Akka-http
 
KeyValue Stores
KeyValue StoresKeyValue Stores
KeyValue Stores
 
Powershell For Developers
Powershell For DevelopersPowershell For Developers
Powershell For Developers
 
Oracle APEX Nitro
Oracle APEX NitroOracle APEX Nitro
Oracle APEX Nitro
 
Speed Up Your APEX Apps with JSON and Handlebars
Speed Up Your APEX Apps with JSON and HandlebarsSpeed Up Your APEX Apps with JSON and Handlebars
Speed Up Your APEX Apps with JSON and Handlebars
 

Similar to Jasig rubyon rails

Introduction to Rails by Evgeniy Hinyuk
Introduction to Rails by Evgeniy HinyukIntroduction to Rails by Evgeniy Hinyuk
Introduction to Rails by Evgeniy Hinyuk
Pivorak MeetUp
 
Lecture #5 Introduction to rails
Lecture #5 Introduction to railsLecture #5 Introduction to rails
Lecture #5 Introduction to rails
Evgeniy Hinyuk
 
12 Introduction to Rails
12 Introduction to Rails12 Introduction to Rails
12 Introduction to Rails
Deepak Hagadur Bheemaraju
 
Supa fast Ruby + Rails
Supa fast Ruby + RailsSupa fast Ruby + Rails
Supa fast Ruby + Rails
Jean-Baptiste Feldis
 
Jasig Rubyon Rails
Jasig Rubyon RailsJasig Rubyon Rails
Jasig Rubyon Rails
Paul Pajo
 
Learning to code for startup mvp session 3
Learning to code for startup mvp session 3Learning to code for startup mvp session 3
Learning to code for startup mvp session 3
Henry S
 
Ruby On Rails
Ruby On RailsRuby On Rails
Ruby On Rails
iradarji
 
Ruby on Rails : 簡介與入門
Ruby on Rails : 簡介與入門Ruby on Rails : 簡介與入門
Ruby on Rails : 簡介與入門
Wen-Tien Chang
 
td_mxc_rubyrails_shin
td_mxc_rubyrails_shintd_mxc_rubyrails_shin
td_mxc_rubyrails_shin
tutorialsruby
 
td_mxc_rubyrails_shin
td_mxc_rubyrails_shintd_mxc_rubyrails_shin
td_mxc_rubyrails_shin
tutorialsruby
 
Ruby projects of interest for DevOps
Ruby projects of interest for DevOpsRuby projects of interest for DevOps
Ruby projects of interest for DevOps
Ricardo Sanchez
 
Ricardo Sanchez - Ruby projects of interest for devops
Ricardo Sanchez - Ruby projects of interest for devopsRicardo Sanchez - Ruby projects of interest for devops
Ricardo Sanchez - Ruby projects of interest for devops
SVDevOps
 
Ruby on Rails introduction
Ruby on Rails introduction Ruby on Rails introduction
Ruby on Rails introduction
Tran Hung
 
Ruby on-rails-101-presentation-slides-for-a-five-day-introductory-course-1194...
Ruby on-rails-101-presentation-slides-for-a-five-day-introductory-course-1194...Ruby on-rails-101-presentation-slides-for-a-five-day-introductory-course-1194...
Ruby on-rails-101-presentation-slides-for-a-five-day-introductory-course-1194...
Nilesh Panchal
 
Ruby on rails for beginers
Ruby on rails for beginersRuby on rails for beginers
Ruby on rails for beginers
shanmukhareddy dasi
 
An introduction to Rails 3
An introduction to Rails 3An introduction to Rails 3
An introduction to Rails 3
Blazing Cloud
 
Getting Started with Rails
Getting Started with RailsGetting Started with Rails
Getting Started with Rails
Basayel Said
 
Rails - getting started
Rails - getting startedRails - getting started
Rails - getting started
True North
 
Introduction to Ruby on Rails
Introduction to Ruby on RailsIntroduction to Ruby on Rails
Introduction to Ruby on Rails
Alessandro DS
 
Ruby On Rails Tutorial
Ruby On Rails TutorialRuby On Rails Tutorial
Ruby On Rails Tutorial
sunniboy
 

Similar to Jasig rubyon rails (20)

Introduction to Rails by Evgeniy Hinyuk
Introduction to Rails by Evgeniy HinyukIntroduction to Rails by Evgeniy Hinyuk
Introduction to Rails by Evgeniy Hinyuk
 
Lecture #5 Introduction to rails
Lecture #5 Introduction to railsLecture #5 Introduction to rails
Lecture #5 Introduction to rails
 
12 Introduction to Rails
12 Introduction to Rails12 Introduction to Rails
12 Introduction to Rails
 
Supa fast Ruby + Rails
Supa fast Ruby + RailsSupa fast Ruby + Rails
Supa fast Ruby + Rails
 
Jasig Rubyon Rails
Jasig Rubyon RailsJasig Rubyon Rails
Jasig Rubyon Rails
 
Learning to code for startup mvp session 3
Learning to code for startup mvp session 3Learning to code for startup mvp session 3
Learning to code for startup mvp session 3
 
Ruby On Rails
Ruby On RailsRuby On Rails
Ruby On Rails
 
Ruby on Rails : 簡介與入門
Ruby on Rails : 簡介與入門Ruby on Rails : 簡介與入門
Ruby on Rails : 簡介與入門
 
td_mxc_rubyrails_shin
td_mxc_rubyrails_shintd_mxc_rubyrails_shin
td_mxc_rubyrails_shin
 
td_mxc_rubyrails_shin
td_mxc_rubyrails_shintd_mxc_rubyrails_shin
td_mxc_rubyrails_shin
 
Ruby projects of interest for DevOps
Ruby projects of interest for DevOpsRuby projects of interest for DevOps
Ruby projects of interest for DevOps
 
Ricardo Sanchez - Ruby projects of interest for devops
Ricardo Sanchez - Ruby projects of interest for devopsRicardo Sanchez - Ruby projects of interest for devops
Ricardo Sanchez - Ruby projects of interest for devops
 
Ruby on Rails introduction
Ruby on Rails introduction Ruby on Rails introduction
Ruby on Rails introduction
 
Ruby on-rails-101-presentation-slides-for-a-five-day-introductory-course-1194...
Ruby on-rails-101-presentation-slides-for-a-five-day-introductory-course-1194...Ruby on-rails-101-presentation-slides-for-a-five-day-introductory-course-1194...
Ruby on-rails-101-presentation-slides-for-a-five-day-introductory-course-1194...
 
Ruby on rails for beginers
Ruby on rails for beginersRuby on rails for beginers
Ruby on rails for beginers
 
An introduction to Rails 3
An introduction to Rails 3An introduction to Rails 3
An introduction to Rails 3
 
Getting Started with Rails
Getting Started with RailsGetting Started with Rails
Getting Started with Rails
 
Rails - getting started
Rails - getting startedRails - getting started
Rails - getting started
 
Introduction to Ruby on Rails
Introduction to Ruby on RailsIntroduction to Ruby on Rails
Introduction to Ruby on Rails
 
Ruby On Rails Tutorial
Ruby On Rails TutorialRuby On Rails Tutorial
Ruby On Rails Tutorial
 

Recently uploaded

Gen Z and the marketplaces - let's translate their needs
Gen Z and the marketplaces - let's translate their needsGen Z and the marketplaces - let's translate their needs
Gen Z and the marketplaces - let's translate their needs
Laura Szabó
 
Discover the benefits of outsourcing SEO to India
Discover the benefits of outsourcing SEO to IndiaDiscover the benefits of outsourcing SEO to India
Discover the benefits of outsourcing SEO to India
davidjhones387
 
Bridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptx
Bridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptxBridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptx
Bridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptx
Brad Spiegel Macon GA
 
办理新西兰奥克兰大学毕业证学位证书范本原版一模一样
办理新西兰奥克兰大学毕业证学位证书范本原版一模一样办理新西兰奥克兰大学毕业证学位证书范本原版一模一样
办理新西兰奥克兰大学毕业证学位证书范本原版一模一样
xjq03c34
 
留学挂科(UofM毕业证)明尼苏达大学毕业证成绩单复刻办理
留学挂科(UofM毕业证)明尼苏达大学毕业证成绩单复刻办理留学挂科(UofM毕业证)明尼苏达大学毕业证成绩单复刻办理
留学挂科(UofM毕业证)明尼苏达大学毕业证成绩单复刻办理
uehowe
 
manuaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaal
manuaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaalmanuaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaal
manuaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaal
wolfsoftcompanyco
 
Internet of Things in Manufacturing: Revolutionizing Efficiency & Quality | C...
Internet of Things in Manufacturing: Revolutionizing Efficiency & Quality | C...Internet of Things in Manufacturing: Revolutionizing Efficiency & Quality | C...
Internet of Things in Manufacturing: Revolutionizing Efficiency & Quality | C...
CIOWomenMagazine
 
国外证书(Lincoln毕业证)新西兰林肯大学毕业证成绩单不能毕业办理
国外证书(Lincoln毕业证)新西兰林肯大学毕业证成绩单不能毕业办理国外证书(Lincoln毕业证)新西兰林肯大学毕业证成绩单不能毕业办理
国外证书(Lincoln毕业证)新西兰林肯大学毕业证成绩单不能毕业办理
zoowe
 
[HUN][hackersuli] Red Teaming alapok 2024
[HUN][hackersuli] Red Teaming alapok 2024[HUN][hackersuli] Red Teaming alapok 2024
[HUN][hackersuli] Red Teaming alapok 2024
hackersuli
 
一比一原版(SLU毕业证)圣路易斯大学毕业证成绩单专业办理
一比一原版(SLU毕业证)圣路易斯大学毕业证成绩单专业办理一比一原版(SLU毕业证)圣路易斯大学毕业证成绩单专业办理
一比一原版(SLU毕业证)圣路易斯大学毕业证成绩单专业办理
keoku
 
存档可查的(USC毕业证)南加利福尼亚大学毕业证成绩单制做办理
存档可查的(USC毕业证)南加利福尼亚大学毕业证成绩单制做办理存档可查的(USC毕业证)南加利福尼亚大学毕业证成绩单制做办理
存档可查的(USC毕业证)南加利福尼亚大学毕业证成绩单制做办理
fovkoyb
 
Meet up Milano 14 _ Axpo Italia_ Migration from Mule3 (On-prem) to.pdf
Meet up Milano 14 _ Axpo Italia_ Migration from Mule3 (On-prem) to.pdfMeet up Milano 14 _ Axpo Italia_ Migration from Mule3 (On-prem) to.pdf
Meet up Milano 14 _ Axpo Italia_ Migration from Mule3 (On-prem) to.pdf
Florence Consulting
 
7 Best Cloud Hosting Services to Try Out in 2024
7 Best Cloud Hosting Services to Try Out in 20247 Best Cloud Hosting Services to Try Out in 2024
7 Best Cloud Hosting Services to Try Out in 2024
Danica Gill
 
可查真实(Monash毕业证)西澳大学毕业证成绩单退学买
可查真实(Monash毕业证)西澳大学毕业证成绩单退学买可查真实(Monash毕业证)西澳大学毕业证成绩单退学买
可查真实(Monash毕业证)西澳大学毕业证成绩单退学买
cuobya
 
制作毕业证书(ANU毕业证)莫纳什大学毕业证成绩单官方原版办理
制作毕业证书(ANU毕业证)莫纳什大学毕业证成绩单官方原版办理制作毕业证书(ANU毕业证)莫纳什大学毕业证成绩单官方原版办理
制作毕业证书(ANU毕业证)莫纳什大学毕业证成绩单官方原版办理
cuobya
 
不能毕业如何获得(USYD毕业证)悉尼大学毕业证成绩单一比一原版制作
不能毕业如何获得(USYD毕业证)悉尼大学毕业证成绩单一比一原版制作不能毕业如何获得(USYD毕业证)悉尼大学毕业证成绩单一比一原版制作
不能毕业如何获得(USYD毕业证)悉尼大学毕业证成绩单一比一原版制作
bseovas
 
Should Repositories Participate in the Fediverse?
Should Repositories Participate in the Fediverse?Should Repositories Participate in the Fediverse?
Should Repositories Participate in the Fediverse?
Paul Walk
 
APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024
APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024
APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024
APNIC
 
重新申请毕业证书(RMIT毕业证)皇家墨尔本理工大学毕业证成绩单精仿办理
重新申请毕业证书(RMIT毕业证)皇家墨尔本理工大学毕业证成绩单精仿办理重新申请毕业证书(RMIT毕业证)皇家墨尔本理工大学毕业证成绩单精仿办理
重新申请毕业证书(RMIT毕业证)皇家墨尔本理工大学毕业证成绩单精仿办理
vmemo1
 
成绩单ps(UST毕业证)圣托马斯大学毕业证成绩单快速办理
成绩单ps(UST毕业证)圣托马斯大学毕业证成绩单快速办理成绩单ps(UST毕业证)圣托马斯大学毕业证成绩单快速办理
成绩单ps(UST毕业证)圣托马斯大学毕业证成绩单快速办理
ysasp1
 

Recently uploaded (20)

Gen Z and the marketplaces - let's translate their needs
Gen Z and the marketplaces - let's translate their needsGen Z and the marketplaces - let's translate their needs
Gen Z and the marketplaces - let's translate their needs
 
Discover the benefits of outsourcing SEO to India
Discover the benefits of outsourcing SEO to IndiaDiscover the benefits of outsourcing SEO to India
Discover the benefits of outsourcing SEO to India
 
Bridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptx
Bridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptxBridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptx
Bridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptx
 
办理新西兰奥克兰大学毕业证学位证书范本原版一模一样
办理新西兰奥克兰大学毕业证学位证书范本原版一模一样办理新西兰奥克兰大学毕业证学位证书范本原版一模一样
办理新西兰奥克兰大学毕业证学位证书范本原版一模一样
 
留学挂科(UofM毕业证)明尼苏达大学毕业证成绩单复刻办理
留学挂科(UofM毕业证)明尼苏达大学毕业证成绩单复刻办理留学挂科(UofM毕业证)明尼苏达大学毕业证成绩单复刻办理
留学挂科(UofM毕业证)明尼苏达大学毕业证成绩单复刻办理
 
manuaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaal
manuaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaalmanuaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaal
manuaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaal
 
Internet of Things in Manufacturing: Revolutionizing Efficiency & Quality | C...
Internet of Things in Manufacturing: Revolutionizing Efficiency & Quality | C...Internet of Things in Manufacturing: Revolutionizing Efficiency & Quality | C...
Internet of Things in Manufacturing: Revolutionizing Efficiency & Quality | C...
 
国外证书(Lincoln毕业证)新西兰林肯大学毕业证成绩单不能毕业办理
国外证书(Lincoln毕业证)新西兰林肯大学毕业证成绩单不能毕业办理国外证书(Lincoln毕业证)新西兰林肯大学毕业证成绩单不能毕业办理
国外证书(Lincoln毕业证)新西兰林肯大学毕业证成绩单不能毕业办理
 
[HUN][hackersuli] Red Teaming alapok 2024
[HUN][hackersuli] Red Teaming alapok 2024[HUN][hackersuli] Red Teaming alapok 2024
[HUN][hackersuli] Red Teaming alapok 2024
 
一比一原版(SLU毕业证)圣路易斯大学毕业证成绩单专业办理
一比一原版(SLU毕业证)圣路易斯大学毕业证成绩单专业办理一比一原版(SLU毕业证)圣路易斯大学毕业证成绩单专业办理
一比一原版(SLU毕业证)圣路易斯大学毕业证成绩单专业办理
 
存档可查的(USC毕业证)南加利福尼亚大学毕业证成绩单制做办理
存档可查的(USC毕业证)南加利福尼亚大学毕业证成绩单制做办理存档可查的(USC毕业证)南加利福尼亚大学毕业证成绩单制做办理
存档可查的(USC毕业证)南加利福尼亚大学毕业证成绩单制做办理
 
Meet up Milano 14 _ Axpo Italia_ Migration from Mule3 (On-prem) to.pdf
Meet up Milano 14 _ Axpo Italia_ Migration from Mule3 (On-prem) to.pdfMeet up Milano 14 _ Axpo Italia_ Migration from Mule3 (On-prem) to.pdf
Meet up Milano 14 _ Axpo Italia_ Migration from Mule3 (On-prem) to.pdf
 
7 Best Cloud Hosting Services to Try Out in 2024
7 Best Cloud Hosting Services to Try Out in 20247 Best Cloud Hosting Services to Try Out in 2024
7 Best Cloud Hosting Services to Try Out in 2024
 
可查真实(Monash毕业证)西澳大学毕业证成绩单退学买
可查真实(Monash毕业证)西澳大学毕业证成绩单退学买可查真实(Monash毕业证)西澳大学毕业证成绩单退学买
可查真实(Monash毕业证)西澳大学毕业证成绩单退学买
 
制作毕业证书(ANU毕业证)莫纳什大学毕业证成绩单官方原版办理
制作毕业证书(ANU毕业证)莫纳什大学毕业证成绩单官方原版办理制作毕业证书(ANU毕业证)莫纳什大学毕业证成绩单官方原版办理
制作毕业证书(ANU毕业证)莫纳什大学毕业证成绩单官方原版办理
 
不能毕业如何获得(USYD毕业证)悉尼大学毕业证成绩单一比一原版制作
不能毕业如何获得(USYD毕业证)悉尼大学毕业证成绩单一比一原版制作不能毕业如何获得(USYD毕业证)悉尼大学毕业证成绩单一比一原版制作
不能毕业如何获得(USYD毕业证)悉尼大学毕业证成绩单一比一原版制作
 
Should Repositories Participate in the Fediverse?
Should Repositories Participate in the Fediverse?Should Repositories Participate in the Fediverse?
Should Repositories Participate in the Fediverse?
 
APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024
APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024
APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024
 
重新申请毕业证书(RMIT毕业证)皇家墨尔本理工大学毕业证成绩单精仿办理
重新申请毕业证书(RMIT毕业证)皇家墨尔本理工大学毕业证成绩单精仿办理重新申请毕业证书(RMIT毕业证)皇家墨尔本理工大学毕业证成绩单精仿办理
重新申请毕业证书(RMIT毕业证)皇家墨尔本理工大学毕业证成绩单精仿办理
 
成绩单ps(UST毕业证)圣托马斯大学毕业证成绩单快速办理
成绩单ps(UST毕业证)圣托马斯大学毕业证成绩单快速办理成绩单ps(UST毕业证)圣托马斯大学毕业证成绩单快速办理
成绩单ps(UST毕业证)圣托马斯大学毕业证成绩单快速办理
 

Jasig rubyon rails

  • 1. Ruby on Rails: An Introduction JA-SIG Summer Conference 2007 Michael Irion The University of Tulsa
  • 2. What is Ruby on Rails (RoR)? • Ruby on Rails is an open-source, full-stack framework for developing database-backed web applications according to the Model-View-Control pattern
  • 3. Overview of Ruby on Rails • Ruby is the programming language used to manipulate the framework • Rails is the framework that provides the necessary infrastructure • Rails is written in Ruby
  • 4. Ruby Features • Ruby is an interpreted language (No compile step necessary) • Ruby is an Object Oriented language. • Everything is an object (No primitives) • Ruby draws from Perl, Smalltalk, and Lisp
  • 5. Duck Typing in Ruby • Objects are dynamic, and their types are determined at runtime • The type of a Ruby object is much less important than it’s capabilities • If a Ruby object walks like a duck and talks like a duck, then it can be treated as a duck
  • 6. Rails Philosophy • Convention over configuration (no XML) • Don’t Repeat Yourself (DRY) • Rails is opinionated
  • 7. Rails Architecture • Rails applications are implemented using the Model- View-Controller (MVC) • Model - ActiveRecord • View - ActionView • Controller - ActionController
  • 8. Starting the Bookmarks Application • Generate application > rails bookmarks
  • 9. Directory Layout • Rails applications have a common directory structure • /app - the MVC core /controllers /helpers - provides extra functionality for views /models /views/nameofcontroller - templates for controller actions
  • 10. Directory Layout /components - will be deprecated /config - database, route and environment configuration /db - database schema and migrations /lib - functions that don’t map to MVC /log /public - static web resources (html, css, javascript etc.) /script - rails utilities /test - tests and fixtures /tmp /vendor - 3rd party plugins
  • 11. Rails Environment Modes • Rails runs in different modes, depending on the parameters given to the server on startup. Each mode defaults to it’s own database schema • Development (verbose logging and error messages) • Test • Production
  • 12. Starting Rails • > cd /directorypath/bookmarks • Start the server > ruby script/server start • Default environment is development • Default port is 3000 • http://127.0.0.1:3000
  • 13. Welcome Aboard - Now What? • Hello world seems appropriate >ruby script/generate controller hello exists app/controllers/ exists app/helpers/ create app/views/hello exists test/functional/ create app/controllers/hello_controller.rb create test/functional/hello_controller_test.rb create app/helpers/hello_helper.rb
  • 14. Mapping URLs to Controllers and Actions • http://127.0.0.1:3000/hello/sayit • http://127.0.0.1:3000 - address and port of the webserver • hello - name of the controller • sayit - name of the action (method in controller)
  • 15. Editing the Controller def sayit render :text => "<h2>Hello World!</h2>" end
  • 16. Now for an actual Bookmark • Edit config/database.yml development: adapter: mysql database: bookmarks_development username: username password: password host: localhost
  • 17. Create the Database • This step depends on the database and dba tool of your choice. Create a new schema/dbname for bookmarks_development, and assign rights for the user you listed in database.yml.
  • 18. Bookmark Model • Our bookmark model will (initially) need two properties • URL • Title
  • 19. Scaffolding for Bookmarks • Rails can generate all the basic CRUD operations for simple models via scaffolding. • Scaffolding is temporary way to get applications wired quickly. > ruby script/generate scaffold_resource bookmark url:string title:string
  • 20. Migrations • Rails uses migrations to version the database. • Rails tries to minimize SQL at every opportunity • Migrations are automatically created whenever you generate a new model • Migration files are located in db/migrations • The version number is stored in a table called schema_info
  • 21. Bookmarks Migration located in db/migrate/001_create_bookmarks.rb class CreateBookmarks < ActiveRecord::Migration def self.up create_table :bookmarks do |t| t.column :url, :string t.column :title, :string end end def self.down drop_table :bookmarks end end
  • 22. Running the Migration • Rake is the general purpose build tool for rails, much like make, or ant. It has many functions, one of which is to control migrations. >rake db:migrate • Now the table has been created
  • 23. Bookmarks Table ID • Bookmarks table has the following fields - id, url, and title • Where did the id field come from? • Convention of configuration - Rails automatically creates an id field for each new table and uses it as the primary key
  • 24. Bookmarks Controller The /app/controllers/bookmarks.rb default action: def index @bookmarks = Bookmark.find(:all) respond_to do |format| format.html # index.rhtml format.xml { render :xml => @bookmarks.to_xml } end End
  • 25. Mapping URLs to Controllers and Actions • http://127.0.0.1:3000/bookmarks/ • http://127.0.0.1:3000 - address and port of the webserver • hello - name of the controller • / - name of the action (blank maps to the index action)
  • 26. Bookmarks Model - Don’t Repeat Yourself • No getters/setters. Rails uses information from the database. class Bookmark < ActiveRecord::Base end
  • 27. Bookmarks View Located in views/bookmarks/index.rhtml <% for bookmark in @bookmarks %> <tr> <td><%=h bookmark.url %></td> <td><%=h bookmark.title %></td> <td><%=h bookmark.description %></td> <td><%= link_to 'Show', bookmark_path(bookmark) %></td> <td><%= link_to 'Edit', edit_bookmark_path(bookmark) %></td> <td><%= link_to 'Destroy', bookmark_path(bookmark), :confirm => 'Are you sure?', :method => :delete %></td> </tr> <% end %>
  • 28. Convention over Configuration • Bookmark model automatically looks for a table in the database with a plural form of it’s name. (bookmarks) • The Bookmarks controller automatically renders the template located in views according to the controller name and the action (views/bookmarks/index.rhtml)
  • 29. Ajax Helpers • Rails allows you to program many AJAX calls in ruby, instead of writing javascript directly • Script.aculo.us and Prototype libraries are included • A quick example. Autocomplete for text boxes
  • 30. AJAX Autocomplete Add to Bookmarks controller auto_complete_for :bookmarks, :url Add to views/bookmarks/edit.rhtml <%= text_field_with_auto_complete :bookmark, :url %> In views/layouts/bookmarks.rhtml, add <%= javascript_include_tag :defaults %>
  • 31. Validations • Rails has a number of validation helpers that can be added to the model. class Bookmark < ActiveRecord::Base validates_presence_of :url, :title end
  • 32. Validations • validates_presence_of • validates_length_of • validates_acceptance_of • validates_confirmation_of • validates_uniqueness_of • validates_format_of • validates_numericality_of • validates_inclusion_in • validates_exclusion_of • validates_associated :relation
  • 33. Associations - Adding Categories to Bookmarks • The bookmarks are working great, but it would be nice if we could group them by category • Rails uses associations to build relationships between tables • Associations are independent of database foreign key constraints
  • 34. Types of Associations • has_one • belongs_to • has_many • has_and_belongs_to_many • has_many :model1, :through => :model2
  • 35. Changes to the Database • A new categories table needs to be created • A category_id field needs to be added to the bookmarks table > ruby script/generate scaffold_resource category name:string • This creates the all the scaffolding and the migration db/migrate/002_create_categories.rb • Note the category table is pluralized as categories. >ruby script/generate migration alter_bookmarks_add_category_id • This creates db/migrate/003_alter_bookmarks_add_category_id.rb
  • 36. Alter Bookmarks Migration def self.up add_column :bookmarks, :category_id, :integer end def self.down remove_column :bookmarks, :category_id end >rake db:migrate
  • 37. Types of Associations • has_one • belongs_to • has_many • has_and_belongs_to_many • has_many :model1, :through => :model2
  • 38. Database Relationships • Parent (or Master) models that have collections of other models use the has_many relationship • Child (or Detail) models contain the id field of their parent record and they use the belongs_to relationship
  • 39. Associations Model Code class Bookmark < ActiveRecord::Base validates_presence_of :url, :title belongs_to :category end class Category < ActiveRecord::Base has_many :bookmarks end
  • 40. Associations Controller code def edit @bookmark = Bookmark.find(params[:id]) @categories = Category.find(:all) end
  • 41. Associations View Code <p> <b>Category</b><br /> <%= collection_select('bookmark', 'category_id', @categories, 'id', 'name') %> </p>
  • 42. Tools • Textmate (Mac OS X) • RadRails (Eclipse plugin) www.radrails.org • Other commercial and opensource IDEs are being made available
  • 43. Resources • Programming Ruby: The Pragmatic Programmers' Guide - Second Edition • Agile Web Development with Rails—Second Edition • Rails main site http://www.rubyonrails.com • My email: michael-irion@utulsa.edu

Editor's Notes

  1. Don&amp;apos;t repeat yourself&amp;quot; means that information is located in a single, unambiguous place. For example, using ActiveRecord, the developer does not need to specify column names in class definitions. Instead, Ruby can retrieve this information from the database.&amp;quot;Convention over Configuration&amp;quot; means a developer only needs to specify unconventional aspects of their application. For example, if there&amp;apos;s a class Sale in the model, the corresponding table in the database is called sales by default. It is only if someone deviates from this convention, such as calling the table &amp;quot;products_sold&amp;quot;, that they need to write code regarding these names.makes it easer to do the right thing
  2. Note that ‘ruby’ is only necessary on windows Load up the app in firefox
  3. Discuss where the files end up
  4. Note the host and the
  5. Go take a look at the result
  6. Go over what this file means Including blocks
  7. Creates the database table
  8. Go to controller to explain variables Link_to help Discuss the difference between commands and strings