Ruby on Rails: An Introduction - PowerPoint PPT Presentation

1 / 43
About This Presentation
Title:

Ruby on Rails: An Introduction

Description:

Ruby on Rails is an open-source, full-stack framework for developing database ... Other commercial and opensource IDEs are being made available. Resources ... – PowerPoint PPT presentation

Number of Views:2584
Avg rating:3.0/5.0
Slides: 44
Provided by: steve1163
Category:

less

Transcript and Presenter's Notes

Title: Ruby on Rails: An Introduction


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 its 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)
  • Dont 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
  • gt 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 dont 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 its own database schema
  • Development (verbose logging and error messages)
  • Test
  • Production

12
Starting Rails
  • gt cd /directorypath/bookmarks
  • Start the server
  • gt ruby script/server start
  • Default environment is development
  • Default port is 3000
  • http//127.0.0.13000

13
Welcome Aboard - Now What?
  • Hello world seems appropriate
  • gtruby 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.r
    b
  • create app/helpers/hello_helper.rb

14
Mapping URLs to Controllers and Actions
  • http//127.0.0.13000/hello/sayit
  • http//127.0.0.13000 - 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 gt "lth2gtHello World!lt/h2gt"
  • 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.
  • gt ruby script/generate scaffold_resource
    bookmark urlstring titlestring

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 lt ActiveRecordMigration
  • 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.
  • gtrake dbmigrate
  • 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
  • _at_bookmarks Bookmark.find(all)
  • respond_to do format
  • format.html index.rhtml
  • format.xml render xml gt
    _at_bookmarks.to_xml
  • end
  • End

25
Mapping URLs to Controllers and Actions
  • http//127.0.0.13000/bookmarks/
  • http//127.0.0.13000 - address and port of the
    webserver
  • hello - name of the controller
  • / - name of the action (blank maps to the index
    action)

26
Bookmarks Model - Dont Repeat Yourself
  • No getters/setters. Rails uses information from
    the database.
  • class Bookmark lt ActiveRecordBase
  • end

27
Bookmarks View
  • Located in views/bookmarks/index.rhtml
  • lt for bookmark in _at_bookmarks gt
  • lttrgt
  • lttdgtlth bookmark.url gtlt/tdgt
  • lttdgtlth bookmark.title gtlt/tdgt
  • lttdgtlth bookmark.description gtlt/tdgt
  • lttdgtlt link_to 'Show', bookmark_path(bookmark
    ) gtlt/tdgt
  • lttdgtlt link_to 'Edit', edit_bookmark_path(boo
    kmark) gtlt/tdgt
  • lttdgtlt link_to 'Destroy', bookmark_path(bookm
    ark), confirm gt 'Are you sure?', method gt
    delete gtlt/tdgt
  • lt/trgt
  • lt end gt

28
Convention over Configuration
  • Bookmark model automatically looks for a table in
    the database with a plural form of its name.
    (bookmarks)
  • The Bookmarks controller automatically renders
    the template located in views according to the
    controller name and the action (views/bookmarks/in
    dex.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
  • lt text_field_with_auto_complete bookmark, url
    gt
  • In views/layouts/bookmarks.rhtml, add
  • lt javascript_include_tag defaults gt

31
Validations
  • Rails has a number of validation helpers that can
    be added to the model.
  • class Bookmark lt ActiveRecordBase
  • 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 gt 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
  • gt ruby script/generate scaffold_resource
    category namestring
  • This creates the all the scaffolding and the
    migration db/migrate/002_create_categories.rb
  • Note the category table is pluralized as
    categories.
  • gtruby script/generate migration
    alter_bookmarks_add_category_id
  • This creates db/migrate/003_alter_bookmarks_add_ca
    tegory_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
  • gtrake dbmigrate

37
Types of Associations
  • has_one
  • belongs_to
  • has_many
  • has_and_belongs_to_many
  • has_many model1, through gt 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 lt ActiveRecordBase
  • validates_presence_of url, title
  • belongs_to category
  • end
  • class Category lt ActiveRecordBase
  • has_many bookmarks
  • end

40
Associations Controller code
  • def edit
  • _at_bookmark Bookmark.find(paramsid)
  • _at_categories Category.find(all)
  • end

41
Associations View Code
  • ltpgt
  • ltbgtCategorylt/bgtltbr /gt
  • lt collection_select('bookmark',
    'category_id', _at_categories, 'id', 'name') gt
  • lt/pgt

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 RailsSecond Edition
  • Rails main site http//www.rubyonrails.com
  • My email michael-irion_at_utulsa.edu
Write a Comment
User Comments (0)
About PowerShow.com