Ruby on Rails For Java Programmers

  • View
    667

  • Download
    2

Embed Size (px)

DESCRIPTION

 

Text of Ruby on Rails For Java Programmers

  • Ruby on Rails For Java Programmers

    Rob Sanheim

    www.robsanheim.comwww.seekingalpha.com

    www.ajaxian.com

    http://www.robsanheim.comhttp://www.robsanheim.comhttp://www.seekingalpha.comhttp://www.seekingalpha.comhttp://www.ajaxian.comhttp://www.ajaxian.com
  • (Briefly) About Me

  • Why Rails?

  • Because its fun.

  • Because it will make you a better

    programmer.

  • Learn at least one new [programming] language every year. Different languages solve the same problems in different ways. By learning several

    different approaches, you can help broaden your thinking and avoid getting stuck in a rut. - David Thomas and Andy Hunt

  • Because you might just be able to get

    paid for it.

  • Fully Object OrientedInterpretedSimple, Yet PowerfulOpen SourceBorn in Japan in 1995Yukihiro Matsumoto (Matz)

    What is Ruby?

  • Power

    Closures (aka Blocks)Modules ReflectionOpen Classes

  • Typing

    StrongDynamicDuck

  • Lets see some code

  • HelloWorld

  • Reading a file

  • Reading a file - Ruby

  • Collections

  • Reflection

  • Ruby on Rails

    David Heinemeier Hansson (DHH) Began via Basecamp in mid 2003 Released July 2004 Full Stack MVC Web Framework Easy database-based web apps

  • Accolades

    2006 Jolt award - Web Dev Tool Featured in BusinessWeek, Wired DHH Best Hacker award from Google

  • Converts

    Dave ThomasBruce TateStuart Halloway Justin GehtlandDavid Geary James Duncan Davidson

  • Rails is the most well thought-out web development framework Ive ever used. And thats in a decade of doing web applications for a living. Ive built my own frameworks, helped develop

    the Servlet API, and have created more than a few web servers from scratch. Nobody has done it like this before.

    -James Duncan Davidson, Creator of Tomcat and Ant

  • Platform

    MySQL, PostgreSQL, SQLite, Oracle, SQL Server, DB2, or Firebird

    develop anywhereApache or LightTPD + FastCGI for

    production

  • DRY - Dont Repeat Yourself

    * Done via intelligent reflection and metaprogramming* Emphasis on writing code to get stuff done - avoid boilerplate* Scaffolding handles left over boilerplate

  • Convention over Configuration

    NO XML!Intelligent DefaultsOnly explicitly configure when necessary

  • ActiveRecordAn object that wraps a row in a database table or view, encapsulates the database

    access, and adds domain logic on that data.

  • Controller and View

    ActionPack maps URLs to controller View handled via Ruby in HTML (RHTML) or builders Ajax provided via Prototype and RJS

  • from: http://www.slash7.com/articles/2005/02/22/mvc-the-most-vexing-conundrum

    http://www.slash7.com/articles/2005/02/22/mvc-the-most-vexing-conundrumhttp://www.slash7.com/articles/2005/02/22/mvc-the-most-vexing-conundrum
  • Lets see some code

  • create table orders ( id int not null auto_increment, name varchar(100) not null, email varchar(255) not null, address text not null, pay_type char(10) not null, shipped_at datetime null, primary key (id));

  • class Order < ActiveRecord::Base end

  • order = Order.neworder.name = Roborder.email = rob@yahoo.comorder.address = 123 1st Storder.save

    mailto:rsanheim@gmail.commailto:rsanheim@gmail.com
  • an_order = Order.find(24)

    robs_order = Order.find(:first, :conditions => "name = 'Rob'")

    rob_count = Order.count(:all, :conditions => name = Rob)

    robs_order = Order.find_by_name(Rob)

    more_orders = Order.find_all_by_name_and_pay_type(name, type)

    Order.delete(24)

    robs_order.destroy

  • create table order_lines ( id int not null auto_increment, description varchar(100) not null, order_id int not null, primary key (id), constraint fk_order foreign key (order_id) references orders(id));

  • class Order < ActiveRecord::Base has_many :order_lines end

    class OrderLine < ActiveRecord::Base belongs_to :orderend

    one to many between orders and order lines

    has_many is actually a class level method call that generates instance methods

  • my_order = Order.find(id)has_order_lines = my_order.order_lines?order_lines_count = my_order.order_lines.size

    my_order.order_lines.push(new_order, another_new_order)

    specific_order_line = my_order.find(....)specific_order_lnes = my_order.find_by_description(abc)

    my_line = OrderLine.find(15)has_owning_order = my_line.order.nil?

    old_order = my_line.ordermy_line.order= another_order

  • Validation

  • 00

    0

    0000

    16,400

    04/17/2006 4868Return Prepared:Prepared On:

    personalized tax filing advice for:

    to file your 2005 federal extension, simply follow these instructions

    quick summary

    filing checklist for your 2005 federal application for automatic extension

    $$

    Gross Income

    Total Taxable Income

    Adjusted Gross Income$$

    $

    $$

    For more information about tax, mortgage and financial services call 1-800-HRBLOCK or visit hrblock.com

    to file your return.

    Total Deductions

    $

    This checklist has been customized for you based on the method you're using to file your 2005

    Total PaymentsTotal Tax

    Refund Amount

    tax return. The statements below are designed to help guide you through the steps you will take

    Amount You Owe

    STEP B - Since you indicated that you are not paying an amount withForm 4868, there is nothing to mail.

    number on your printed copy of Form 4868 and keep for your records.

    STEP A - Once your e-filed extension has been accepted, you willreceive an email with a confirmation number. Enter the confirmation

    * confirmation checks two password fields against each other

    * acceptance is pattern of terms of service/agreement checkbox

  • 00

    0

    0000

    16,400

    04/17/2006 4868Return Prepared:Prepared On:

    personalized tax filing advice for:

    to file your 2005 federal extension, simply follow these instructions

    quick summary

    filing checklist for your 2005 federal application for automatic extension

    $$

    Gross Income

    Total Taxable Income

    Adjusted Gross Income$$

    $

    $$

    For more information about tax, mortgage and financial services call 1-800-HRBLOCK or visit hrblock.com

    to file your return.

    Total Deductions

    $

    This checklist has been customized for you based on the method you're using to file your 2005

    Total PaymentsTotal Tax

    Refund Amount

    tax return. The statements below are designed to help guide you through the steps you will take

    Amount You Owe

    STEP B - Since you indicated that you are not paying an amount withForm 4868, there is nothing to mail.

    number on your printed copy of Form 4868 and keep for your records.

    STEP A - Once your e-filed extension has been accepted, you willreceive an email with a confirmation number. Enter the confirmation

  • Much More...

    many to many via has_many :through eager loading acts_as_list, acts_as_tree single table inheritance callbacks and observers

  • ActionController

  • http://url.com/weblog/aka

    http://url.com/weblog/index.rhtml

    http://myurl.com/store/http://myurl.com/store/
  • /app/controllers/weblog_controller.rb:class WeblogController < ActionController::Base layout "weblog/layout"

    def index @posts = Post.find_all end

    def display @post = Post.find(:params[:id]) end

    def new @post = Post.new end

    def create @post = Post.create(params[:post]) redirect_to :action => "display", :id => @post.id end end

    ||= conditional assignment operator

    cart is a non persistent model object used for storing/calculating products

    the flash = place to throw stuff like response messages, error reports that accumulate as a request is processed

  • /app/views/weblog/layout.rhtml:

    /app/views/weblog/index.rhtml:

    "display", :id => post.id %>

    /app/views/weblog/display.rhtml:


    /app/views/weblog/new.rhtml:

  • http://url.com/weblog/display/5http://url.com/weblog/display/123

    http://url.com/weblog/new/

    http://url.com/weblog/display/5http://url.com/weblog/display/5http://url.com/weblog/http://url.com/weblog/http://url.com/weblog/newhttp://url.com/weblog/new
  • rxml - XML Builders rhtml - HTML templates helpers - module to assist the view form and field helpers layouts and partials - similiar to tiles page caching and fragment caching

    ActionPack features

  • FORM HELPERS 342

    !"#$#%&'(')

    ''''*+,'(-'./012

    ''''*3&4$'(-')'

    ''''''''*5#%4'(-'6'777'62'

    ''''''''*8935:$;'(-'6'777'62

    ''''''''*"#&&?9$%'#8:+95(6@%;#""@A4@./016-

    ''''>+5"3:'5#%4(63&4$B5#%4C6'777'-

    ''''>+5"3:'5#%4(63&4$B8935:$;C6'777'-

    ''''>+5"3:'5#%4(63&4$B"#&&@?9$%-

    >D('?9$%E:#F'*#8:+95'(-'GA4G2'*+,'(-'!3&4$'D-

    >D(':4H:E!4I,'''''''''''G3&4$G2'G5#%4G''D->@"-

    >D(':4H:E!4I,'''''''''''G3&4$G2'G8935:$;G''D->@"-

    >D('"#&&D('45,E?9$%E:#F'D-

    !"#'A4

    ''3&4$'('J&4$7!5,K"#$#%&B*+,CL

    ''$#'3&4$73",#:4E#::$+M3:4&K"#$#%&B*3&4$C