td mxc rubyrails shin

Upload: armandochagoya

Post on 30-May-2018

221 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/14/2019 TD MXC RubyRails Shin

    1/49

    1

    Rapid WebRapid Web

    ApplicationApplicationDevelopment usingDevelopment usingRuby on RailsRuby on Rails

    Sang ShinSang ShinSun Microsystems, Inc.Sun Microsystems, Inc.javapassion.comjavapassion.com

    1

  • 8/14/2019 TD MXC RubyRails Shin

    2/49

  • 8/14/2019 TD MXC RubyRails Shin

    3/49

    3

    Topics

    What is and Why Ruby on Rails (Rails)? Rails Basics

    Step by step for building Hello World Rails application

    ActiveRecord ActionController

    ActionView

    Deployment

  • 8/14/2019 TD MXC RubyRails Shin

    4/49

    What is and WhyWhat is and WhyRuby on Rails (RoR)?Ruby on Rails (RoR)?

  • 8/14/2019 TD MXC RubyRails Shin

    5/49

    5

    What Is Ruby on Rails?

    A full-stack MVC web development framework

    Written in Ruby

    First released in 2004 byDavid Heinemeier Hansson

    Gaining popularity

  • 8/14/2019 TD MXC RubyRails Shin

    6/49

    6

    Ruby on Rails Principles

    Convention over configuration> Why punish the common cases?

    > Encourages standard practices

    > Everything simpler and smaller Dont Repeat Yourself (DRY)

    > Repetitive code is harmful to adaptability

    Agile development environment> No recompile, deploy, restart cycles

    > Simple tools to generate code quickly

    > Testing built into the framework

  • 8/14/2019 TD MXC RubyRails Shin

    7/49

    Rails BasicsRails Basics

  • 8/14/2019 TD MXC RubyRails Shin

    8/498

    Ruby on Rails MVC

    source: http://www.ilug-cal.org

  • 8/14/2019 TD MXC RubyRails Shin

    9/49

    Step By Step ProcessStep By Step Processof Building Hello Worldof Building Hello World

    Rails ApplicationRails Application

  • 8/14/2019 TD MXC RubyRails Shin

    10/4910

    Steps to Follow

    1.Create Ruby on Rails project

    > IDE generate necessary directories and files

    2.Create and populate database tables3.Create Models (through Rails Generator)

    > Migrate database

    4.Create Controllers (through Rails Generator)5.Create Views

    6.Set URL Routing

    > Map URL to controller and action

  • 8/14/2019 TD MXC RubyRails Shin

    11/4911

    Demo:Demo:Building Hello WorldBuilding Hello World

    Rails Application Step by Step.Rails Application Step by Step.

    http://www.javapassion.com/handsonlabs/rails_basics/#Exercise_1http://www.javapassion.com/handsonlabs/rails_basics/#Exercise_1

  • 8/14/2019 TD MXC RubyRails Shin

    12/4912

    Key Learning Points

    How to create a Rails project

    > Rails application directory structure

    > Concept of environments - development, test, and

    production

    How to create a database using Rake

    How to create and populate tables using Migration

    How to create a model using Generator

    How to use Rails console

  • 8/14/2019 TD MXC RubyRails Shin

    13/4913

    Key Learning Points

    How to create a controller using Generator

    > How to add actions to a controller

    How to create a related view

    > How a controller and a view are related

    > How to create instance variables in an action and theyare used in a view

    How to set up a routing

    How to trouble-shoot a problem

  • 8/14/2019 TD MXC RubyRails Shin

    14/4914

    Demo:Demo:How to create an input form.How to create an input form.

    http://www.javapassion.com/handsonlabs/rails_basics/#Exercise_4http://www.javapassion.com/handsonlabs/rails_basics/#Exercise_4

  • 8/14/2019 TD MXC RubyRails Shin

    15/4915

    Key Learning Points

    How to use form_tag and text_fieldhelpers tocreate an input form

    How input form fields are accessed in an action

    method throughparams

  • 8/14/2019 TD MXC RubyRails Shin

    16/49

    ScaffoldingScaffolding

  • 8/14/2019 TD MXC RubyRails Shin

    17/4917

    What is Scaffolding?

    Scaffolding is a way to quickly create a CRUDapplication

    > Rails framework generates a set of actions for listing,

    showing, creating, updating, and destroying objects ofthe class

    > These standardized actions come with both controllerlogic and default templates that through introspectionalready know which fields to display and which inputtypes to use

    Supports RESTful view of the a Model

  • 8/14/2019 TD MXC RubyRails Shin

    18/4918

    Demo:Demo:

    Creating a Rails ApplicationCreating a Rails Applicationusing Scaffoldingusing Scaffolding

    http://www.javapassion.com/handsonlabs/rails_scaffold/#Exercise_1http://www.javapassion.com/handsonlabs/rails_scaffold/#Exercise_1

  • 8/14/2019 TD MXC RubyRails Shin

    19/4919

    Key Learning Points

    How to perform scaffolding using Generator

    What action methods are created throughscaffolding

    What templates are created through scaffolding

  • 8/14/2019 TD MXC RubyRails Shin

    20/49

    ActiveRecordActiveRecordBasicsBasics

  • 8/14/2019 TD MXC RubyRails Shin

    21/49

    21

    ActiveRecord Basics

    Model (from MVC)

    Object Relation Mapping library

    > A table maps to a Ruby class (Model)

    > A row maps to a Ruby object

    > Columns map to attributes

    Database agnostic

    Your model class extendsActiveRecord::Base

  • 8/14/2019 TD MXC RubyRails Shin

    22/49

    22

    ActiveRecord Class

    Your model class extendsActiveRecord::Base

    class User [ "user_name = ? ANDpassword = ?", user_name, password ])

    end

    end

  • 8/14/2019 TD MXC RubyRails Shin

    23/49

    23

    Naming Conventions

    Table names are plural and class names aresingular

    > posts (table), Post(class)

    > students (table), Student(class)

    > people (table), Person (class)

    Tables contain a column named id

  • 8/14/2019 TD MXC RubyRails Shin

    24/49

    24

    Find: Examples

    find by id

    Person.find(1) # returns the object for ID = 1

    Person.find(1, 2, 6) # returns an array for objects with IDs in (1, 2, 6)

    find firstPerson.find(:first) # returns the first object fetched by SELECT * FROM peo

    Person.find(:first, :conditions => [ "user_name = ?", user_name])

    Person.find(:first, :order => "created_on DESC", :offset => 5)

  • 8/14/2019 TD MXC RubyRails Shin

    25/49

    25

    Dynamic attribute-based finders

    Dynamic attribute-based finders are a cleaner wayof getting (and/or creating) objects by simplequeries without turning to SQL

    They work by appending the name of an attribute tofind_by_orfind_all_by_, so you get finders like

    > Person.find_by_user_name(user_name)

    > Person.find(:first, :conditions => ["user_name = ?",user_name])

    > Person.find_all_by_last_name(last_name)> Person.find(:all, :conditions => ["last_name = ?", last_name])

    >Payment.find_by_transaction_id

  • 8/14/2019 TD MXC RubyRails Shin

    26/49

    ActiveRecordActiveRecordMigrationMigration

  • 8/14/2019 TD MXC RubyRails Shin

    27/49

    27

    ActiveRecord Migration

    Provides version control of database schema

    > Adding a new field to a table

    > Removing a field from an existing table

    > Changing the name of the column

    > Creating a new table

    Each change in schema is represented in pure

    Ruby code

  • 8/14/2019 TD MXC RubyRails Shin

    28/49

    28

    Example: Migration

    Add a boolean flag to the accounts table and remove itagain, if youre backing out of the migration.

    class AddSsl < ActiveRecord::Migration def self.up

    add_column :accounts, :ssl_enabled, :boolean, :default => 1end

    def self.downremove_column :accounts, :ssl_enabled

    end

    end

  • 8/14/2019 TD MXC RubyRails Shin

    29/49

    29

    Demo:Demo:How to add a field to a tableHow to add a field to a table

    using Migrationusing Migration

    http://www.javapassion.com/handsonlabs/rails_scaffold/#Exercise_2http://www.javapassion.com/handsonlabs/rails_scaffold/#Exercise_2

  • 8/14/2019 TD MXC RubyRails Shin

    30/49

    30

    Key Learning Points

    How to add a new field to a table using Migration

    How to create a migration file using Generator

    How to see a log file

  • 8/14/2019 TD MXC RubyRails Shin

    31/49

    ActiveRecordActiveRecordValidationValidation

  • 8/14/2019 TD MXC RubyRails Shin

    32/49

    32

    ActiveRecord::Validations

    Validation methods

    class User < ActiveRecord::Base

    validates_presence_of :username, :level

    validates_uniqueness_of :username

    validates_oak_id :username

    validates_length_of :username, :maximum => 3, :allow_nil

    validates_numericality_of :value, :on => :create

    end

  • 8/14/2019 TD MXC RubyRails Shin

    33/49

    33

    Validation

  • 8/14/2019 TD MXC RubyRails Shin

    34/49

    34

    Demo:Demo:ValidationValidation

    http://www.javapassion.com/handsonlabs/rails_scaffold/#2.4http://www.javapassion.com/handsonlabs/rails_scaffold/#2.4

  • 8/14/2019 TD MXC RubyRails Shin

    35/49

  • 8/14/2019 TD MXC RubyRails Shin

    36/49

    36

    Associations

    Associations are a set of macro-like class methodsfor tying objects together through foreign keys.

    They express relationships like "Project has one

    Project Manager" or "Project belongs to a Portfolio".

    Each macro adds a number of methods to the classwhich are specialized according to the collection or

    association symbol and the options hash. Cardinality

    > One-to-one, One-to-many, Many-to-many

  • 8/14/2019 TD MXC RubyRails Shin

    37/49

    37

    One-to-many

    Use has_manyin the base, and belongs_to in theassociated model

    class Manager < ActiveRecord::Base

    has_many:employees

    end

    class Employee < ActiveRecord::Base belongs_to :manager # foreign key - manager_id

    end

  • 8/14/2019 TD MXC RubyRails Shin

    38/49

    38

    Demo:Demo:

    AssociationAssociation

    http://www.javapassion.com/handsonlabs/rails_activerecord/http://www.javapassion.com/handsonlabs/rails_activerecord/

  • 8/14/2019 TD MXC RubyRails Shin

    39/49

    ActionControllerActionControllerBasicsBasics

  • 8/14/2019 TD MXC RubyRails Shin

    40/49

    40

    ActionController

    Controller is made up of one or more actions that areexecuted on request and then either render a template orredirect to another action

    An action is defined as a public method on the controller,which will automatically be made accessible to the web-server through Rails Routes

    Actions, by default, render a template in the app/views

    directory corresponding to the name of the controller andaction after executing code in the action.

  • 8/14/2019 TD MXC RubyRails Shin

    41/49

    41

    ActionController

    For example, the indexaction of the GuestBookControllerwouldrender the template app/views/guestbook/index.erb by defaultafter populating the @entries instance variable.

    class GuestBookController < ActionController::Base

    def index@entries = Entry.find(:all)

    end

    def signEntry.create(params[:entry])redirect_to :action => "index"

    end

    end

  • 8/14/2019 TD MXC RubyRails Shin

    42/49

    DeploymentDeployment

  • 8/14/2019 TD MXC RubyRails Shin

    43/49

    43

    Web Servers

    By default, Rails will try to use Mongrel and lighttpdif they are installed, otherwise Rails will useWEBrick, the webserver that ships with Ruby.

    Java Server integration> Goldspike

    > GlassFish V3

  • 8/14/2019 TD MXC RubyRails Shin

    44/49

    44

    Goldspike

    Rails Plugin

    Packages Rails application as WAR

    WAR contains a servlet that translates data from theservlet request to the Rails dispatcher

    Works for any servlet container

    rake war:standalone:create

  • 8/14/2019 TD MXC RubyRails Shin

    45/49

    45

    Demo:Demo:Deployment throughDeployment through

    GoldspikeGoldspike

    http://www.javapassion.com/handsonlabs/rails_deploy/#Exercise_1http://www.javapassion.com/handsonlabs/rails_deploy/#Exercise_1

  • 8/14/2019 TD MXC RubyRails Shin

    46/49

    JRuby on RailsJRuby on Rails

  • 8/14/2019 TD MXC RubyRails Shin

    47/49

    47

    Why JRuby on Rails

    over Ruby on Rails? Java technology production environments pervasive

    > Easier to switch framework vs. whole architecture

    > Lower barrier to entry

    Integration with Java technology libraries,legacy services

    No need to leave Java technology servers, libraries,reliability

    Deployment to Java application servers

  • 8/14/2019 TD MXC RubyRails Shin

    48/49

    48

    JRuby on Rails: Java EE Platform

    Pool database connections

    Access any Java Naming and Directory Interface

    (J.N.D.I.) API resource

    Access any Java EE platform TLA:

    > Java Persistence API (JPA)

    > Java Management Extensions (JMX)

    > Enterprise JavaBeans (EJB)

    > Java Message Service (JMS) API

    > SOAP/WSDL/SOA

  • 8/14/2019 TD MXC RubyRails Shin

    49/49

    Rapid WebRapid Web

    ApplicationApplicationDevelopment usingDevelopment usingRuby on RailsRuby on Rails

    Sang ShinSang ShinSun Microsystems, Inc.Sun Microsystems, Inc.javapassion.comjavapassion.com