6 (J)Ruby on Rails-Sang Shin
Transcript of 6 (J)Ruby on Rails-Sang Shin
-
8/14/2019 6 (J)Ruby on Rails-Sang Shin
1/72
1
(J)Ruby on Rails(J)Ruby on Rails
Sang Shin, Technology ArchitectSang Shin, Technology Architect
Sun Microsystems, Inc.Sun Microsystems, Inc.
-
8/14/2019 6 (J)Ruby on Rails-Sang Shin
2/72
2
Topics
What is and Why Ruby on Rails?
Building HelloWorld Rails application step bystep> App directory structure (MVC), Environment, Rake,
Generator, Migration, Rails console, etc Scaffolding
JRuby
Testing Ajax
REST support
Deployment
-
8/14/2019 6 (J)Ruby on Rails-Sang Shin
3/72
3
What is and WhyWhat is and Why
Ruby on Rails (RoR)?Ruby on Rails (RoR)?
-
8/14/2019 6 (J)Ruby on Rails-Sang Shin
4/72
4
What Is Ruby on Rails?
A full-stack MVC web development framework
Written in Ruby> Rails is the killer application that leverages various
characteristics of Ruby language - meta-programming,
closure, etc.
First released in 2004 by David HeinemeierHansson
Gaining popularity
-
8/14/2019 6 (J)Ruby on Rails-Sang Shin
5/72
5
Ruby on Rails MVC
source: http://www.ilug-cal.org
-
8/14/2019 6 (J)Ruby on Rails-Sang Shin
6/72
6
Ruby on Rails Principles
Convention over configuration> Why punish the common cases?
> Encourages standard practices
> Everything simpler and smaller
Dont Repeat Yourself (DRY)> Framework written around minimizing repetition
> Repetitive code 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 6 (J)Ruby on Rails-Sang Shin
7/727
Step By Step ProcessStep By Step Process
of Building Hello Worldof Building Hello WorldRails ApplicationRails Application
www.javapassion.com/handsonlabs/rawww.javapassion.com/handsonlabs/rails_basics/#Exercise_1ils_basics/#Exercise_1
-
8/14/2019 6 (J)Ruby on Rails-Sang Shin
8/72
-
8/14/2019 6 (J)Ruby on Rails-Sang Shin
9/729
1. Create Ruby on Rails1. Create Ruby on Rails
ProjectProject
-
8/14/2019 6 (J)Ruby on Rails-Sang Shin
10/7210
1. Create Ruby on Rails Project
Directory structure and boilerplate files of theapplication are created
-
8/14/2019 6 (J)Ruby on Rails-Sang Shin
11/72
11
Directory Structure of a Rails
Application When you ask NetBeans to create a Rails
project - internally NetBeans uses the rails'helper script -, it creates the entire directory
structure for your application.> The boiler plate files are also created
> The names of the directories and files are the samefor all Rails projects
Rails knows where to find things it needs withinthis structure, so you don't have to tell itexplicitly.
-
8/14/2019 6 (J)Ruby on Rails-Sang Shin
12/72
12
Directory Structure of a Rails Application
app: Holds all the code that's specific to thisparticular application.> app/controllers: Holds controllers that should be
named like hello_controller.rb for automated URL
mapping. All controllers should descend fromApplicationControllerwhich itself descends fromActionController::Base.
> app/models: Holds models that should be named likemessage.rb. Most models will descend fromActiveRecord::Base.
> app/views: Holds the template files for the view thatshould be named like hello/say_hello.rhtmlfor theHelloController#say_hello action.
-
8/14/2019 6 (J)Ruby on Rails-Sang Shin
13/72
13
Directory Structure of a Rails Application
app> app/views/layouts: Holds the template files for layouts
to be used with views. This models the commonheader/footer method of wrapping views. In yourviews, define a layout using the layout :defaultand create a file named default.rhtml. Insidedefault.rhtml, call to render the viewusing this layout.
> app/helpers: Holds view helpers that should be namedlike hello_helper.rb. These are generated for you
automatically when using script/generate (Generator)for controllers. Helpers can be used to wrapfunctionality for your views into methods.
-
8/14/2019 6 (J)Ruby on Rails-Sang Shin
14/72
14
Directory Structure of a Rails Application
config: Holds configuration files for the Railsenvironment, the routing map, the database, andother dependencies.> config/environments
> config/initializers> boot.rb
> database.yml
> environment.rb
> routes.rb
-
8/14/2019 6 (J)Ruby on Rails-Sang Shin
15/72
15
Demo:Demo:
Building Hello WorldBuilding Hello WorldRails Application Step by Step.Rails Application Step by Step.
-
8/14/2019 6 (J)Ruby on Rails-Sang Shin
16/7216
Learning Point:Learning Point:
EnvironmentsEnvironments
-
8/14/2019 6 (J)Ruby on Rails-Sang Shin
17/72
17
What is an Environment?
Rails provides the concept of environments -development, test, production
As a default, different databases are used fordifferent environments.> You can set each environment with its own database
connection settings.
It is easy to add custom environments> For example, staging server environment
-
8/14/2019 6 (J)Ruby on Rails-Sang Shin
18/72
18
config/database.yml
development:adapter: mysqlencoding: utf8database: helloname_developmenusername: root
password:host: localhost
test:adapter: mysql
encoding: utf8database: helloname_testusername: rootpassword:host: localhost
-
8/14/2019 6 (J)Ruby on Rails-Sang Shin
19/7219
2. Create Database2. Create Database
using Rakeusing Rake
-
8/14/2019 6 (J)Ruby on Rails-Sang Shin
20/72
20
Creating Database
Creating and dropping of databases are doneusing Rake
-
8/14/2019 6 (J)Ruby on Rails-Sang Shin
21/72
21
Creating Database
After create rake task is performed,_developmentdatabase, forexample, helloworld_developmentis created
-
8/14/2019 6 (J)Ruby on Rails-Sang Shin
22/72
22
Learning Point:Learning Point:
What is Rake?What is Rake?
-
8/14/2019 6 (J)Ruby on Rails-Sang Shin
23/72
23
What is Rake?
Rake is a build language for Ruby.
Rails uses Rake to automate several tasks suchas creating and dropping databases, runningtests, and updating Rails support files.
Rake lets you define a dependency tree of tasksto be executed
-
8/14/2019 6 (J)Ruby on Rails-Sang Shin
24/72
24
How does Rake Work?
Rake tasks are loaded from the file Rakefile
Rails rake tasks are under/lib/tasks
You can put your custom tasks underlib/tasks
-
8/14/2019 6 (J)Ruby on Rails-Sang Shin
25/72
25
3. Create a Model3. Create a Model
through Generatorthrough Generator
-
8/14/2019 6 (J)Ruby on Rails-Sang Shin
26/72
26
What is a Model?
In the context of MVC pattern, a Modelrepresents domain objects such as message,school, product, etc.
A model has attributes and methods.> The attributes represents the characteristics of the
domain object, for example, a message model mighthave length, creator as attributes.
> The methods in a model contains some businesslogic.
Most models have corresponding databasetables. For example, a message model will havemessages table.
Most model classes are ActiveRecord type
-
8/14/2019 6 (J)Ruby on Rails-Sang Shin
27/72
27
Creating a Model using Generator
-
8/14/2019 6 (J)Ruby on Rails-Sang Shin
28/72
28
Creating a Model using Generator
-
8/14/2019 6 (J)Ruby on Rails-Sang Shin
29/72
29
Files That Are Created
app/models/message.rb (Model file)> Models/messages.rb in logical view> A file that holds the methods for the Message model.
test/unit/message_test.rb
> Unit Tests/message_test.rb in logical view> A unit test for checking the Message model. test/fixtures/messages.yml
> Test Fixtures/messages.yml in logical view> A test fixture for populating the model.
db/migrate/migrate/001_create_messages.rb> Database Migrations/migrate/001_create_messages.rb in
logical view> A migration file for defining the initial structure of the
database.
-
8/14/2019 6 (J)Ruby on Rails-Sang Shin
30/72
30
Model Class Example
Message mode in messages.rb fileclass Message < ActiveRecord::Base
end
-
8/14/2019 6 (J)Ruby on Rails-Sang Shin
31/72
31
Learning Point:Learning Point:
What is Generator?What is Generator?
-
8/14/2019 6 (J)Ruby on Rails-Sang Shin
32/72
32
What is Generator?
You can often avoid writing boilerplate code byusing the built-in generator scripts of Rails tocreate it for you.> This leaves you with more time to concentrate on the
code that really matters--your business logic.
-
8/14/2019 6 (J)Ruby on Rails-Sang Shin
33/72
33
Leaning Point:Leaning Point:
What is Rails Console?What is Rails Console?
-
8/14/2019 6 (J)Ruby on Rails-Sang Shin
34/72
34
What is Rails Console?
The Rails console gives you access to your RailsEnvironment, for example, you can interact withthe domain models of your application as if theapplication is actually running.
> Things you can do include performing find operationsor creating a new active record object and then savingit to the database.
A great tool for impromptu testing
-
8/14/2019 6 (J)Ruby on Rails-Sang Shin
35/72
35
Leaning Point:Leaning Point:
What is Rails Script?What is Rails Script?
-
8/14/2019 6 (J)Ruby on Rails-Sang Shin
36/72
36
Script
NetBeans runs Rails Script internally> You can run the Script at the command line
Useful scripts> console
> generate
> plugin
> server
-
8/14/2019 6 (J)Ruby on Rails-Sang Shin
37/72
-
8/14/2019 6 (J)Ruby on Rails-Sang Shin
38/72
38
Create Database Table using Migration
You are going to create a database table (in apreviously created database) through migration> You also use migration for any change you are going to
make in the schema - adding a new column, forexample
When you create a Model, the first version of themigration file is automatically created> db/migrate/migrate/001_create_messages.rb, which
defines initial structure of the table
class CreateMessages < ActiveRecord::Migration def self.up
create_table :messages do |t|t.string :greetingt.timestamps
end
end
-
8/14/2019 6 (J)Ruby on Rails-Sang Shin
39/72
39
Performing Migration
-
8/14/2019 6 (J)Ruby on Rails-Sang Shin
40/72
40
Leaning Point:Leaning Point:
What is Migration?What is Migration?
-
8/14/2019 6 (J)Ruby on Rails-Sang Shin
41/72
-
8/14/2019 6 (J)Ruby on Rails-Sang Shin
42/72
42
Migration To the Rescue
Migration can manage the evolution of a schema
With migrations, you can describe schemachanges in self-contained Ruby classes -migration files
You can check these migration files into aversion control system
Migration files are part of an application structure
You (and others) can choose a schema versionof choice, for example, several versions backfrom the current one
-
8/14/2019 6 (J)Ruby on Rails-Sang Shin
43/72
-
8/14/2019 6 (J)Ruby on Rails-Sang Shin
44/72
44
What is a Controller?
Action Controllers handle incoming Webrequests
A controller is made up of one or more actions
Actions are executed to handle the incomingrequests and then either render a template orredirect to another action.
An action is defined as a public method of acontroller
Mapping between a request URL and an actionis specified in the Rails routing map(configuration/routes.rb)
-
8/14/2019 6 (J)Ruby on Rails-Sang Shin
45/72
45
Create a Controller using Generator
You are going to create a controller usingGenerator
-
8/14/2019 6 (J)Ruby on Rails-Sang Shin
46/72
-
8/14/2019 6 (J)Ruby on Rails-Sang Shin
47/72
47
6. Write a View6. Write a View
-
8/14/2019 6 (J)Ruby on Rails-Sang Shin
48/72
48
What is a View?
View is represented by a set of templates thatget displayed.
Templates share data with controllers throughmutually accessible variables.
A template can be either in the form of*.rhtmlor*.erb file.> The *.erb file is searched first by Rails. If there is no
*.erb file, then *.rhtmlfile is used.
-
8/14/2019 6 (J)Ruby on Rails-Sang Shin
49/72
49
Creating *.rhmtl file (or *.erb file)
*.rhtmlor*.erb file is created under the directoryof/app/views/
-
8/14/2019 6 (J)Ruby on Rails-Sang Shin
50/72
-
8/14/2019 6 (J)Ruby on Rails-Sang Shin
51/72
51
7. Set URL Routing7. Set URL Routing
-
8/14/2019 6 (J)Ruby on Rails-Sang Shin
52/72
-
8/14/2019 6 (J)Ruby on Rails-Sang Shin
53/72
-
8/14/2019 6 (J)Ruby on Rails-Sang Shin
54/72
54
ScaffoldingScaffolding
-
8/14/2019 6 (J)Ruby on Rails-Sang Shin
55/72
55
What Is Scaffolding?
Scaffolding is a way to quickly put an ActiveRecord class online by providing a series ofstandardized actions for listing, showing,creating, updating, and destroying objects of theclass.> Useful for quick prototyping
These standardized actions come with bothcontroller logic and default templates thatthrough introspection already know which fieldsto display and which input types to use.
-
8/14/2019 6 (J)Ruby on Rails-Sang Shin
56/72
56
Scaffolding via Generator
-
8/14/2019 6 (J)Ruby on Rails-Sang Shin
57/72
-
8/14/2019 6 (J)Ruby on Rails-Sang Shin
58/72
-
8/14/2019 6 (J)Ruby on Rails-Sang Shin
59/72
59
Why JRuby (over Ruby) &Why JRuby (over Ruby) &
JRuby on Rails (over RubyJRuby on Rails (over Rubyon Rails)?on Rails)?
-
8/14/2019 6 (J)Ruby on Rails-Sang Shin
60/72
60
Why JRuby (over Ruby)
With JRuby you get the best of both worlds:> Ruby applications and libraries, plus Java libraries.
> You can access those Java libraries with Ruby syntax(or Java syntax, if you want).
On average JRuby, runs 2 and a half timesfaster than Ruby, except at startup
In addition to native threads, JRuby supportsUnicode natively
Code can be fully compiled ahead of time or justin time
-
8/14/2019 6 (J)Ruby on Rails-Sang Shin
61/72
61
Why JRuby on Rails (over Ruby on Rails)
A JRuby on Rails application can be packagedinto a WAR, and then deployed onto anycompliant server.> The packaging of creating a war file from Rails can be
done with Goldspike, or with the new kid on the block:Warbler
Can use vast array of Java libraries> JPA, JTA, JMS, EJB, JDBC, JMX, JSF, etc.
> Database connections are made through JDBC, whichprovides broader, more scalable database support.
Glassfish--the Java web app server that scaleswell--is available as a JRuby gem.
-
8/14/2019 6 (J)Ruby on Rails-Sang Shin
62/72
62
Demo:Demo:Build Ruby ApplicationBuild Ruby Applicationusing Java Libraryusing Java Library
javapassion.com/handsonlabs/ruby_jruby/#Exercise_3javapassion.com/handsonlabs/ruby_jruby/#Exercise_3
-
8/14/2019 6 (J)Ruby on Rails-Sang Shin
63/72
63
TestingTesting
-
8/14/2019 6 (J)Ruby on Rails-Sang Shin
64/72
64
Testing
Unit testing> Testing model classes
Functional testing> Testing controllers
Integration testing> Testing usage scenarios
-
8/14/2019 6 (J)Ruby on Rails-Sang Shin
65/72
-
8/14/2019 6 (J)Ruby on Rails-Sang Shin
66/72
66
DeploymentDeployment
-
8/14/2019 6 (J)Ruby on Rails-Sang Shin
67/72
67
Web Servers
By default, Rails will try to use Mongrel andlighttpd if they are installed, otherwise Rails willuse WEBrick, the webserver that ships withRuby.
Java Server integration> Goldspike
> GlassFish V3
-
8/14/2019 6 (J)Ruby on Rails-Sang Shin
68/72
68
Goldspike
Rails Plugin Packages Rails application as WAR
WAR contains a servlet that translates data fromthe servlet request to the Rails dispatcher
Works for any servlet container
rake war:standalone:create
-
8/14/2019 6 (J)Ruby on Rails-Sang Shin
69/72
69
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
-
8/14/2019 6 (J)Ruby on Rails-Sang Shin
70/72
70
Why Rails 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 developers.sun.com/appserver/reference/techart/ra
ils_gf/
-
8/14/2019 6 (J)Ruby on Rails-Sang Shin
71/72
71
Demo:Demo:Deployment throughDeployment through
GoldspikeGoldspike
-
8/14/2019 6 (J)Ruby on Rails-Sang Shin
72/72
Thank You!Thank You!
Sang Shin
javapassion.com