Ruby on Rails Part1: ihower@gmail.com http://creativecommons.org/licenses/by-nc/2.5/tw/
• (a.k.a ihower) http://ihower.idv.tw/blog/
• (a.k.a ihower) http://ihower.idv.tw/blog/ • http://handlino.com
• (a.k.a ihower) http://ihower.idv.tw/blog/ • http://handlino.com
Rails? • ( MIT ) Web database-backed • MVC (Model-View-Control ) • ( ) Ruby Ajax ORM (object-relational-mapping) • 2004 David Heinemeier Hanson(DHH) 37signals
• Ruby • Don’t Repeat Yourself (DRY) • • • Convention Over Configuration • •
Ruby on Rails ? • ( ) • ( Joomla Durpal CMS •
Rails • 2005 DHH Hacker • 2006 Rails Jolt • 2005~2006 Ruby/Rails 1552% • Ruby Tiobe 26 10
Rails ? Java(Spring/Hibernate) Rails 4 20 4 ( 5 ) 3293 1164 1161 113 / 62/549 55/126 Justin Gehtland Java Rails
Rails ? • Justin Gehtland Java :Rails = 3.5 : 1 • Proc.net PHP : Rails = 10 : 1 • JavaEye JAVA : Rails = 10 : 1 • thegiive PHP : Rails = 8 : 1
Rails clone
Rails ? M V C MVC Model-View-Control
DB schema Ruby class CreatePeople < ActiveRecord::Migration def self.up create_table :people do |t| t.string :name t.integer :age t.date :birthday t.text :bio t.timestamps end end def self.down drop_table :people end end
Active Record ORM class Person < ActiveRecord::Base # end person = Person.new person.name "ihower" person.age = 18 person.save person = Person.find(1) puts person.name # ihower
Action Controller HTTP request class PeopleController < ApplicationController # GET /people def index @people = Person.all end end
Action Controller HTTP request class PeopleController < ApplicationController method action # GET /people def index @people = Person.all end end
Action Controller HTTP request class PeopleController < ApplicationController method action # GET /people def index @people = Person.all end instance variable end View
Action View Ruby HTML <html> <body> <h1>Guestbook</h1> <% @people.each do |person| %> <p><%= person.name %>: <%= person.bio %></p> <% end %> </body> </html>
Why Rails? • • • (prototyping) • •
Because Rails is ... • MVC • (Ajax RESTful ) • • • • Ruby ( ) • ActiveRecord ORM • Migration • (DRY)
Thank you. Get to the Point! (http://johnwlong.com/slides/gettothepoint/) Ruby on Rails slide by thegiive in COSCUP Delivery of the key adoption Factors and key characteristics of companies using ruby on rails by Michel Barbosa
Ruby on Rails Part2: ihower@gmail.com http://creativecommons.org/licenses/by-nc/2.5/tw/
README • http://ihower.idv.tw/blog/archives/1743 • http://ihower.idv.tw/course/rails.html
Web framework? • • Perl CGI PHP ASP MVC Database URL Template Cookie Ajax .... • Framework Framework
MVC? • Model Controller View • Model • • View • Ruby HTML • Controller Model • (e.g. ) Request Model View (e.g. HTML)
1. Controller 2. 3. 4. View Model DB
Rails MVC? Routing 1. 2. ActionController 4. 3. 5. ActionView ActiveRecord DB
MVC? • • (DRY: Don’t repeat yourself) •
Ruby on Rails • Ruby 1.8.6 1.8.7 • Rubygems (Ruby ) • Rails SQLite 3 MySQL Postgres Oracle DB2 MSSQL
Rails Hello World!(live demo & )
(Routes) http://localhost:3000/welcome/say Controller Action # /config/routes.rb map.connect ':controller/:action/:id' map.connect ':controller/:action/:id.:format'
Model (live demo)
ORM ? • ActiveRecord Rails ORM • ORM: Object-Relational Mapping • (table) (classe) • • (row) (object) • • (column) (object attribute)
script/console ORM ( )
script/* ? • script/about • script/console • script/generate • script/server • script/plugin
Rake? • Make Ruby • # lib/tasks/my_task.rake namespace :my desc "task description" task :foo_task => :environment do puts “foobarrrr” end end rake my:foo_task
Rake ? • rake -T • rake db:migrate • rake db:drop • rake tmp:clear • rake notes
DB Migration? • Ruby (Schema) • • e.g. ?? • • e.g. SQLite3 MySQL Postgres...etc • Migration
Rails • app • log • controllers • public • helpers • script • models • test • views • tmp • config • vendor • db • doc • lib
Rails environments • development, production, test mode • • Log level Session store, custom library, Email setting • environment
controller view (live demo & )
controller view (live demo & )
controller view (live demo & )
controller view (live demo & )
Helper ? • view template helper method • link_to • form_for •h (XSS ) • /app/helpers/* Helper
Layout (live demo & ) • View Layout HTML • /app/views/layouts/application.html.erb
flash hash (live demo & ) • flash hash redirect action •
DRY: Partial template (live demo & ) • View •
DRY: before_filter (live demo & ) • Controller • ID Model
Model Validation (live demo & ) • post.rb validates_presence_of :title • new.html.erb edit.html.erb <%= error_messages_for :post %> helper
Thank you. Agile Web Development with Rails 3rd. RailsGuides http://guides.rubyonrails.org

Ruby on Rails : 簡介與入門

  • 1.
    Ruby on Rails Part1: ihower@gmail.com http://creativecommons.org/licenses/by-nc/2.5/tw/
  • 2.
    (a.k.a ihower) http://ihower.idv.tw/blog/
  • 3.
    (a.k.a ihower) http://ihower.idv.tw/blog/ • http://handlino.com
  • 4.
    (a.k.a ihower) http://ihower.idv.tw/blog/ • http://handlino.com
  • 5.
    Rails? • ( MIT ) Web database-backed • MVC (Model-View-Control ) • ( ) Ruby Ajax ORM (object-relational-mapping) • 2004 David Heinemeier Hanson(DHH) 37signals
  • 6.
    Ruby • Don’t Repeat Yourself (DRY) • • • Convention Over Configuration • •
  • 7.
    Ruby on Rails ? • ( ) • ( Joomla Durpal CMS •
  • 8.
    Rails • 2005 DHH Hacker • 2006 Rails Jolt • 2005~2006 Ruby/Rails 1552% • Ruby Tiobe 26 10
  • 9.
    Rails ? Java(Spring/Hibernate) Rails 4 20 4 ( 5 ) 3293 1164 1161 113 / 62/549 55/126 Justin Gehtland Java Rails
  • 10.
    Rails ? • Justin Gehtland Java :Rails = 3.5 : 1 • Proc.net PHP : Rails = 10 : 1 • JavaEye JAVA : Rails = 10 : 1 • thegiive PHP : Rails = 8 : 1
  • 11.
  • 12.
    Rails ? M V C MVC Model-View-Control
  • 13.
    DB schema Ruby class CreatePeople < ActiveRecord::Migration def self.up create_table :people do |t| t.string :name t.integer :age t.date :birthday t.text :bio t.timestamps end end def self.down drop_table :people end end
  • 14.
    Active Record ORM class Person < ActiveRecord::Base # end person = Person.new person.name "ihower" person.age = 18 person.save person = Person.find(1) puts person.name # ihower
  • 15.
    Action Controller HTTP request class PeopleController < ApplicationController # GET /people def index @people = Person.all end end
  • 16.
    Action Controller HTTP request class PeopleController < ApplicationController method action # GET /people def index @people = Person.all end end
  • 17.
    Action Controller HTTP request class PeopleController < ApplicationController method action # GET /people def index @people = Person.all end instance variable end View
  • 18.
    Action View Ruby HTML <html> <body> <h1>Guestbook</h1> <% @people.each do |person| %> <p><%= person.name %>: <%= person.bio %></p> <% end %> </body> </html>
  • 19.
    Why Rails? • • • (prototyping) • •
  • 20.
    Because Rails is... • MVC • (Ajax RESTful ) • • • • Ruby ( ) • ActiveRecord ORM • Migration • (DRY)
  • 21.
    Thank you. Get tothe Point! (http://johnwlong.com/slides/gettothepoint/) Ruby on Rails slide by thegiive in COSCUP Delivery of the key adoption Factors and key characteristics of companies using ruby on rails by Michel Barbosa
  • 22.
    Ruby on Rails Part2: ihower@gmail.com http://creativecommons.org/licenses/by-nc/2.5/tw/
  • 23.
    README • http://ihower.idv.tw/blog/archives/1743 • http://ihower.idv.tw/course/rails.html
  • 24.
    Web framework? • • Perl CGI PHP ASP MVC Database URL Template Cookie Ajax .... • Framework Framework
  • 25.
    MVC? • Model Controller View • Model • • View • Ruby HTML • Controller Model • (e.g. ) Request Model View (e.g. HTML)
  • 26.
    1. Controller 2. 3. 4. View Model DB
  • 27.
    Rails MVC? Routing 1. 2. ActionController 4. 3. 5. ActionView ActiveRecord DB
  • 28.
    MVC? • • (DRY: Don’t repeat yourself) •
  • 29.
    Ruby on Rails •Ruby 1.8.6 1.8.7 • Rubygems (Ruby ) • Rails SQLite 3 MySQL Postgres Oracle DB2 MSSQL
  • 30.
  • 31.
    (Routes) http://localhost:3000/welcome/say Controller Action # /config/routes.rb map.connect ':controller/:action/:id' map.connect ':controller/:action/:id.:format'
  • 32.
  • 33.
    ORM ? • ActiveRecord Rails ORM • ORM: Object-Relational Mapping • (table) (classe) • • (row) (object) • • (column) (object attribute)
  • 34.
  • 35.
    script/* ? • script/about • script/console • script/generate • script/server • script/plugin
  • 36.
    Rake? • Make Ruby • # lib/tasks/my_task.rake namespace :my desc "task description" task :foo_task => :environment do puts “foobarrrr” end end rake my:foo_task
  • 37.
    Rake ? • rake -T • rake db:migrate • rake db:drop • rake tmp:clear • rake notes
  • 38.
    DB Migration? • Ruby (Schema) • • e.g. ?? • • e.g. SQLite3 MySQL Postgres...etc • Migration
  • 39.
    Rails • app • log • controllers • public • helpers • script • models • test • views • tmp • config • vendor • db • doc • lib
  • 40.
    Rails environments • development, production, test mode • • Log level Session store, custom library, Email setting • environment
  • 41.
    controller view (live demo & )
  • 42.
    controller view (live demo & )
  • 43.
    controller view (live demo & )
  • 44.
    controller view (live demo & )
  • 45.
    Helper ? • view template helper method • link_to • form_for •h (XSS ) • /app/helpers/* Helper
  • 46.
    Layout (live demo & ) • View Layout HTML • /app/views/layouts/application.html.erb
  • 47.
    flash hash (live demo & ) • flash hash redirect action •
  • 48.
    DRY: Partial template (live demo & ) • View •
  • 49.
    DRY: before_filter (live demo & ) • Controller • ID Model
  • 50.
    Model Validation (live demo & ) • post.rb validates_presence_of :title • new.html.erb edit.html.erb <%= error_messages_for :post %> helper
  • 51.
    Thank you. Agile WebDevelopment with Rails 3rd. RailsGuides http://guides.rubyonrails.org