Click here to load reader

Ruby On Rails Introduction [Εισαγωγή στο Rails]

  • View
    3.627

  • Download
    3

Embed Size (px)

DESCRIPTION

Μία περιεκτική εισαγωγή στo Rails framework γραμμένη εξ'ολοκλήρου στα ελληνικά. Έκδοση 1.0, 12 Ιαν 2007 Δημητρακόπουλος Νικόλαος, Άδεια Χρήσης Ελεύθερης Τεκμηρίωσης GNU Στα πλαίσια του ACM Student Chapter του Πανεπιστημίου Πελοποννήσου και του Ruby Hellenic Users Group

Text of Ruby On Rails Introduction [Εισαγωγή στο Rails]

  • 1. RUBY ON RAILS: [email_address] Association for Computing Machinery ACM. 12 2007 Web development that doesnt hurt! 1. 0

2.

  • Copyright 200 7Dimitrakopoulos Nikolaos
  • Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A link to the license is included in the section entitled "GNU Free Documentation License".

Copyright 200 7 , / GNU , 1.2 Free Software Foundation , . GNU". 3. Rails???

  • ( framework ) web Ruby
  • :
    • MVC
    • ORM ( )
    • AJAX
    • Web Services
    • Templating layout Ruby html, CSS, Javascript...
  • buzzwords ->web2.0

4. Outline

  • framework
  • Rails
  • Rails framework
  • -
  • -

I

  • /
  • /
  • /

5.

  • framework
  • Rails
  • Rails framework
    • DRY
    • Convention over configuration
    • MVC
    • Agile
    • Active Record
    • Action Pack
    • Prototype
    • Action Mailer
    • Action Web Service
    • Scripts
    • Migrations

6. framework

  • , framework:
  • :
    • scripting
  • framework / /
  • (.. MVC , Rails)
  • framework :
    • ,

framework! 7. Rails

  • Rails David Heinemeier Hansson basecamp.com ( / ( project management) 37signals )
  • web 2004.
  • 1.0 13 2005
  • 1.1 28 2006
  • 2006 Apple Mac OS X v10.5 ( Leopard) , 2007, Rails framework[2]
  • 2007 1.2

8. Rails (1)

  • 1 :
  • !( DRY Dont Repeat Yourself)
  • 2 :
  • !( convention over configuration)

9. Rails (2) : DRY

  • 1 ;
    • ( )
  • :
  • :
    • ( database schema )
    • ( test plans )
    • ( documentation )
  • , ,
  • , ,

10. Rails (3) : Convention over configuration

  • 2 ;
    • ..: Post posts
  • , Rails
  • , /

11. Rails ( 4 ) : Agile

  • Agile ,
  • () , ,
  • ,

12. Rails ( 5 ) :Agile Manifesto

  • ( )
  • ,
  • --
  • -

13. Rails ( 6 ) :MVC

  • Rails MVC ( Model-View-Controller) :
    • Model :
    • View : , /
    • Controller:

14. Rails (1)

  • Active Record
    • ( object-relation mapping ORM)
    • , /.
    • ..:table: people class Person
  • Action Pack
    • .
    • ( View) controllerMVC.
    • controller browser controller.
    • browser (.. html, xml,...)

15. Rails (2)

  • Prototype
    • AJAX site,drag-and-drop, ...
  • Action Mailer
    • emails.
  • Action Web Service
    • web service .
    • , :
      • SOAP
      • REST
      • XML-RPC
      • WSDL

16. Rails + MVC 17. (1): Scripts / Generators

  • Rails
  • ..:
    • controller,model , ...
    • controllers, .. scaffold
    • migration( )
  • / ->
  • , !

18. ( 2 ): !

  • ;
  • :
    • , ( browser server)
    • , !
  • ;

19. (3): !

  • !
  • () :
    • Development
    • Production
    • Testing
  • !
  • :
    • development,
    • testing
    • , , !

