Ruby on Rails : 簡介與入門

77,570 views

Published on

Ruby on Rails : 簡介與入門

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

×