Your SlideShare is downloading. ×
0
Rapid Development with
 Ruby, JRuby and Rails

        Brian Leonard
      Software Engineer
    Sun Microsystems, Inc.
Overall Presentation Goal



      Gain a basic understanding
                    of the
               Ruby on Rails
    ...
Speaker’s Qualifications


   Brian Leonard began working with
    enterprise Java in 1997 at NetDynamics.
    Today he's...
What is Rails?




            An MVC framework
                   for
             web applications




                 ...
The Features of Rails

   Metaprogramming
   Active Record
   Convention over Configuration
   Scaffolding
   Easy Aj...
Rails Naming Conventions

   Class names are usually CamelCase
        RocketShip
   File names are lower_case
       ...
Ruby/Rails Terminology

   Rake
       Ruby Make
   Symbols
       Often used in place of a string
        :rocket, :e...
Rails File Types




     Extension         Type
     *.rb              Ruby file
     *.erb             Embedded Ruby fil...
Symbols
   Self-descriptive immediate objects
       like the number 3, for example.
   Atomic, immutable and unique
  ...
Symbols
   Often used to refer to:
     Method names (:post_comment)
     Options in a method argument list (:name, :ti...
So really, what is a symbol?
   Simply, a symbol is something that you use
    to represent names and strings.
   What t...
How do I use it?



                   12
Outline


   Creating the Project
   Schema Migrations
   Validation
   The View
   Database Relationships
   Ajax
...
Outline


   Creating the Project
   Schema Migrations
   Validation
   The View
   Database Relationships
   Ajax
...
Creating a Basic Rails Project


   Create your project
        rails #{project name}
   Create your database
      ra...
Creating a Basic Rails Project

   Scaffold your first resource
        ruby script/generate scaffold #{model
         n...
Creating a Basic Rails Project - Example

   rails blog
   rake db > create;
   ruby script/generate scaffold post
    ...
DEMO

Rails Application – Iteration 1:
 Application, Database and
      Initial Scaffolding
Outline


   Creating the Project
   Schema Migrations
   Validation
   The View
   Database Relationships
   Ajax
...
Database Migrations




                      20
The Evolving Data Model

   Our requirements have changed!
        Our database needs another field
   Migrations to th...
DEMO


Rails Application – Iteration 2:
     Database Migrations
Outline


   Creating the Project
   Schema Migrations
   Validation
   The View
   Database Relationships
   Ajax
...
Validation

   Defined in the model – the gatekeeper of
    our data
   Common methods:
      validates_presence_of
   ...
Validation
DEMO


Rails Application – Iteration 3:
          Validation
Outline


   Creating the Project
   Schema Migrations
   Validation
   The View
   Database Relationships
   Ajax
...
Understand the Rails URL


http://domain/#{controller}/#{id} and/or #{action}

                   Controller              ...
erb Templates


<p>
  <b>Title:</b>
  <%=h @post.title %>
</p>
<p>
  <b>Body:</b>
  <%=h @post.body %>
</p>
<%= link_to 'E...
Application Flow
posts_controller.rb
   def edit
     @post = Post.find( params[:id])              1
   end
        edit.h...
DEMO


Rails Application – Iteration 4:
          The View
Outline


   Creating the Project
   Schema Migrations
   Validation
   The View
   Database Relationships
   Ajax
...
Model Relationships

   Declaring relationships
      has_many
      belongs_to
Model Relationships

   For example:
    class Post < ActiveRecord::Base
      has_many :comments
    end

    class Comm...
DEMO

Rails Application – Iteration 5:
     Model Relationships
Outline


   Creating the Project
   Schema Migrations
   Validation
   The View
   Database Relationships
   Ajax
...
AJAX

   Prototype and script.aculo.us libraries are
    included with Rails
        Others can be easily added
Prepare the Application for AJAX

   Move the html you would like to be dynamic
    into a partial template.
   “Partial...
First Steps to AJAX

   Include the Prototype and script.aculo.us
    Javascript libraries
       <%= javascript_include_...
Steps to AJAX

   Use render :update construct to handle the
    XMLHTTPRequest
   render :update allows you to use Ruby...
DEMO

Rails Application – Iteration 6:
            AJAX
Outline


   Creating the Project
   Schema Migrations
   Validation
   The View
   Database Relationships
   Ajax
...
Java

   To call Java from Ruby:
     include Java

   Import classes (just as in Java)
     import java.util.ArrayList
...
DEMO

Rails Application – Iteration 7:
             Java
Outline


   Creating the Project
   Schema Migrations
   Validation
   The View
   Database Relationships
   Ajax
...
Java Server Integration

 Goldspike
 GlassFish V3




                          46
Goldspike

 Rails Plugin
 Packages Rails application as WAR

 WAR contains a servlet that translates data
  from the se...
GlassFish v3

   Next version of GlassFish
   Ideal container for Web 2.0 applications
   Small
        Kernel < 100k
...
Why JRuby on GlassFish ?

   Java EE is tested deployment platform
   Integrate existing Java EE & RoR apps in
    one c...
DEMO

Rails Application – Iteration 8:
 Deploying to GlassFish and
            Tomcat
Outline


   Creating the Project
   Schema Migrations
   The View
   Validation
   Database Relationships
   Ajax
...
Summary


   The popular Ruby language and Ruby on
    Rails framework are now available on the
    Java platform
   Rub...
Java Powered Ruby
Q&A
Thank you for your
    attention
Upcoming SlideShare
Loading in...5
×

Rapid Development with Ruby, JRuby and Rails

1,892

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,892
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
15
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Rapid Development with Ruby, JRuby and Rails"

  1. 1. Rapid Development with Ruby, JRuby and Rails Brian Leonard Software Engineer Sun Microsystems, Inc.
  2. 2. Overall Presentation Goal Gain a basic understanding of the Ruby on Rails concepts
  3. 3. Speaker’s Qualifications  Brian Leonard began working with enterprise Java in 1997 at NetDynamics. Today he's a technology evangelist at Sun Microsystems.  Brian developed the Ruby on Rails tutorial series on netbeans.org which is used at the basis for this presentation.
  4. 4. What is Rails? An MVC framework for web applications 4
  5. 5. The Features of Rails  Metaprogramming  Active Record  Convention over Configuration  Scaffolding  Easy Ajax  Rapid Feedback Loop 5
  6. 6. Rails Naming Conventions  Class names are usually CamelCase  RocketShip  File names are lower_case  rocket_ship.rb  Model names are singular  Rocket, Person  Table names are plural  rockets, people 6
  7. 7. Ruby/Rails Terminology  Rake  Ruby Make  Symbols  Often used in place of a string :rocket, :edit  Generators  Scripts that create project artifacts (i.e., models, views and controllers)  The Flash  A way to pass objects between requests  A temporary scratchpad  Commonly used for IDs flash[:post_id] = @params[:id] 7
  8. 8. Rails File Types Extension Type *.rb Ruby file *.erb Embedded Ruby file *.yml * Configuration * YAML Ain't Markup Language 8
  9. 9. Symbols  Self-descriptive immediate objects  like the number 3, for example.  Atomic, immutable and unique  Can't be parsed or modified  All references to a symbol refer to the same object  :foo.equals?(:foo) #True  'foo'.equals?('foo') #False  Always interchangeable with strings  No right or wrong usage  Easier to type  Stand out in the editor  The different syntax can distinguish keys from values  :name => 'Brian' 9
  10. 10. Symbols  Often used to refer to:  Method names (:post_comment)  Options in a method argument list (:name, :title)  Hash keys (:name => 'Brian')  Symbols are not:  Variables  They don't hold references to other objects  Strings  Although you can get their string representation 10
  11. 11. So really, what is a symbol?  Simply, a symbol is something that you use to represent names and strings.  What this boils down to is a way to efficiently have descriptive names while saving the space one would use to generate a string for each naming instance.  Useful whenever you’re going to be reusing a word over and over to represent something else http://glu.ttono.us/articles/2005/08/19/understanding-ruby-symbols 11
  12. 12. How do I use it? 12
  13. 13. Outline  Creating the Project  Schema Migrations  Validation  The View  Database Relationships  Ajax  Java  Deployment
  14. 14. Outline  Creating the Project  Schema Migrations  Validation  The View  Database Relationships  Ajax  Java  Deployment
  15. 15. Creating a Basic Rails Project  Create your project  rails #{project name}  Create your database  rake > db > create > all  Will create the database(s) as defined in database.yml:  #{project name}_development  #{project name}_test  #{project name}_production
  16. 16. Creating a Basic Rails Project  Scaffold your first resource  ruby script/generate scaffold #{model name} #{attribute pairs}  Follow conventions (singular form)  Execute the database migrations  rake db:migrate
  17. 17. Creating a Basic Rails Project - Example  rails blog  rake db > create;  ruby script/generate scaffold post title:string  rake db:migrate  ruby script/server  Test:  http://localhost:3000/posts
  18. 18. DEMO Rails Application – Iteration 1: Application, Database and Initial Scaffolding
  19. 19. Outline  Creating the Project  Schema Migrations  Validation  The View  Database Relationships  Ajax  Java  Deployment
  20. 20. Database Migrations 20
  21. 21. The Evolving Data Model  Our requirements have changed!  Our database needs another field  Migrations to the rescue  ruby script/generate migration #{migration name} #{attribute pairs}  For example:  ruby script/generate migration AddBodyToPosts body:text class AddBody < ActiveRecord::Migration def self.up add_column :post, :body, :text end end
  22. 22. DEMO Rails Application – Iteration 2: Database Migrations
  23. 23. Outline  Creating the Project  Schema Migrations  Validation  The View  Database Relationships  Ajax  Java  Deployment
  24. 24. Validation  Defined in the model – the gatekeeper of our data  Common methods:  validates_presence_of  validates_numericality_of  validates_uniqueness_of  validates_format_of  validates_length_of  Code Templates exist for most of these
  25. 25. Validation
  26. 26. DEMO Rails Application – Iteration 3: Validation
  27. 27. Outline  Creating the Project  Schema Migrations  Validation  The View  Database Relationships  Ajax  Java  Deployment
  28. 28. Understand the Rails URL http://domain/#{controller}/#{id} and/or #{action} Controller ID
  29. 29. erb Templates <p> <b>Title:</b> <%=h @post.title %> </p> <p> <b>Body:</b> <%=h @post.body %> </p> <%= link_to 'Edit', edit_post_path(@post) %> | <%= link_to 'Back', posts_path %>
  30. 30. Application Flow posts_controller.rb def edit @post = Post.find( params[:id]) 1 end edit.html.erb <% form_for(@post) do |f| %> <p><b>Title</b><br /> <%= f.text_field :title %></p> <p><b>Body </b><br /> <%= f.text_area :body %></p> <p><%= submit “Update” %></p> <% end %> posts_controller.rb def update 2 @post = Post.find(params[:id]) if @post.update_attributes(params[:post]) redirect_to(@post) else render :action => “edit” end end
  31. 31. DEMO Rails Application – Iteration 4: The View
  32. 32. Outline  Creating the Project  Schema Migrations  Validation  The View  Database Relationships  Ajax  Java  Deployment
  33. 33. Model Relationships  Declaring relationships  has_many  belongs_to
  34. 34. Model Relationships  For example: class Post < ActiveRecord::Base has_many :comments end class Comment < ActiveRecord::Base belongs_to :post end  Allows for: # Fetch all comments for post id 1 post = Post.find(1) comments = post.comments.collect
  35. 35. DEMO Rails Application – Iteration 5: Model Relationships
  36. 36. Outline  Creating the Project  Schema Migrations  Validation  The View  Database Relationships  Ajax  Java  Deployment
  37. 37. AJAX  Prototype and script.aculo.us libraries are included with Rails  Others can be easily added
  38. 38. Prepare the Application for AJAX  Move the html you would like to be dynamic into a partial template.  “Partials”  Chunks of rhtml that act as methods  Prepended with an underscore.  For example: _comment.rhtml  Usage: <%= render :partial => "comment", :object => comment %>  Test!
  39. 39. First Steps to AJAX  Include the Prototype and script.aculo.us Javascript libraries <%= javascript_include_tag :defaults %>  Replace form_tag (does an HTTP POST) with form_remote_tag (does an XMLHTTPRequest)  Test!
  40. 40. Steps to AJAX  Use render :update construct to handle the XMLHTTPRequest  render :update allows you to use Ruby commands to generate JavaScript render :update do |page| page.insert_html :bottom, "comments", :partial => “comment” end  Create a DOM id to reference <div id=”comments”> ... </div>  Test!
  41. 41. DEMO Rails Application – Iteration 6: AJAX
  42. 42. Outline  Creating the Project  Schema Migrations  Validation  The View  Database Relationships  Ajax  Java  Deployment
  43. 43. Java  To call Java from Ruby: include Java  Import classes (just as in Java) import java.util.ArrayList  Use the class:  Note the Ruby syntax! dogs = ArrayList.new dogs.add "Spaniel" dogs.add "Hound" dogs.add "Retriever" dogs.each do |dog| puts dog end
  44. 44. DEMO Rails Application – Iteration 7: Java
  45. 45. Outline  Creating the Project  Schema Migrations  Validation  The View  Database Relationships  Ajax  Java  Deployment
  46. 46. Java Server Integration  Goldspike  GlassFish V3 46
  47. 47. Goldspike  Rails Plugin  Packages Rails application as WAR  WAR contains a servlet that translates data from the servlet request to the Rails dispatcher  Works for any servlet container  rake war:standalone:create 47
  48. 48. GlassFish v3  Next version of GlassFish  Ideal container for Web 2.0 applications  Small  Kernel < 100k  Fast  Starts up in < 1 second  Modular  Java, Ruby, PHP, JavaScript, ...  Will be Java EE 6 compatible 48
  49. 49. Why JRuby on GlassFish ?  Java EE is tested deployment platform  Integrate existing Java EE & RoR apps in one container  Hot Deployment  No need to restart container  Database Connection Pooling  One instance, one process  OOTB Clustering and High Availability 49
  50. 50. DEMO Rails Application – Iteration 8: Deploying to GlassFish and Tomcat
  51. 51. Outline  Creating the Project  Schema Migrations  The View  Validation  Database Relationships  Ajax  Java  Deployment
  52. 52. Summary  The popular Ruby language and Ruby on Rails framework are now available on the Java platform  Ruby's dynamic nature make it a fun and ideal language for web development (Ruby on Rails)  Rails applications can be deployed to your favorite Java server container
  53. 53. Java Powered Ruby
  54. 54. Q&A
  55. 55. Thank you for your attention
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×