Web Development with Ruby on Rails, MyGOSSCON 2007
-
date post
12-Sep-2014 -
Category
Technology
-
view
7.262 -
download
0
description
Transcript of Web Development with Ruby on Rails, MyGOSSCON 2007
Web Development with Ruby on RailsMyGOSSCON 2007
Kamal Fariz Mahyuddin RSB
A full-stack framework for developing database-backed web applications according to the Model-View-Controller pattern.
A full-stack framework for developing database-backed web applications according to the Model-View-Controller pattern.
A bunch of stuff that makes web developers
happy!
- David Heinemeier Hansson http://flickr.com/photos/pragdave/174964316/
2005476
> 1 million downloads
Hundreds of plugins
~10k people on rubyonrails-talk
Oracle
SQL Server
MySQL
PostgreSQL
SQLite
DB2
InformixFirebird
SybaseASA
Ruby on RailsModel-View-Controller
MVC is like a bakery shop.
ActionControllerModel-View-Controller
ActiveRecordModel-View-Controller
ActionViewModel-View-Controller
ActiveRecordModel-View-Controller
class Department < ActiveRecord::Base belongs_to :ministry has_many :officers has_many :projectsend
File: apps/models/department.rb
id name ministry_id
1 Jabatan Lanskap dan Taman 1
2 Jabatan Pembangunan Bandar 1
3 Jabatan Imigresen Malaysia 2
departments
id name
1 Kementerian Wilayah Persekutuan
2 Kementerian Hal Ehwal Dalam Negeri
ministries
dept = Department.find(1)=> #<Department id: 1, name: 'Jabatan Lanskap dan Taman'>
SELECT *FROM departmentsWHERE id = 1
dept.ministry=> #<Ministry id: 1, name: 'Kementerian Wilayah Persekutuan'>
SELECT *FROM ministriesWHERE id = 1
class Ministry < ActiveRecord::Base has_many :departmentsend
File: apps/models/ministry.rb
id name
1 Kementerian Wilayah Persekutuan
2 Kementerian Hal Ehwal Dalam Negeri
ministries
id name ministry_id
1 Jabatan Lanskap dan Taman 1
2 Jabatan Pembangunan Bandar 1
3 Jabatan Imigresen Malaysia 2
departments
ministry = Ministry.find(1)=> #<Ministry id: 1, name: 'Kementerian Wilayah Persekutuan'>
ministry.departments=> [#<Department id: 1, name: 'Jabatan Lanskap dan Taman'>, #<Department id: 2, name: 'Jabatan Pembangunan Bandar'>]
SELECT *FROM departmentsWHERE ministry_id = 1
Convention over Configuration
ActiveRecord Conventions
Model names are singular, e.g. Ministry.
Table names are plural, e.g. ministries.
Foreign-key columns are named model_id, e.g. ministry_id.
Foreign key column goes onto the model that belongs_to another model, e.g. departments table has a column named ministry_id.
ActionControllerModel-View-Controller
http://mampu.gov.my/departments/show/1
http://mampu.gov.my/departments/show/1
http://mampu.gov.my/controller/action/id
class DepartmentsController < ApplicationController
def show @dept = Department.find(params[:id]) end
end
/departments/show/1/controller/action/id
File: apps/controllers/departments_controller.rb
Department.find(1)
Remember this?
class DepartmentsController < ApplicationController
def show @dept = Department.find(params[:id]) end
end
/departments/show/1/controller/action/id
File: apps/controllers/departments_controller.rb
Convention over Configuration
ActionController Conventions
First segment of URL is the controller name,e.g. /departments/show/1, the controller is DepartmentsController.
Second segment of URL is the controller action,e.g. /departments/show/1, the action is show in DepartmentsController.
Third segment of URL is the ID,e.g. /departments/show/1, the value is accessible from params[:id].
ActionViewModel-View-Controller
class DepartmentsController < ApplicationController
def show @dept = Department.find(params[:id]) end
end
Remember this?
Department: Jabatan Lanskap dan TamanMinistry: Kementerian Wilayah Persekutuan
Projects:
• Next Generation Network
• e-Perolehan Integration
http://localhost/departments/show/1
<html> <body> <h1>Department: <%= @dept.name %></h1> <h2>Ministry: <%= @dept.ministry.name %></h2> <h2>Projects</h2> <ul> <%= render :partial => @dept.projects %> </ul> </body></html>
File: apps/views/departments/show.html.erb
<li><%= project.title %></li>
File: apps/views/projects/_project.html.erb
Department: Jabatan Lanskap dan TamanMinistry: Kementerian Wilayah Persekutuan
Projects:
• Next Generation Network
• e-Perolehan Integration
http://localhost/departments/show/1
Convention over Configuration
ActionView Conventions
Instance variables set in controller action are automatically available in view, e.g. @dept in previous example.
Values are displayed in view by enclosing it in <%= %>.
Loops can be extracted into partials.
Use Helpers as much as possible to code even less, e.g. link_to, form_for
Resources
Malaysia.rbMalaysia Ruby Brigade
~65 people in Google Groups~28 people in Facebook Group
Meets every third Thursday of the month
Malaysia.rb 3rd MeetupJuly 2007
http://groups.google.com/group/malaysia-rb
Local Companies on Rails
RSB
Freelance Developers
Local Ruby on Rails Sites
http://www.mampu.gov.my http://www.ravejoint.com
http://www.geotude.com http://www.haze.net.my
Sneak Preview of Time Together
Your Department’s Next Internal Web Application