20. (4): Migrations

  • , ( )
  • ;
  • ;
  • :Migrations !

21. (5): Plugins

  • Rails plugins
  • plugin framework.
  • plugins :
    • rails
    • rails,
  • plugins:
    • acts_as_taggable mixin ActiveRecord( tags )
    • file_column ActiveRecord ( )
    • globalize ( ( i18n) Rails)

22.

  • :
    • Rails
    • web servers
  • :
    • Rails
    • web server
    • controllers
  • :
    • CRUD
    • Validations

23. (1) : Rails

  • Ruby Gems :
    • gem install rails --include-dependencies
    • sudo gem install rails --include-dependencies( *nix)
  • :
    • package-manager (.. aptdebian-based)
    • internet, ( web server, ,Ruby, Rails, , ... ) , :
      • Locomotive(Mac OS X)
      • Instant Rails( Windows)

24. ( 2 ) : web servers

  • Apache
  • Lighttpd
    • !web server :
    • FastCGI
    • SCGI
    • Mongrel
  • , server ( WEBrick ), development

25. ( 3 ) :

  • MySQL
  • PostgreSQL
  • SQLite
  • Oracle
  • SQL Server
  • DB2
  • Firebird

26. (1) : Rails

  • blog
  • rails (,scripts , ...)
  • C: path> rails simple-blog
      • createapp/controllers
      • createapp/helpers
      • createapp/models
      • createapp/views/layouts
      • createconfig/environments
      • createcomponents

27. ( 2 ) : controllers views O O views ( view controller) views controller ( migrations sql) ( style sheets,, ...) plugins( , ) 28. ( 3 ) : WEBrick server

  • C: ... simple-blog> ruby scriptserver

29. (4) : WEBrick server 30.

  • mysql :
  • C:mysqladmin -u root -p create simple-blog_developmentEnter password: ******
  • 1: . mysql
  • 2: , Rails 3 . development. :
    • xxx_development
    • xxx_production
    • xxx_testing

( 5 ) : 31. (6) :

  • Rails
  • config/database.yml
  • development .

development:adapter: mysqldatabase: simple-blog_developmentusername: rootpassword: host: localhost development:adapter: mysqldatabase: simple-blog_developmentusername: rootpassword:pass host: localhost

  • rake db:migrate
  • :(in C:/ /simple-blog)
  • : rake , ( )

32.

  • :
    • posts
    • post
    • , CRUD(Create, Read, Update, Delete)
  • Post :

(7) : 33.

  • ( )
  • rails :
  • C: ...>ruby scriptgenerate model post
  • :
  • createapp/models/post.rb
  • createtest/unit/post_test.rb
  • createtest/fixtures/posts.yml
  • createdb/migrate
  • createdb/migrate/001_create_posts.rb

(8) : 34.

  • :
    • SQL( command line, phpMyAdmin...)
    • rails:
      • ruby
      • rails ,
      • ,
      • ( ) DRY
  • migrations

( 9 ) : 35.

  • rails dbmigrate01_create_posts.rb

( 10 ) :

  • class CreatePosts < ActiveRecord::Migration
  • def self.up
  • create_table :posts do |t|
  • # t.column :name, :string
  • end
  • end
  • def self.down
  • drop_table :posts
  • end
  • end

, ! 36. ( 1 1) :

  • posts
  • :
    • title( string)
    • description (text)
    • creation_date ( )

class CreatePosts < ActiveRecord::Migration def self.up create_table :posts do |t| t.column :title, :string t.column :description, :text t.column :creation_date, :datetime endend def self.down drop_table :posts end end

  • posts
  • rake db:migrate :
  • == CreatePosts: migrating==============================================
  • -- create_table(:posts)
  • -> 0.7820s
  • == CreatePosts: migrated (0.7820s) ======================================

37.

  • :string ,:text
  • :integer ,:float
  • :datetime, :timestamp, :time, :date
  • :binary, :boolean

( 1 2) : 38.

  • app/models/post.rb
  • :
    • class Post < ActiveRecord::Base
    • end
  • ( )
  • posts!

( 1 3) : 39.

  • rails ( ) :
    • ( post) ( posts)
    • id
  • migrations,
  • SQL, ! ..
    • id
    • 1
  • ( )

( 1 4) : !!! 40.

  • ruby scriptgenerate scaffold post posts
  • browser ( http://localhost:3000/posts )

( 1 5) :Controller/View- Scaffolding 41.

  • :
    • controller routing
    • browser
    • Rails urls
    • , ,
    • http://localhost:3000/posts/show/1

( 1 6) :Controller/View- Scaffolding server O controller H . id 42.

  • ;
  • app/controllers/posts_controller.rb:
    • controller
    • index, list, show, new, edit create, update ( ) new, edit destroy
  • app/views/posts/list.rhtml
    • app/views/posts/show.rhtml
    • app/views/posts/new.rhtml
    • app/views/posts/edit.rhtml
    • view
    • controller .rhtml
    • controller
    • html ( ) , xml)

( 1 7) :Controller/View- Scaffolding 43.

  • ()
  • app/views/layouts/posts.rhtml
    • layout, controller
    • viewscontroller
      • Banners
      • Footers
      • Style sheet
      • ...
  • public/stylesheets/scaffold.css
    • ( stylesheets) css
    • scaffold , , css

( 1 8) :Controller/View- Scaffolding 44.

  • scaffold generator CRUD
  • :
    • controller / view
  • ( )

( 1 9) :Controller/View- Scaffolding 45.

  • , :
    • , scaffold
  • scaffold generator
  • 100% agile

(20) :Controller/View- Scaffolding 46. (1)

  • rails
  • on-line wikis...
  • , , :
    • CRUD
    • Validations

47.

  • :
  • - ,..
  • a_post =Post.new
  • a_post.title = Test
  • a_post.description = Double Test
  • a_post.save
  • :
  • all_posts = Post.find :all
  • test_posts = Post.find :all, :conditions => [ title =? , test]
  • the_first_post = Post.find :first,:group => id

( 2 ): CRUD 48. (3): Validations

  • Post :
    • ( post )
  • Post :
    • validates_presence_of :title, :description
    • validates_uniqueness_of :title

49.

  • validation , ..:
    • ...
  • validations:
    • validates_acceptance_of
    • validates_associeted
    • validates_format_of
    • validates_numericallity_of
    • validates_length_of
    • ...
  • validations :
    • validate
    • :
      • before_save
      • before_create, after_create
      • before_update
      • ...

(4): Validations 50.

  • rails ( )
  • () :
    • 1 1
    • 1
  • :
    • has_one : < > -belongs_to : < >
    • has_many : < > -belongs_to : < >
    • has_and_belongs_to_many : < >( 2 )

(5): 51. (6): ( ) 52. (7): ( ) 53. (8): ( ) 54.

  • ( )
  • Acts As List ( )
  • Acts As Tree( .. )

(9): ( ) 55.

  • /
  • /

56.

  • Rails
  • : --controller / view. !
  • :
    • Web Services
    • AJAX
    • Asterisk ( )
  • , / plugins

57. /

  • rails !
  • ,
  • frameworks Pearl, PHP,...
  • rails /
    • ( , web hosting ...)

58. 59. / Rails ;

  • , !
  • ( web)
  • ( Active Record)
  • Ruby Rails

60. / Rails ;

  • , .. ! PHP
  • Rails web hosting providers ,
  • Ruby ( !:-D)
  • ( , ...)

61. ...

  • &

: http://ruby-hellug.gr 62. -

  • Pragmatic Programmers:Agile software development using Rails [Second Edition]
  • www.rubyonrails.com
  • Rails cheatsheet( apirails)

63. GNU GNU Free Documentation Licence

  • http://www.fsf.org/licensing/licenses/fdl.txt