Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

executable file 15001 lines (10575 sloc) 229.407 kb
#LyX 2.0 created this file. For more info see http://www.lyx.org/
\lyxformat 413
\begin_document
\begin_header
\textclass hobo
\use_default_options true
\master hobo.lyx
\begin_modules
logicalmkup
\end_modules
\maintain_unincluded_children false
\language english
\language_package default
\inputencoding auto
\fontencoding global
\font_roman default
\font_sans default
\font_typewriter default
\font_default_family default
\use_non_tex_fonts false
\font_sc false
\font_osf false
\font_sf_scale 100
\font_tt_scale 100
\graphics default
\default_output_format default
\output_sync 0
\bibtex_command default
\index_command default
\float_placement H
\paperfontsize default
\spacing single
\use_hyperref false
\papersize default
\use_geometry false
\use_amsmath 1
\use_esint 1
\use_mhchem 1
\use_mathdots 1
\cite_engine basic
\use_bibtopic false
\use_indices false
\paperorientation portrait
\suppress_date false
\use_refstyle 1
\boxbgcolor #e6e6e6
\index Index
\shortcut idx
\color #008000
\end_index
\secnumdepth 3
\tocdepth 3
\paragraph_separation skip
\defskip smallskip
\quotes_language english
\papercolumns 1
\papersides 1
\paperpagestyle default
\tracking_changes false
\output_changes false
\html_math_output 0
\html_css_as_file 0
\html_be_strict false
\end_header
\begin_body
\begin_layout Standard
Introductory Concepts and Comments
\end_layout
\begin_layout Standard
Tutorial 1 - Directories and Generators
\end_layout
\begin_layout Standard
Tutorial 2 - Changing Field Names
\end_layout
\begin_layout Standard
Tutorial 3 - Field Validation
\end_layout
\begin_layout Standard
Tutorial 4 - Introduction to Permissions
\end_layout
\begin_layout Standard
Tutorial 5 - Hobo
\end_layout
\begin_layout Standard
Tutorial 6 - Editing the Navigation Tabs
\end_layout
\begin_layout Standard
Tutorial 7 - Model Relationships
\end_layout
\begin_layout Standard
Tutorial 8 - Model Relationships
\end_layout
\begin_layout Section*
Introductory Concepts and Comments
\end_layout
\begin_layout Standard
If you explain a magic trick before it is performed, you risk spoiling the
enjoyment.
There will be plenty of time after you work through a few of the tutorials
to learn what is going on “behind the curtain.”
\end_layout
\begin_layout Standard
So, in the spirit of this adventure we will explain just enough right now
to allow you to dive in head first…
\end_layout
\begin_layout Section*
Tutorial 1 – Directories and Generators
\end_layout
\begin_layout Standard
You will create a single-table application that demonstrates how Hobo constructs
a nice user interface that includes a built-in login system and basic search
capability.
Hobo 1.3 generators are compatible with the new Rails 3 generator API and
operate quite differently from Hobo 1.0 and Rails 2.x.
We will explain in more detail below.
\end_layout
\begin_layout Subsubsection*
Tutorial Application:
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
my-first-app
\end_layout
\end_inset
\end_layout
\begin_layout Subsubsection*
Topics
\end_layout
\begin_layout Itemize
Creating a Hobo application
\end_layout
\begin_layout Itemize
Learning the Hobo Directory structure
\end_layout
\begin_layout Itemize
Generating Hobo models and controllers
\end_layout
\begin_layout Itemize
Generating Hobo models
\end_layout
\begin_layout Itemize
Generating Hobo controllers
\end_layout
\begin_layout Itemize
Creating Migrations and Databases
\end_layout
\begin_layout Itemize
Editing Models and propagating the changes
\end_layout
\begin_layout Subsubsection*
Tutorial Application:
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
my-first-app
\end_layout
\end_inset
\end_layout
\begin_layout Subsubsection*
Steps
\end_layout
\begin_layout Standard
1.
\begin_inset Flex Strong
status collapsed
\begin_layout Plain Layout
Description of development tools.
\end_layout
\end_inset
You will use three tools to do the work in these tutorials.
They include:
\end_layout
\begin_layout Itemize
A shell command prompt to run scripts
\end_layout
\begin_layout Itemize
A text editor for you to edit your application files
\end_layout
\begin_layout Itemize
A browser to run and test your application
\end_layout
\begin_layout Standard
Ordinarily you will have two shell windows or tabs open: one from which
to run Hobo scripts or operating system commands and one from which to
run a web server (Mongrel in these tutorials).
These tutorials are not done with an integrated development environment
(IDE).
\end_layout
\begin_layout Standard
2.
\begin_inset Flex Strong
status collapsed
\begin_layout Plain Layout
Create a Hobo application directory.
\end_layout
\end_inset
Before you create your first Hobo application, create a directory called
tutorials.
This will be the directory where you keep all of your Hobo tutorials.
Navigate to the tutorials directory using your shell application.
You should now see the following prompt:
\end_layout
\begin_layout Standard
\begin_inset Box Shadowbox
position "t"
hor_pos "c"
has_inner_box 1
inner_pos "t"
use_parbox 0
use_makebox 0
width "100col%"
special "none"
height "1in"
height_special "totalheight"
status open
\begin_layout LyX-Code
\backslash
tutorials>
\end_layout
\end_inset
\end_layout
\begin_layout Standard
3.
\begin_inset Flex Strong
status collapsed
\begin_layout Plain Layout
Create a Hobo application.
\end_layout
\end_inset
All you have to do to create a Hobo application is to issue the Hobo command:
\end_layout
\begin_layout Standard
\begin_inset Box Shadowbox
position "t"
hor_pos "c"
has_inner_box 1
inner_pos "t"
use_parbox 0
use_makebox 0
width "100col%"
special "none"
height "1in"
height_special "totalheight"
status open
\begin_layout LyX-Code
\backslash
tutorials> hobo new my-first-app
\end_layout
\end_inset
\end_layout
\begin_layout Standard
\begin_inset Float figure
wide false
sideways false
status open
\begin_layout Plain Layout
\begin_inset Graphics
filename figures/figure55.png
width 90col%
\end_inset
\end_layout
\begin_layout Plain Layout
\begin_inset Caption
\begin_layout Plain Layout
Command Prompt with Ruby and Rails
\end_layout
\end_inset
\end_layout
\begin_layout Plain Layout
\end_layout
\end_inset
\end_layout
\begin_layout Standard
\begin_inset Box Shaded
position "t"
hor_pos "c"
has_inner_box 1
inner_pos "t"
use_parbox 0
use_makebox 0
width "100col%"
special "none"
height "1in"
height_special "totalheight"
status open
\begin_layout Description
Note: The screen captures were taken with Hobo 1.3 pre-release # 26 as of
2/15/2011.
\end_layout
\end_inset
\end_layout
\begin_layout Standard
You will see a log of what Hobo is creating go by within the shell window
that you will better understand as you learn Hobo’s directory structure.
The first prompt from the Hobo Setup Wizard will appear as follows:
\end_layout
\begin_layout Standard
\begin_inset Float figure
wide false
sideways false
status open
\begin_layout Plain Layout
\begin_inset Graphics
filename figures/figure56.png
width 90col%
\end_inset
\end_layout
\begin_layout Plain Layout
\begin_inset Caption
\begin_layout Plain Layout
Command Prompt with Ruby on Rails
\end_layout
\end_inset
\end_layout
\begin_layout Plain Layout
\end_layout
\end_inset
\end_layout
\begin_layout Standard
The Hobo Setup Wizard will present you with the following eight options,
for this tutorial choose the following “default” options:
\end_layout
\begin_layout Standard
\begin_inset Box Shadowbox
position "t"
hor_pos "c"
has_inner_box 1
inner_pos "t"
use_parbox 0
use_makebox 0
width "100col%"
special "none"
height "1in"
height_special "totalheight"
status open
\begin_layout LyX-Code
Question Defaults
\end_layout
\begin_layout LyX-Code
Do you want to customize the test_framework? [y|n]
\begin_inset Flex Strong
status collapsed
\begin_layout Plain Layout
n
\end_layout
\end_inset
\end_layout
\begin_layout LyX-Code
a.
Choose a name for the user resource
\end_layout
\begin_layout LyX-Code
[<enter>=user|<custom_name>]
\begin_inset Flex Strong
status collapsed
\begin_layout Plain Layout
<enter>
\end_layout
\end_inset
\end_layout
\begin_layout LyX-Code
b.
Do you want to send an activation
\end_layout
\begin_layout LyX-Code
email to activate the user? [y|n]
\begin_inset Flex Strong
status collapsed
\begin_layout Plain Layout
n
\end_layout
\end_inset
\end_layout
\begin_layout LyX-Code
c.
Do you want to add the features
\end_layout
\begin_layout LyX-Code
for an invite only website? [y|n]
\begin_inset Flex Strong
status collapsed
\begin_layout Plain Layout
n
\end_layout
\end_inset
\end_layout
\begin_layout LyX-Code
d.
Will you application use only hobo/dryml
\end_layout
\begin_layout LyX-Code
web page templates? (Choose 'n' only if you
\end_layout
\begin_layout LyX-Code
also plan to use plain rails/erb web page
\end_layout
\begin_layout LyX-Code
templates) [y|n]
\begin_inset Flex Strong
status collapsed
\begin_layout Plain Layout
y
\end_layout
\end_inset
\end_layout
\begin_layout LyX-Code
e.
Choose a name for the front controller
\end_layout
\begin_layout LyX-Code
[<enter>=front|<custom_name>]:
\begin_inset Flex Strong
status collapsed
\begin_layout Plain Layout
<enter>
\end_layout
\end_inset
\end_layout
\begin_layout LyX-Code
f.
Initial Migration: [s]kip, [g]enerate
\end_layout
\begin_layout LyX-Code
migration file only, generate and
\end_layout
\begin_layout LyX-Code
[m]igrate [s|g|m]:
\begin_inset Flex Strong
status collapsed
\begin_layout Plain Layout
m
\end_layout
\end_inset
\end_layout
\begin_layout LyX-Code
g.
Type the locales (space separated) you
\end_layout
\begin_layout LyX-Code
want to add to your application or
\end_layout
\begin_layout LyX-Code
<enter> for 'en':
\begin_inset Flex Strong
status collapsed
\begin_layout Plain Layout
<enter>
\end_layout
\end_inset
\end_layout
\begin_layout LyX-Code
h.
Do you want to initialize a git repository
\end_layout
\begin_layout LyX-Code
now? [y|n]
\begin_inset Flex Strong
status collapsed
\begin_layout Plain Layout
n
\end_layout
\end_inset
\end_layout
\end_inset
\end_layout
\begin_layout Standard
You will see the following message upon completion:
\end_layout
\begin_layout Standard
\begin_inset Float figure
wide false
sideways false
status open
\begin_layout Plain Layout
\begin_inset Graphics
filename figures/figure57.png
width 90col%
\end_inset
\end_layout
\begin_layout Plain Layout
\begin_inset Caption
\begin_layout Plain Layout
Completion Message
\end_layout
\end_inset
\end_layout
\begin_layout Plain Layout
\end_layout
\end_inset
\end_layout
\begin_layout Standard
4.
\begin_inset Flex Strong
status collapsed
\begin_layout Plain Layout
Start the web server.
\end_layout
\end_inset
Create a second shell window (or tab).
You are now going to start a local web server on your computer.
This will enable you to run the Hobo application and see what a deployed
application looks like in your browser.
\end_layout
\begin_layout Standard
Navigate to your application directory and fire up the local web server
by issuing the following command at your command prompt.
\end_layout
\begin_layout LyX-Code
\begin_inset Box Boxed
position "t"
hor_pos "c"
has_inner_box 1
inner_pos "t"
use_parbox 0
use_makebox 0
width "100col%"
special "none"
height "1in"
height_special "totalheight"
status open
\begin_layout Plain Layout
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
\backslash
my-first-app> rails server
\end_layout
\end_inset
\end_layout
\end_inset
\end_layout
\begin_layout Standard
While your server is executing, it does
\begin_inset Flex Strong
status collapsed
\begin_layout Plain Layout
not
\end_layout
\end_inset
return you to your command prompt.
As you run your application, it logs what it is doing to this shell.
You can terminate the web server by typing control-c and restart it the
same way you started it above, but do not terminate the server.
\end_layout
\begin_layout Standard
5.
\begin_inset Flex Strong
status collapsed
\begin_layout Plain Layout
Open your application in a web browser.
\end_layout
\end_inset
Type the following URL into your browser:
\end_layout
\begin_layout Standard
\begin_inset Box Shadowbox
position "t"
hor_pos "c"
has_inner_box 1
inner_pos "t"
use_parbox 0
use_makebox 0
width "100col%"
special "none"
height "1in"
height_special "totalheight"
status open
\begin_layout LyX-Code
http://localhost:3000/
\end_layout
\end_inset
\end_layout
\begin_layout Standard
The following “Register Administrator” page will appear:
\end_layout
\begin_layout Standard
\begin_inset Float figure
wide false
sideways false
status open
\begin_layout Plain Layout
\begin_inset Graphics
filename figures/figure58.png
width 90col%
\end_inset
\end_layout
\begin_layout Plain Layout
\begin_inset Caption
\begin_layout Plain Layout
Register Administrator Screen
\end_layout
\end_inset
\end_layout
\begin_layout Plain Layout
\end_layout
\end_inset
\end_layout
\begin_layout Standard
Hobo, by default, assumes the first person that launches the application
will be an administrator.
Go ahead and enter the information required and click on the “Register
Administrator” button:
\end_layout
\begin_layout Standard
\begin_inset Box Shaded
position "t"
hor_pos "c"
has_inner_box 1
inner_pos "t"
use_parbox 0
use_makebox 0
width "100col%"
special "none"
height "1in"
height_special "totalheight"
status open
\begin_layout Description
Note: The first person to register is assigned the administrator privileges
by Hobo.
Notice that in the upper right-hand corner of your web page there is a
drop-down list of created users that allows you to sign in automatically
to any of the user accounts without going through the login page if you
are in development mode.
This is turned off in production mode.
\end_layout
\end_inset
\end_layout
\begin_layout Standard
\begin_inset Float figure
wide false
sideways false
status open
\begin_layout Plain Layout
\begin_inset Graphics
filename figures/figure59.png
width 90col%
\end_inset
\end_layout
\begin_layout Plain Layout
\begin_inset Caption
\begin_layout Plain Layout
Register Administrator Screen
\end_layout
\end_inset
\end_layout
\begin_layout Plain Layout
\end_layout
\end_inset
\end_layout
\begin_layout Standard
This is how your app looks after registering:
\end_layout
\begin_layout Standard
\begin_inset Float figure
wide false
sideways false
status open
\begin_layout Plain Layout
\begin_inset Graphics
filename figures/figure60.png
width 90col%
\end_inset
\end_layout
\begin_layout Plain Layout
\begin_inset Caption
\begin_layout Plain Layout
Completed Registration
\end_layout
\end_inset
\end_layout
\begin_layout Plain Layout
\end_layout
\end_inset
\end_layout
\begin_layout Standard
Now let’s take a look at what Hobo has generated so far.
Use your text editor to locate the
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
user.rb
\end_layout
\end_inset
file under the
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
app/models
\end_layout
\end_inset
directory:
\end_layout
\begin_layout Standard
\begin_inset Float figure
wide false
sideways false
status open
\begin_layout Plain Layout
\begin_inset Graphics
filename figures/figure61.png
width 90col%
\end_inset
\end_layout
\begin_layout Plain Layout
\begin_inset Caption
\begin_layout Plain Layout
The default User model created by Hobo
\end_layout
\end_inset
\end_layout
\begin_layout Plain Layout
\end_layout
\end_inset
\end_layout
\begin_layout Standard
Hobo took care of building the User model and generating the database table
needed because we selected the “m” (generate and migrate) option for step
6 of the Hobo Setup Wizard:
\end_layout
\begin_layout Standard
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
Initial Migration: [s]kip, [g]enerate migration file only, generate and
[m]igrate [s|g|m]:
\end_layout
\end_inset
\end_layout
\begin_layout Standard
6.
\begin_inset Flex Strong
status collapsed
\begin_layout Plain Layout
Examine what Hobo created during the first “migration”.
\end_layout
\end_inset
In the following figure, you can see that the db directory is populated.
\end_layout
\begin_layout Standard
a.
The file,
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
development.sqllite3
\end_layout
\end_inset
, is the database file.
\end_layout
\begin_layout Standard
b.
The
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
<timestamp>_initial_migration.rb
\end_layout
\end_inset
file defines the database table that will be created when the migration
is executed.
\end_layout
\begin_layout Standard
c.
The
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
schema.rb
\end_layout
\end_inset
file shows the current database schema after all migration executions to
date.
In this case we have only created the user table.
\end_layout
\begin_layout Standard
\begin_inset Float figure
wide false
sideways false
status open
\begin_layout Plain Layout
\begin_inset Graphics
filename figures/figure62.png
width 90col%
\end_inset
\end_layout
\begin_layout Plain Layout
\begin_inset Caption
\begin_layout Plain Layout
Contents of the first Hobo migration file
\end_layout
\end_inset
\end_layout
\begin_layout Plain Layout
\end_layout
\end_inset
\end_layout
\begin_layout Standard
Take a look at the schema and you will see that it corresponds to the migration
file:
\end_layout
\begin_layout Standard
\begin_inset Float figure
wide false
sideways false
status open
\begin_layout Plain Layout
\begin_inset Graphics
filename figures/figure63.png
width 90col%
\end_inset
\end_layout
\begin_layout Plain Layout
\begin_inset Caption
\begin_layout Plain Layout
Contents of the "schema.rb" file after the first migration
\end_layout
\end_inset
\end_layout
\begin_layout Plain Layout
\end_layout
\end_inset
\end_layout
\begin_layout Standard
\begin_inset Box Shaded
position "t"
hor_pos "c"
has_inner_box 1
inner_pos "t"
use_parbox 0
use_makebox 0
width "100col%"
special "none"
height "1in"
height_special "totalheight"
status open
\begin_layout Description
Note: You can see that the User model does not display all the fields that
are implemented in the database.
Hobo does not expose all of the User fields but reserves them for its own
use.
All of the fields in other models will be reflected in the schema file.
<def tag= new-page> .....
</end>
\end_layout
\end_inset
\end_layout
\begin_layout Standard
Click on the “Logout” link and then click on the “Signup” link to create
another account.
In the example below we are creating another account for “John Smith”.
We will call this and all other accounts you create user accounts, because
by default they will not have administrative privileges.
\end_layout
\begin_layout Standard
Log out of the user (e.g., John Smith) account you just created and login
using the account you created as administrator (e.g., Owen Dall) for now.
\end_layout
\begin_layout Standard
\begin_inset Box Shaded
position "t"
hor_pos "c"
has_inner_box 1
inner_pos "t"
use_parbox 0
use_makebox 0
width "100col%"
special "none"
height "1in"
height_special "totalheight"
status open
\begin_layout Description
Note: You will use the user’s email address and password to log in, not
the user’s name.
Also, notice that in the upper right corner of your web page, there is
a drop down box that lets you automatically login to any of your accounts
without using the normal login page.
This speeds up testing permission customizations in development mode.
In production mode this option disappears.
More on switching modes later.
\end_layout
\end_inset
\end_layout
\begin_layout Standard
\begin_inset Float figure
wide false
sideways false
status open
\begin_layout Plain Layout
\begin_inset Graphics
filename figures/figure64.png
width 90col%
\end_inset
\end_layout
\begin_layout Plain Layout
\begin_inset Caption
\begin_layout Plain Layout
Drop down selector for the active user
\end_layout
\end_inset
\end_layout
\begin_layout Plain Layout
\end_layout
\end_inset
\end_layout
\begin_layout Standard
7.
\begin_inset Flex Strong
status collapsed
\begin_layout Plain Layout
Check the changes in the views/taglibs directory.
\end_layout
\end_inset
Notice that since you fired up your web server, there is now a change in
the taglibs directory.
There is a new branch called
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
views/taglibs/auto/rapid
\end_layout
\end_inset
and three files in that directory:
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
cards.dryml
\end_layout
\end_inset
,
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
forms.dryml
\end_layout
\end_inset
and
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
pages.dryml
\end_layout
\end_inset
.
We are going to show you a few things to pique your curiosity but we will
not cover how Hobo handles views in any detail until the intermediate tutorials.
We will just make a few high level comments here in case you know something
about Ruby on Rails and so you know what is coming.
\end_layout
\begin_layout Standard
Familiarize yourself with the contents of these files.
You will see many lines that look similar to:
\end_layout
\begin_layout Standard
\begin_inset Box Shadowbox
position "t"
hor_pos "c"
has_inner_box 1
inner_pos "t"
use_parbox 0
use_makebox 0
width "100col%"
special "none"
height "1in"
height_special "totalheight"
status open
\begin_layout LyX-Code
<def tag= new-page>
\end_layout
\begin_layout LyX-Code
.....
\end_layout
\begin_layout LyX-Code
</end>
\end_layout
\end_inset
\end_layout
\begin_layout Standard
You will see mark-up in between the “def” and “end” tags.
The contents are what we have mentioned before as “tag definitions.” Hobo
uses them to construct view templates on the fly.
\end_layout
\begin_layout Standard
These three files contain the libraries of tags that Hobo uses to construct
view templates.
\end_layout
\begin_layout Standard
\begin_inset Float figure
wide false
sideways false
status open
\begin_layout Plain Layout
\begin_inset Graphics
filename figures/figure65.png
width 90col%
\end_inset
\end_layout
\begin_layout Plain Layout
\begin_inset Caption
\begin_layout Plain Layout
Location of the Rapid templates
\end_layout
\end_inset
\end_layout
\begin_layout Plain Layout
\end_layout
\end_inset
\end_layout
\begin_layout Standard
\begin_inset Box Shaded
position "t"
hor_pos "c"
has_inner_box 1
inner_pos "t"
use_parbox 0
use_makebox 0
width "100col%"
special "none"
height "1in"
height_special "totalheight"
status open
\begin_layout Description
Note: When Hobo makes a web page, it takes tags from the
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
pages.dryml
\end_layout
\end_inset
file.
When it wants to construct a data entry form, tags in the
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
pages.dryml
\end_layout
\end_inset
file call tags in the
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
form.dryml
\end_layout
\end_inset
file.
When Hobo wants to list the records from a table, tags in the
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
pages.dryml
\end_layout
\end_inset
file call tags in the
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
cards.dryml
\end_layout
\end_inset
file.
Card tags define how individual database table records are rendered.
\end_layout
\begin_deeper
\begin_layout Plain Layout
(Actually, these files are a copy of what Hobo is doing on the fly behind
the scenes but it is easier to think of it in this way.)
\end_layout
\begin_layout Plain Layout
You will learn that you can edit and redefine the tags from the
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
/rapid
\end_layout
\end_inset
directory.
When you want your changes to be available to the application, you can
either put the new tags in the
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
application.dryml
\end_layout
\end_inset
file or create a
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
taglibs/application
\end_layout
\end_inset
folder and save your tags to a
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
taglibs/application/<filename>.dryml
\end_layout
\end_inset
.
Any
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
dryml
\end_layout
\end_inset
file located in the
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
taglibs/application
\end_layout
\end_inset
directory will automatically loaded and its tags will be available application
wide.
When you want them to be available only in a particular view you can put
them in a
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
dryml
\end_layout
\end_inset
file under the
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
app/views/<model>
\end_layout
\end_inset
directory named for the model.
\end_layout
\begin_layout Plain Layout
So far, we only have the front (home page) and the users template directories.
You will see after creating a new model (running hobo g resource or hobo
g model) and running hobo g migration, that directories will be created
and named for your new models.
\end_layout
\end_deeper
\end_inset
\end_layout
\begin_layout Standard
8.
\begin_inset Flex Strong
status collapsed
\begin_layout Plain Layout
Create a new model and controller.
\end_layout
\end_inset
Let’s create a simple contacts model and see what Hobo does for us.
\end_layout
\begin_layout LyX-Code
\begin_inset Box Boxed
position "t"
hor_pos "c"
has_inner_box 1
inner_pos "t"
use_parbox 0
use_makebox 0
width "100col%"
special "none"
height "1in"
height_special "totalheight"
status open
\begin_layout Plain Layout
\backslash
my-first-app> hobo g resource contact name:string company:string
\end_layout
\end_inset
\end_layout
\begin_layout Standard
This generator will create both a model and controller.
Execute it and then take a look at what has changed in your application
directories.
\end_layout
\begin_layout Standard
You will see the new
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
contacts_controller.rb
\end_layout
\end_inset
file in the
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
/controllers
\end_layout
\end_inset
directory and the new
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
contact.rb
\end_layout
\end_inset
file in the
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
/models
\end_layout
\end_inset
directory.
\end_layout
\begin_layout Standard
\begin_inset Box Shaded
position "t"
hor_pos "c"
has_inner_box 1
inner_pos "t"
use_parbox 0
use_makebox 0
width "100col%"
special "none"
height "1in"
height_special "totalheight"
status open
\begin_layout Description
Note: Unlike Hobo 1.0, a view template file is not created in the views/contacts
folder.
We will discuss later how the rapid taglibs in the /auto/rapid folder take
care of the default views for you).
\end_layout
\end_inset
\end_layout
\begin_layout Standard
\begin_inset Float figure
wide false
sideways false
status open
\begin_layout Plain Layout
\begin_inset Graphics
filename figures/figure66.png
width 90col%
\end_inset
\end_layout
\begin_layout Plain Layout
\begin_inset Caption
\begin_layout Plain Layout
Folder location for Models and Views
\end_layout
\end_inset
\end_layout
\begin_layout Plain Layout
\end_layout
\end_inset
\end_layout
\begin_layout Standard
9.
\begin_inset Flex Strong
status collapsed
\begin_layout Plain Layout
Run a Hobo migration.
\end_layout
\end_inset
Before you run the migration, take a look at the
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
contact.rb
\end_layout
\end_inset
model file.
We just want to review the relevant part for now.
The permissions part will be explained in a later tutorial.
\end_layout
\begin_layout Standard
Here is the code that declares the fields that you want in your database
table that will be called
\begin_inset Flex Emph
status collapsed
\begin_layout Plain Layout
contacts
\end_layout
\end_inset
.
When you ran
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
hobo g resource
\end_layout
\end_inset
, it generated this code.
\end_layout
\begin_layout Standard
\begin_inset Box Shadowbox
position "t"
hor_pos "c"
has_inner_box 1
inner_pos "t"
use_parbox 0
use_makebox 0
width "100col%"
special "none"
height "1in"
height_special "totalheight"
status open
\begin_layout Plain Layout
class Contact < ActiveRecord::Base
\end_layout
\begin_layout LyX-Code
hobo_model # Don't put anything above this
\end_layout
\begin_layout LyX-Code
\end_layout
\begin_layout LyX-Code
fields do
\end_layout
\begin_layout LyX-Code
name :string
\end_layout
\begin_layout LyX-Code
company :string timestamps
\end_layout
\begin_layout LyX-Code
end
\end_layout
\end_inset
\end_layout
\begin_layout Standard
When you run
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
hobo g migration
\end_layout
\end_inset
, Hobo will take this declaration and create a migration file.
It will then in turn use the migration file to create the database table.
These two steps will be executed within a single Hobo migration.
You could do them separately but we will not do that here.
\end_layout
\begin_layout Standard
Now run
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
hobo g migration
\end_layout
\end_inset
and observe what happens.
\end_layout
\begin_layout Standard
\begin_inset Box Shadowbox
position "t"
hor_pos "c"
has_inner_box 1
inner_pos "t"
use_parbox 0
use_makebox 0
width "100col%"
special "none"
height "1in"
height_special "totalheight"
status open
\begin_layout LyX-Code
\backslash
my-first-app> hobo g migration
\end_layout
\end_inset
\end_layout
\begin_layout Standard
Remember to select the ‘m’ option to both create and execute the migration
file.
Then hit return to accept the proposed name of the migration file.
\end_layout
\begin_layout Standard
You will notice some changes now in the
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
my-first-app/db directory
\end_layout
\end_inset
of your app.
\end_layout
\begin_layout Standard
\begin_inset Float figure
wide false
sideways false
status open
\begin_layout Plain Layout
\begin_inset Graphics
filename figures/figure67.png
width 90col%
\end_inset
\end_layout
\begin_layout Plain Layout
\begin_inset Caption
\begin_layout Plain Layout
Migration file changes
\end_layout
\end_inset
\end_layout
\begin_layout Plain Layout
\end_layout
\end_inset
\end_layout
\begin_layout Standard
There is a new migration file and changes in your schema file as well.
The new migration file contains the following code:
\end_layout
\begin_layout Standard
\begin_inset Box Shadowbox
position "t"
hor_pos "c"
has_inner_box 1
inner_pos "t"
use_parbox 0
use_makebox 0
width "100col%"
special "none"
height "1in"
height_special "totalheight"
status open
\begin_layout LyX-Code
def self.up
\end_layout
\begin_layout LyX-Code
create_table :contacts do |t|
\end_layout
\begin_layout LyX-Code
t.string :name
\end_layout
\begin_layout LyX-Code
t.string :company
\end_layout
\begin_layout LyX-Code
t.datetime :created_at
\end_layout
\begin_layout LyX-Code
t.datetime :updated_at
\end_layout
\begin_layout LyX-Code
end
\end_layout
\end_inset
\end_layout
\begin_layout Standard
The schema file (
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
schema.rb
\end_layout
\end_inset
), reflecting this code, shows the current state of the database in the
db/schema file:
\end_layout
\begin_layout Standard
\begin_inset Box Shadowbox
position "t"
hor_pos "c"
has_inner_box 1
inner_pos "t"
use_parbox 0
use_makebox 0
width "100col%"
special "none"
height "1in"
height_special "totalheight"
status open
\begin_layout LyX-Code
create_table "contacts", :force => true do |t|
\end_layout
\begin_layout LyX-Code
t.string "name"
\end_layout
\begin_layout LyX-Code
t.string "company"
\end_layout
\begin_layout LyX-Code
t.datetime "created_at"
\end_layout
\begin_layout LyX-Code
t.datetime "updated_at"
\end_layout
\begin_layout LyX-Code
end
\end_layout
\end_inset
\end_layout
\begin_layout Standard
Now check out the application in your browser with the following URL after
running the command rails server
\end_layout
\begin_layout Standard
\begin_inset CommandInset href
LatexCommand href
target "http://localhost:3000/"
\end_inset
\end_layout
\begin_layout Standard
\begin_inset Float figure
wide false
sideways false
status open
\begin_layout Plain Layout
\begin_inset Graphics
filename figures/figure68.png
width 90col%
\end_inset
\end_layout
\begin_layout Plain Layout
\begin_inset Caption
\begin_layout Plain Layout
Contacts tab on
\begin_inset Quotes eld
\end_inset
My First App
\begin_inset Quotes erd
\end_inset
\end_layout
\end_inset
\end_layout
\begin_layout Plain Layout
\end_layout
\end_inset
\end_layout
\begin_layout Standard
Now you have a new tab called “Contacts.”
\end_layout
\begin_layout Standard
10.
\begin_inset Flex Strong
status collapsed
\begin_layout Plain Layout
Create some contacts.
\end_layout
\end_inset
Now you should be able to create a new contact by clicking the ‘New Contact’
link in the Contacts tab.
Go ahead and create a couple of new contacts to convince yourself that
the database entry actually works.
While you are at it also try editing a contact.
\end_layout
\begin_layout Standard
So far, Hobo is doing a pretty decent job.
You have a usable UI, I/O capability for your contact model and a login
system and you have written no code.
\end_layout
\begin_layout Standard
11.
\begin_inset Flex Strong
status collapsed
\begin_layout Plain Layout
Try out the search facility.
\end_layout
\end_inset
Type the name of one your contacts to exercise the search facility.
The default search searches “name” fields.
You need at lease three characters for a partial word search.
\end_layout
\begin_layout Standard
12.
\begin_inset Flex Strong
status collapsed
\begin_layout Plain Layout
Add columns to the database.
\end_layout
\end_inset
Now we are going to add a couple more fields to the model and have hobo
add columns to the database.
In this and the following steps, you will get a sense for the power of
the
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
hobo g migration
\end_layout
\end_inset
generator.
Since we have already generated our model using
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
hobo g resource
\end_layout
\end_inset
, we do not have to do that again.
Go into the model and add some new fields.
Your code should now look like this:
\end_layout
\begin_layout Standard
\begin_inset Box Shadowbox
position "t"
hor_pos "c"
has_inner_box 1
inner_pos "t"
use_parbox 0
use_makebox 0
width "100col%"
special "none"
height "1in"
height_special "totalheight"
status open
\begin_layout Plain Layout
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
class Contact < ActiveRecord::Base
\end_layout
\end_inset
\end_layout
\begin_layout LyX-Code
hobo_model # Don't put anything above this
\end_layout
\begin_layout LyX-Code
\end_layout
\begin_layout LyX-Code
fields do
\end_layout
\begin_layout LyX-Code
name :string
\end_layout
\begin_layout LyX-Code
company :string
\end_layout
\begin_layout LyX-Code
address_1 :string
\end_layout
\begin_layout LyX-Code
address_2 :string
\end_layout
\begin_layout LyX-Code
city :string
\end_layout
\begin_layout LyX-Code
state :string
\end_layout
\begin_layout LyX-Code
date_met :date
\end_layout
\begin_layout LyX-Code
married :boolean
\end_layout
\begin_layout LyX-Code
age :integer
\end_layout
\begin_layout LyX-Code
notes :text
\end_layout
\begin_layout LyX-Code
timestamps
\end_layout
\begin_layout LyX-Code
end
\end_layout
\end_inset
\end_layout
\begin_layout Standard
Make sure you save your changes and run
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
hobo g migration
\end_layout
\end_inset
.
Select the ‘m’ option and accept the default filename for the migration.
\end_layout
\begin_layout Standard
\begin_inset Box Shadowbox
position "t"
hor_pos "c"
has_inner_box 1
inner_pos "t"
use_parbox 0
use_makebox 0
width "100col%"
special "none"
height "1in"
height_special "totalheight"
status open
\begin_layout LyX-Code
\backslash
my-first-app> hobo g migration
\end_layout
\end_inset
\end_layout
\begin_layout Standard
Now refresh your browser.
Go to the contacts tab and click ‘New Contact ’
\end_layout
\begin_layout Standard
\begin_inset Float figure
wide false
sideways false
status open
\begin_layout Plain Layout
\begin_inset Graphics
filename figures/figure69.png
width 90col%
\end_inset
\end_layout
\begin_layout Plain Layout
\begin_inset Caption
\begin_layout Plain Layout
New Contact page for
\begin_inset Quotes eld
\end_inset
My First App
\begin_inset Quotes erd
\end_inset
\end_layout
\end_inset
\end_layout
\begin_layout Plain Layout
\end_layout
\end_inset
\end_layout
\begin_layout Standard
Note what Hobo has done for you.
It determines which entry controls you need based on the type of field
you defined in your model.
It has one-line fields for strings, a set of three combo boxes for dates,
a one-line field for integers, a check box for boolean field, and a multi-line
box for text fields.
Later you will see that Hobo can provide the controls you need for multi-model
situations.
\end_layout
\begin_layout Standard
Hobo has also provided reasonable names and styles from the field names.
It removed the underscore characters and appropriately capitalized words
to give the presentation a nice look and feel.
\end_layout
\begin_layout Standard
13.
\begin_inset Flex Strong
status collapsed
\begin_layout Plain Layout
Remove columns from the database.
\end_layout
\end_inset
Now suppose you decide that you need only one address field and you decide
to remove the second one.
Go back to the Contact model and delete it (we just commented it out with
the # sign so you can see things clearer.)
\end_layout
\begin_layout Standard
\begin_inset Float figure
wide false
sideways false
status open
\begin_layout Plain Layout
\begin_inset Graphics
filename figures/figure70.png
width 90col%
\end_inset
\end_layout
\begin_layout Plain Layout
\begin_inset Caption
\begin_layout Plain Layout
Remove field from contact model
\end_layout
\end_inset
\end_layout
\begin_layout Plain Layout
\end_layout
\end_inset
\end_layout
\begin_layout Standard
\begin_inset Box Shadowbox
position "t"
hor_pos "c"
has_inner_box 1
inner_pos "t"
use_parbox 0
use_makebox 0
width "100col%"
special "none"
height "1in"
height_special "totalheight"
status open
\begin_layout Plain Layout
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
class Contact < ActiveRecord::Base
\end_layout
\end_inset
\end_layout
\begin_layout LyX-Code
hobo_model # Don't put anything above this
\end_layout
\begin_layout LyX-Code
\end_layout
\begin_layout LyX-Code
fields do
\end_layout
\begin_layout LyX-Code
name :string
\end_layout
\begin_layout LyX-Code
company :string
\end_layout
\begin_layout LyX-Code
address_1 :string
\end_layout
\begin_layout LyX-Code
#address_2:string
\end_layout
\begin_layout LyX-Code
city :string
\end_layout
\begin_layout LyX-Code
state :string
\end_layout
\begin_layout LyX-Code
date_met :date
\end_layout
\begin_layout LyX-Code
married :boolean
\end_layout
\begin_layout LyX-Code
age :integer
\end_layout
\begin_layout LyX-Code
notes :text
\end_layout
\begin_layout LyX-Code
timestamps
\end_layout
\begin_layout LyX-Code
end
\end_layout
\end_inset
\end_layout
\begin_layout Standard
Run
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
hobo g migration
\end_layout
\end_inset
again.
\end_layout
\begin_layout LyX-Code
\begin_inset Box Boxed
position "t"
hor_pos "c"
has_inner_box 1
inner_pos "t"
use_parbox 0
use_makebox 0
width "100col%"
special "none"
height "1in"
height_special "totalheight"
status open
\begin_layout Plain Layout
\backslash
my-first-app> hobo g migration
\end_layout
\end_inset
\end_layout
\begin_layout Standard
Hobo notices that you have deleted a model field and responds in this way.
\end_layout
\begin_layout Standard
\begin_inset Box Shadowbox
position "t"
hor_pos "c"
has_inner_box 1
inner_pos "t"
use_parbox 0
use_makebox 0
width "100col%"
special "none"
height "1in"
height_special "totalheight"
status open
\begin_layout LyX-Code
CONFIRM DROP! column contacts.address_2
\end_layout
\begin_layout LyX-Code
Enter 'drop address_2' to confirm:
\end_layout
\end_inset
\end_layout
\begin_layout Standard
You respond by typing what it asks (without the quotes).
\end_layout
\begin_layout Standard
\begin_inset Box Shadowbox
position "t"
hor_pos "c"
has_inner_box 1
inner_pos "t"
use_parbox 0
use_makebox 0
width "100col%"
special "none"
height "1in"
height_special "totalheight"
status open
\begin_layout LyX-Code
CONFIRM DROP! column contacts.address_2
\end_layout
\begin_layout LyX-Code
Enter 'drop address_2' to confirm: drop address_2
\end_layout
\end_inset
\end_layout
\begin_layout Standard
Complete the migration as you have learned above.
Then go check the db directory.
You will see another migration,
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
*_hobo_migration_4.rb
\end_layout
\end_inset
with the following code.
(The asterisk (*) here stands for the time/date stamp that precedes the
rest of the migration file name.)
\end_layout
\begin_layout Standard
\begin_inset Box Shadowbox
position "t"
hor_pos "c"
has_inner_box 1
inner_pos "t"
use_parbox 0
use_makebox 0
width "100col%"
special "none"
height "1in"
height_special "totalheight"
status open
\begin_layout Plain Layout
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
class HoboMigration4 < ActiveRecord::Migration
\end_layout
\end_inset
\end_layout
\begin_layout LyX-Code
def self.up
\end_layout
\begin_layout LyX-Code
remove_column :contacts, :address_2
\end_layout
\begin_layout LyX-Code
end
\end_layout
\begin_layout LyX-Code
\end_layout
\begin_layout LyX-Code
def self.down
\end_layout
\begin_layout LyX-Code
add_column :contacts, :address_2, :string
\end_layout
\begin_layout LyX-Code
end
\end_layout
\begin_layout LyX-Code
end
\end_layout
\end_inset
\end_layout
\begin_layout Standard
Check out the
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
schema.rb
\end_layout
\end_inset
file now.
\end_layout
\begin_layout Standard
\begin_inset Box Shadowbox
position "t"
hor_pos "c"
has_inner_box 1
inner_pos "t"
use_parbox 0
use_makebox 0
width "100col%"
special "none"
height "1in"
height_special "totalheight"
status open
\begin_layout Plain Layout
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
ActiveRecord::Schema.define(:version => 20090220154125) do
\end_layout
\end_inset
\end_layout
\begin_layout LyX-Code
create_table "contacts", :force => true do |t|
\end_layout
\begin_layout LyX-Code
t.string "name"
\end_layout
\begin_layout LyX-Code
t.string "company"
\end_layout
\begin_layout LyX-Code
t.datetime "created_at"
\end_layout
\begin_layout LyX-Code
t.datetime "updated_at"
\end_layout
\begin_layout LyX-Code
t.string "address_1"
\end_layout
\begin_layout LyX-Code
t.string "city"
\end_layout
\begin_layout LyX-Code
t.string "state"
\end_layout
\begin_layout LyX-Code
t.date "date_met"
\end_layout
\begin_layout LyX-Code
t.boolean "married"
\end_layout
\begin_layout LyX-Code
t.integer “age”
\end_layout
\begin_layout LyX-Code
t.text "notes"
\end_layout
\begin_layout LyX-Code
end
\end_layout
\end_inset
\end_layout
\begin_layout Standard
You can see that address_2 is gone.
\end_layout
\begin_layout Standard
14.
\begin_inset Flex Strong
status collapsed
\begin_layout Plain Layout
Adding and removing database tables.
\end_layout
\end_inset
You can also use
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
hobo g migration
\end_layout
\end_inset
to remove (drop) a table.
Delete the model file and the associated helper and controller files.
Then run
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
hobo g migration
\end_layout
\end_inset
.
You will be prompted to confirm that you wish to drop the table.
(If you neglect to delete the helper and controller file for this model
you will get an error.)
\end_layout
\begin_layout Standard
15.
\begin_inset Flex Strong
status collapsed
\begin_layout Plain Layout
Going back to earlier migrations.
\end_layout
\end_inset
Hobo does not provide this facility within
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
hobo g migration
\end_layout
\end_inset
.
You will need to use the
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
rake db:migrate VERSION = XXX
\end_layout
\end_inset
procedure.
You can roll back your tables but the rest of your changes will not be
synchronized so you will have to perform manual edits.
\end_layout
\begin_layout Section*
Tutorial 2 – Changing Field Names
\end_layout
\begin_layout Standard
We are going to continue from the previous tutorial and show you how to
do rename fields in a couple of different ways and improve your UI with
hints about what to enter in a particular field.
\end_layout
\begin_layout Subsubsection*
Topics
\end_layout
\begin_layout Itemize
Two ways of changing field names displayed
\end_layout
\begin_layout Itemize
Displaying data entry hints
\end_layout
\begin_layout Itemize
Changing field sizes: Hobo does not provide this facility now.
\end_layout
\begin_layout Subsubsection*
Tutorial Application:
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
my-first-app
\end_layout
\end_inset
\end_layout
\begin_layout Subsubsection*
Steps
\end_layout
\begin_layout Standard
1.
\begin_inset Flex Strong
status collapsed
\begin_layout Plain Layout
Rename a database column.
\end_layout
\end_inset
In Tutorial 1, we showed you how to make changes to your database by editing
the model file.
You can rename a field and database column in the same way.
We will try this with the
\begin_inset Flex Emph
status collapsed
\begin_layout Plain Layout
married
\end_layout
\end_inset
field.
Go to your
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
contacts.rb
\end_layout
\end_inset
file and rename
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
married
\end_layout
\end_inset
to
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
married_now
\end_layout
\end_inset
and run the
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
hobo g migration
\end_layout
\end_inset
.
\end_layout
\begin_layout Standard
\begin_inset Box Shadowbox
position "t"
hor_pos "c"
has_inner_box 1
inner_pos "t"
use_parbox 0
use_makebox 0
width "100col%"
special "none"
height "1in"
height_special "totalheight"
status open
\begin_layout Plain Layout
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
class Contact < ActiveRecord::Base
\end_layout
\end_inset
\end_layout
\begin_layout LyX-Code
hobo_model # Don't put anything above this
\end_layout
\begin_layout LyX-Code
\end_layout
\begin_layout LyX-Code
fields do
\end_layout
\begin_layout LyX-Code
name :string
\end_layout
\begin_layout LyX-Code
company :string
\end_layout
\begin_layout LyX-Code
address_1 :string
\end_layout
\begin_layout LyX-Code
#address_2 :string
\end_layout
\begin_layout LyX-Code
city :string
\end_layout
\begin_layout LyX-Code
state :string
\end_layout
\begin_layout LyX-Code
date_met :date
\end_layout
\begin_layout LyX-Code
#married :boolean
\end_layout
\begin_layout LyX-Code
married_now :Boolean
\end_layout
\begin_layout LyX-Code
age :integer
\end_layout
\begin_layout LyX-Code
notes :text
\end_layout
\begin_layout LyX-Code
timestamps
\end_layout
\begin_layout LyX-Code
end
\end_layout
\end_inset
\end_layout
\begin_layout LyX-Code
\begin_inset Box Boxed
position "t"
hor_pos "c"
has_inner_box 1
inner_pos "t"
use_parbox 0
use_makebox 0
width "100col%"
special "none"
height "1in"
height_special "totalheight"
status open
\begin_layout Plain Layout
\backslash
my-first-app> hobo g migration
\end_layout
\end_inset
\end_layout
\begin_layout Standard
Hobo should now respond:
\end_layout
\begin_layout Standard
\begin_inset Box Shadowbox
position "t"
hor_pos "c"
has_inner_box 1
inner_pos "t"
use_parbox 0
use_makebox 0
width "100col%"
special "none"
height "1in"
height_special "totalheight"
status open
\begin_layout LyX-Code
DROP, RENAME or KEEP?: column contacts.married
\end_layout
\begin_layout LyX-Code
Rename choices: married_now
\end_layout
\begin_layout LyX-Code
Enter either 'drop married' or one of the rename
\end_layout
\begin_layout LyX-Code
choices or press enter to keep:
\end_layout
\end_inset
\end_layout
\begin_layout Standard
Hobo is trying to confirm that what you really want to do is rename the
column and not drop it.
Enter
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
married_now
\end_layout
\end_inset
to rename.
Check your s
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
chema.db
\end_layout
\end_inset
file and you will see that the column has been renamed.
The KEEP option is a safety option in case you mistakenly renamed the column,
\end_layout
\begin_layout Standard
\begin_inset Box Shaded
position "t"
hor_pos "c"
has_inner_box 1
inner_pos "t"
use_parbox 0
use_makebox 0
width "100col%"
special "none"
height "1in"
height_special "totalheight"
status open
\begin_layout Description
Note: Do not use question marks (?) in field names.
You will get an error.
\end_layout
\end_inset
\end_layout
\begin_layout Standard
Refresh your browser and you will now see the field labeled ‘Married Now.’
\end_layout
\begin_layout Standard
2.
\begin_inset Flex Strong
status collapsed
\begin_layout Plain Layout
Changing field names.
\end_layout
\end_inset
There is no need to change the name of a field or column if all you wish
to do is to change the name of a label in the user interface.
Hobo provides this as part of its new Internationalization (
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
i18n
\end_layout
\end_inset
) module.
This
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
i18n
\end_layout
\end_inset
module is very versatile and can be used for providing field/model renaming
capabilities up to full multi-lingual support for your application.
The
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
i18n
\end_layout
\end_inset
module is comprised of
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
.yml
\end_layout
\end_inset
files that reside in the
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
config/locales
\end_layout
\end_inset
directory; if you look in the directory you will see these files:
\end_layout
\begin_layout Itemize
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
app.en.yml
\end_layout
\end_inset
(or
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
app.<locale>.yml
\end_layout
\end_inset
)
\end_layout
\begin_layout Itemize
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
hobo.en.yml
\end_layout
\end_inset
(or
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
hobo.<locale>.yml
\end_layout
\end_inset
)
\end_layout
\begin_layout Standard
For this tutorial we will assume that english (en) was specified as the
default locale.
The
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
hobo.en.yml
\end_layout
\end_inset
file contains all the strings that the hobo framework uses, while application
specific strings are stored in the
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
app.en.yml
\end_layout
\end_inset
file.
We will modify the
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
app.en.yml
\end_layout
\end_inset
file to rename a couple of fields in the Contact model.
When you open the
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
config/locales/app.en.yml
\end_layout
\end_inset
file, you will see:
\end_layout
\begin_layout Standard
\begin_inset Float figure
wide false
sideways false
status open
\begin_layout Plain Layout
\begin_inset Graphics
filename figures/figure71.png
width 90col%
\end_inset
\end_layout
\begin_layout Plain Layout
\begin_inset Caption
\begin_layout Plain Layout
Default config/locales/app.en.yml File
\end_layout
\end_inset
\end_layout
\begin_layout Plain Layout
\end_layout
\end_inset
\end_layout
\begin_layout Standard
The commented
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
yaml
\end_layout
\end_inset
code is very useful in understanding how to setup your locale file.
Our goal is to rename the
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
name
\end_layout
\end_inset
and
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
address_1
\end_layout
\end_inset
fields of the Contact model.
To do this, add the illustrated code to the file:
\end_layout
\begin_layout Standard
\begin_inset Float figure
wide false
sideways false
status open
\begin_layout Plain Layout
\begin_inset Graphics
filename figures/figure72.png
width 90col%
\end_inset
\end_layout
\begin_layout Plain Layout
\begin_inset Caption
\begin_layout Plain Layout
app.en.yml File with Fields Renamed
\end_layout
\end_inset
\end_layout
\begin_layout Plain Layout
\end_layout
\end_inset
\end_layout
\begin_layout Standard
As shown above, to declare new names for model fields in the
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
app.en.yml
\end_layout
\end_inset
file, the following pattern must be followed:
\end_layout
\begin_layout Standard
\begin_inset Box Shadowbox
position "t"
hor_pos "c"
has_inner_box 1
inner_pos "t"
use_parbox 0
use_makebox 0
width "100col%"
special "none"
height "1in"
height_special "totalheight"
status open
\begin_layout LyX-Code
activerecord:
\end_layout
\begin_layout LyX-Code
attributes:
\end_layout
\begin_layout LyX-Code
<model1>:
\end_layout
\begin_layout LyX-Code
<field1>: Label/Rename
\end_layout
\begin_layout LyX-Code
<field2>: Label/Rename
\end_layout
\begin_layout LyX-Code
\end_layout
\begin_layout LyX-Code
\end_layout
\begin_layout LyX-Code
<model2>:
\end_layout
\begin_layout LyX-Code
<field1>: Label/Rename
\end_layout
\begin_layout LyX-Code
<field2>: Label/Rename
\end_layout
\begin_layout LyX-Code
\end_layout
\end_inset
\end_layout
\begin_layout Standard
\begin_inset Box Shaded
position "t"
hor_pos "c"
has_inner_box 1
inner_pos "t"
use_parbox 0
use_makebox 0
width "100col%"
special "none"
height "1in"
height_special "totalheight"
status open
\begin_layout Description
Note: Indentation and spacing is very important when working with yml files;
the activerecord: line must start with 2 spaces.
\end_layout
\end_inset
\end_layout
\begin_layout Standard
Refresh your browser and you should see the fields relabeled with your choices
from above.
Notice that a migration is not necessary for any changes made using the
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
i18n
\end_layout
\end_inset
module.
\end_layout
\begin_layout Standard
\begin_inset Float figure
wide false
sideways false
status open
\begin_layout Plain Layout
\begin_inset Graphics
filename figures/figure73.png
width 90col%
\end_inset
\end_layout
\begin_layout Plain Layout
\begin_inset Caption
\begin_layout Plain Layout
View of fields relabeled using the Hobo i18n module
\end_layout
\end_inset
\end_layout
\begin_layout Plain Layout
\end_layout
\end_inset
\end_layout
\begin_layout Standard
3.
\begin_inset Flex Strong
status collapsed
\begin_layout Plain Layout
Using the
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
i18n
\end_layout
\end_inset
module to suggest field uses.
\end_layout
\end_inset
The application locale (
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
app.en.yml
\end_layout
\end_inset
) file also provides the facility to provide a suggestion below the field
on what to enter into it.
Edit your
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
app.en.yml
\end_layout
\end_inset
file to look like this.
\end_layout
\begin_layout Standard
\begin_inset Float figure
wide false
sideways false
status open
\begin_layout Plain Layout
\begin_inset Graphics
filename figures/figure74.png
width 90col%
\end_inset
\end_layout
\begin_layout Plain Layout
\begin_inset Caption
\begin_layout Plain Layout
Adding help text using the Hobo i18n
\begin_inset Quotes eld
\end_inset
attribute_help
\begin_inset Quotes erd
\end_inset
method
\end_layout
\end_inset
\end_layout
\begin_layout Plain Layout
\end_layout
\end_inset
\end_layout
\begin_layout Standard
Now refresh your browser and you will see hints on the field use in a small
font below:
\end_layout
\begin_layout Standard
\begin_inset Float figure
wide false
sideways false
status open
\begin_layout Plain Layout
\begin_inset Graphics
filename figures/figure75.png
width 90col%
\end_inset
\end_layout
\begin_layout Plain Layout
\begin_inset Caption
\begin_layout Plain Layout
Contact entry page with ViewHints enabled
\end_layout
\end_inset
\end_layout
\begin_layout Plain Layout
\end_layout
\end_inset
\end_layout
\begin_layout Standard
\begin_inset Box Shaded
position "t"
hor_pos "c"
has_inner_box 1
inner_pos "t"
use_parbox 0
use_makebox 0
width "100col%"
special "none"
height "1in"
height_special "totalheight"
status open
\begin_layout Description
Note: In the Intermediate tutorials you will also learn how to use yet another
way to manipulate the labels on a web page by using Hobo’s view markup
language called DRYML (Don’t Repeat Yourself Markup Language).
DRYML is used by the Rapid UI generator that creates much of Hobo’s magic.
\end_layout
\end_inset
\end_layout
\begin_layout Standard
4.
\begin_inset Flex Strong
status collapsed
\begin_layout Plain Layout
Changing field sizes.
\end_layout
\end_inset
As of the latest version of Hobo, the way to change the field length on
an input form is to add an entry to
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
application.css
\end_layout
\end_inset
that will override any other reference to the element you wish to modify.
\end_layout
\begin_layout Standard
Look for the relevant class definition used by Hobo’s “Rapid” UI generator:
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
rapid-ui.css
\end_layout
\end_inset
, located at:
\end_layout
\begin_layout Standard
\begin_inset Box Shadowbox
position "t"
hor_pos "c"
has_inner_box 1
inner_pos "t"
use_parbox 0
use_makebox 0
width "100col%"
special "none"
height "1in"
height_special "totalheight"
status open
\begin_layout LyX-Code
/public/hobothemes/clean/stylesheets/rapid-ui.css
\end_layout
\end_inset
\end_layout
\begin_layout Standard
\begin_inset Float figure
wide false
sideways false
status open
\begin_layout Plain Layout
\begin_inset Graphics
filename figures/figure76.png
width 90col%
\end_inset
\end_layout
\begin_layout Plain Layout
\begin_inset Caption
\begin_layout Plain Layout
CSS definitions for the input text fields
\end_layout
\end_inset
\end_layout
\begin_layout Plain Layout
\end_layout
\end_inset
\end_layout
\begin_layout Standard
\begin_inset Float figure
wide false
sideways false
status open
\begin_layout Plain Layout
\begin_inset Graphics
filename figures/figure77.png
width 90col%
\end_inset
\end_layout
\begin_layout Plain Layout
\begin_inset Caption
\begin_layout Plain Layout
Modified entry in "application.css" to shorten text prompts
\end_layout
\end_inset
\end_layout
\begin_layout Plain Layout
\end_layout
\end_inset
\end_layout
\begin_layout Section*
Tutorial 3 – Field Validation
\end_layout
\begin_layout Standard
You will be introduced to a couple of ways of validating data entry fields.
This is a capability that is derived from what are called Rails helper
methods.
There are a couple of enhancements Hobo has made for the most common need.
\end_layout
\begin_layout Subsubsection*
Topics
\end_layout
\begin_layout Itemize
Field validation using Hobo’s enhancements
\end_layout
\begin_layout Itemize
Field validation using Rails helper methods
\end_layout
\begin_layout Itemize
Validation on save, create and update processes
\end_layout
\begin_layout Subsubsection*
Tutorial Application:
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
my-first-app
\end_layout
\end_inset
\end_layout
\begin_layout Standard
1.
\begin_inset Flex Strong
status collapsed
\begin_layout Plain Layout
Make sure data is entered.
\end_layout
\end_inset
Open up the model
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
contact.rb
\end_layout
\end_inset
file.
Add the following code to the “name” field definition
\end_layout
\begin_layout Standard
\begin_inset Box Shadowbox
position "t"
hor_pos "c"
has_inner_box 1
inner_pos "t"
use_parbox 0
use_makebox 0
width "100col%"
special "none"
height "1in"
height_special "totalheight"
status open
\begin_layout LyX-Code
name :string, :required
\end_layout
\end_inset
\end_layout
\begin_layout Standard
This is the simplified version that Hobo provides.
To do this in the “normal” rails way, you would need to add this line after
the “fields/do” block:
\end_layout
\begin_layout Standard
\begin_inset Box Shadowbox
position "t"
hor_pos "c"
has_inner_box 1
inner_pos "t"
use_parbox 0
use_makebox 0
width "100col%"
special "none"
height "1in"
height_special "totalheight"
status open
\begin_layout LyX-Code
validates_presence_of :name
\end_layout
\end_inset
\end_layout
\begin_layout Standard
(The difference in the two is a matter of taste, but the former seems “DRYer”
to us.)
\end_layout
\begin_layout Standard
By default Hobo will provide a message if a user fails to enter data.
Try it out by trying to create a contact record with no data in it.
Click the Contacts tab and then
\begin_inset Flex Emph
status collapsed
\begin_layout Plain Layout
New Contact
\end_layout
\end_inset
.
\end_layout
\begin_layout Standard
Without entering anything in the form, click
\begin_inset Flex Emph
status collapsed
\begin_layout Plain Layout
Create Contact
\end_layout
\end_inset
.
\end_layout
\begin_layout Standard
\begin_inset Float figure
wide false
sideways false
status open
\begin_layout Plain Layout
\begin_inset Graphics
filename figures/figure78.png
width 90col%
\end_inset
\end_layout
\begin_layout Plain Layout
\begin_inset Caption
\begin_layout Plain Layout
Page view of validation presence of name
\end_layout
\end_inset
\end_layout
\begin_layout Plain Layout
\end_layout
\end_inset
\end_layout
\begin_layout Standard
2.
\begin_inset Flex Strong
status collapsed
\begin_layout Plain Layout
Validate multiple fields.
\end_layout
\end_inset
In order to validate multiple fields, add the “:required” label to another
field:
\end_layout
\begin_layout Standard
\begin_inset Box Shadowbox
position "t"
hor_pos "c"
has_inner_box 1
inner_pos "t"
use_parbox 0
use_makebox 0
width "100col%"
special "none"
height "1in"
height_special "totalheight"
status open
\begin_layout LyX-Code
address_1 :string, :required
\end_layout
\end_inset
\end_layout
\begin_layout Standard
Click the Contacts tab and then
\begin_inset Flex Emph
status collapsed
\begin_layout Plain Layout
New Contact
\end_layout
\end_inset
.
Without entering anything in the form, click
\begin_inset Flex Emph
status collapsed
\begin_layout Plain Layout
Create Contact
\end_layout
\end_inset
.
\end_layout
\begin_layout Standard
\begin_inset Float figure
wide false
sideways false
status open
\begin_layout Plain Layout
\begin_inset Graphics
filename figures/figure79.png
width 90col%
\end_inset
\end_layout
\begin_layout Plain Layout
\begin_inset Caption
\begin_layout Plain Layout
Page view of double validation error
\end_layout
\end_inset
\end_layout
\begin_layout Plain Layout
\end_layout
\end_inset
\end_layout
\begin_layout Standard
Notice the “declarative” nature of this validation.
All you need to do is use the label “:required” for the name and address_1
fields and Hobo takes care of all of the logic associated with validation
and delivering error messages.
\end_layout
\begin_layout Standard
Now let’s try some other validations.
\end_layout
\begin_layout Standard
3.
\begin_inset Flex Strong
status collapsed
\begin_layout Plain Layout
Make sure the integer field contains a number.
\end_layout
\end_inset
Add this validation to the “age” field after the “fields do/end” block:
\end_layout
\begin_layout Standard
\begin_inset Box Shadowbox
position "t"
hor_pos "c"
has_inner_box 1
inner_pos "t"
use_parbox 0
use_makebox 0
width "100col%"
special "none"
height "1in"
height_special "totalheight"
status open
\begin_layout LyX-Code
validates_numericality_of :age
\end_layout
\end_inset
\end_layout
\begin_layout Standard
\begin_inset Float figure
wide false
sideways false
status open
\begin_layout Plain Layout
\begin_inset Graphics
filename figures/figure80.png
width 90col%
\end_inset
\end_layout
\begin_layout Plain Layout
\begin_inset Caption
\begin_layout Plain Layout
Adding
\begin_inset Quotes eld
\end_inset
validate_numericality_of
\begin_inset Quotes erd
\end_inset
validation
\end_layout
\end_inset
\end_layout
\begin_layout Plain Layout
\end_layout
\end_inset
\end_layout
\begin_layout Standard
Now try this out by entering the text “old” in the age field.
(Also put something in the
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
name
\end_layout
\end_inset
and
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
address_1
\end_layout
\end_inset
fields so you won’t trip the validations we put into place earlier in the
tutorial.)
\end_layout
\begin_layout Standard
\begin_inset Float figure
wide false
sideways false
status open
\begin_layout Plain Layout
\begin_inset Graphics
filename figures/figure81.png
width 90col%
\end_inset
\end_layout
\begin_layout Plain Layout
\begin_inset Caption
\begin_layout Plain Layout
Page view of triggering the "validates_numericality_of" error
\end_layout
\end_inset
\end_layout
\begin_layout Plain Layout
\end_layout
\end_inset
\end_layout
\begin_layout Standard
\begin_inset Box Shaded
position "t"
hor_pos "c"
has_inner_box 1
inner_pos "t"
use_parbox 0
use_makebox 0
width "100col%"
special "none"
height "1in"
height_special "totalheight"
status open
\begin_layout Description
Note: When you cause a validation error for integer, Hobo/Rails replaces
what you entered with a zero (0).
If the validation rule was not there, the text will be replaced by a zero,
but the validation error will not be displayed.
\end_layout
\end_inset
\end_layout
\begin_layout Standard
4.
\begin_inset Flex Strong
status collapsed
\begin_layout Plain Layout
Prevent the entry of duplicates.
\end_layout
\end_inset
Use the following code to prevent a user from entering code that duplicates
an existing record with a column value that is the same as the new record.
\end_layout
\begin_layout Standard
\begin_inset Box Shadowbox
position "t"
hor_pos "c"
has_inner_box 1
inner_pos "t"
use_parbox 0
use_makebox 0
width "100col%"
special "none"
height "1in"
height_special "totalheight"
status open
\begin_layout LyX-Code
name :string, :required, :unique
\end_layout
\end_inset
\end_layout
\begin_layout Standard
\begin_inset Float figure
wide false
sideways false
status open
\begin_layout Plain Layout
\begin_inset Graphics
filename figures/figure82.png
width 90col%
\end_inset
\end_layout
\begin_layout Plain Layout
\begin_inset Caption
\begin_layout Plain Layout
Page view of uniqueness validation error
\end_layout
\end_inset
\end_layout
\begin_layout Plain Layout
\end_layout
\end_inset
\end_layout
\begin_layout Standard
\begin_inset Box Shaded
position "t"
hor_pos "c"
has_inner_box 1
inner_pos "t"
use_parbox 0
use_makebox 0
width "100col%"
special "none"
height "1in"
height_special "totalheight"
status open
\begin_layout Description
Note: This particular validation will only verify that there is no existing
record with the same field value at the time of validation.
In a multi-user application, there is still a chance that records could
be entered nearly at the same time resulting in a duplicate entry.
The most reliable way to enforce uniqueness is with a database-level constraint.
\end_layout
\end_inset
\end_layout
\begin_layout Standard
5.
\begin_inset Flex Strong
status collapsed
\begin_layout Plain Layout
Including and excluding values.
\end_layout
\end_inset
Now suppose we wish to exclude people who have an age between 0 and 17,
and include people under 65 years of age.
Try the following code after the “fields do/end” block:
\end_layout
\begin_layout Standard
\begin_inset Box Shadowbox
position "t"
hor_pos "c"
has_inner_box 1
inner_pos "t"
use_parbox 0
use_makebox 0
width "100col%"
special "none"
height "1in"
height_special "totalheight"
status open
\begin_layout LyX-Code
validates_inclusion_of :age, :in => 18..65, :message => "Must be between 18
and 65"
\end_layout
\end_inset
\end_layout
\begin_layout Standard
\begin_inset Float figure
wide false
sideways false
status open
\begin_layout Plain Layout
\begin_inset Graphics
filename figures/figure83.png
width 90col%
\end_inset
\end_layout
\begin_layout Plain Layout
\begin_inset Caption
\begin_layout Plain Layout
Page view of triggering a range validation error
\end_layout
\end_inset
\end_layout
\begin_layout Plain Layout
\end_layout
\end_inset
\end_layout
\begin_layout Standard
6.
\begin_inset Flex Strong
status collapsed
\begin_layout Plain Layout
Validate length of entry.
\end_layout
\end_inset
Suppose you wish to check the length of a string entry.
You can specify a length range in the following way.
\end_layout
\begin_layout Standard
\begin_inset Box Shadowbox
position "t"
hor_pos "c"
has_inner_box 1
inner_pos "t"
use_parbox 0
use_makebox 0
width "100col%"
special "none"
height "1in"
height_special "totalheight"
status open
\begin_layout LyX-Code
validates_length_of :name, :within => 2..20,
\end_layout
\begin_layout LyX-Code
:too_long => "pick a shorter name",
\end_layout
\begin_layout LyX-Code
:too_short => "pick a longer name"
\end_layout
\end_inset
\end_layout
\begin_layout Standard
Try to enter a one-character name.
You will get the following response:
\end_layout
\begin_layout Standard
\begin_inset Float figure
wide false
sideways false
status open
\begin_layout Plain Layout
\begin_inset Graphics
filename figures/figure84.png
width 90col%
\end_inset
\end_layout
\begin_layout Plain Layout
\begin_inset Caption
\begin_layout Plain Layout
Page view of validation of text length error
\end_layout
\end_inset
\end_layout
\begin_layout Plain Layout
\end_layout
\end_inset
\end_layout
\begin_layout Standard
7.
\begin_inset Flex Strong
status collapsed
\begin_layout Plain Layout
Validate acceptance.
\end_layout
\end_inset
If you wish to get the user to accept a contract, for example, you can
use the following validation code.
Assume you have a Boolean variable named
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
contract_agree
\end_layout
\end_inset
, which would show up in the UI as a checkbox.
\end_layout
\begin_layout Standard
\begin_inset Box Shadowbox
position "t"
hor_pos "c"
has_inner_box 1
inner_pos "t"
use_parbox 0
use_makebox 0
width "100col%"
special "none"
height "1in"
height_special "totalheight"
status open
\begin_layout LyX-Code
validates_acceptance_of :contract_agree, :accept => true
\end_layout
\end_inset
\end_layout
\begin_layout Standard
Hobo will generate an error if the
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
contract_agree
\end_layout
\end_inset
check box is not checked setting the value to 1.
\end_layout
\begin_layout Standard
\begin_inset Float figure
wide false
sideways false
status open
\begin_layout Plain Layout
\begin_inset Graphics
filename figures/figure85.png
width 90col%
\end_inset
\end_layout
\begin_layout Plain Layout
\begin_inset Caption
\begin_layout Plain Layout
Page view of
\begin_inset Quotes eld
\end_inset
validates_acceptance_of
\begin_inset Quotes erd
\end_inset
error
\end_layout
\end_inset
\end_layout
\begin_layout Plain Layout
\end_layout
\end_inset
\end_layout
\begin_layout Standard
8.
\begin_inset Flex Strong
status collapsed
\begin_layout Plain Layout
Summary.
\end_layout
\end_inset
Here is the list of validations we accumulated during this tutorial:
\end_layout
\begin_layout Standard
\begin_inset Box Shadowbox
position "t"
hor_pos "c"
has_inner_box 1
inner_pos "t"
use_parbox 0
use_makebox 0
width "100col%"
special "none"
height "1in"
height_special "totalheight"
status open
\begin_layout LyX-Code
address_1 :string, :required
\end_layout
\begin_layout LyX-Code
name :string, :required, :unique
\end_layout
\end_inset
\end_layout
\begin_layout Standard
\begin_inset Box Shadowbox
position "t"
hor_pos "c"
has_inner_box 1
inner_pos "t"
use_parbox 0
use_makebox 0
width "100col%"
special "none"
height "1in"
height_special "totalheight"
status open
\begin_layout LyX-Code
validates_numericality_of :age
\end_layout
\begin_layout LyX-Code
\end_layout
\begin_layout LyX-Code
validates_acceptance_of :contract_agree,
\end_layout
\begin_layout LyX-Code
:accept => true
\end_layout
\begin_layout LyX-Code
\end_layout
\begin_layout LyX-Code
validates_length_of :name, :within => 2..20,
\end_layout
\begin_layout LyX-Code
:too_long => "pick a shorter name",
\end_layout
\begin_layout LyX-Code
:too_short => "pick a longer name"
\end_layout
\begin_layout LyX-Code
\end_layout
\begin_layout LyX-Code
validates_inclusion_of :age, :in => 18..65,
\end_layout
\begin_layout LyX-Code
:message => "Must be between 18 and 65"
\end_layout
\end_inset
\end_layout
\begin_layout Standard
There are several other very useful validation functions provided by Rails,
and the ones that we have shown you above have many other options.
These functions can provide very sophisticated business rule execution.
\end_layout
\begin_layout Standard
For example, the following is a sample of the list of options for the
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
validates_length_of
\end_layout
\end_inset
and
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
validates_size_of
\end_layout
\end_inset
(synonym) declarative expressions:
\end_layout
\begin_layout Itemize
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
:minimum
\end_layout
\end_inset
- The minimum size of the attribute.
\end_layout
\begin_layout Itemize
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
:maximum
\end_layout
\end_inset
- The maximum size of the attribute.
\end_layout
\begin_layout Itemize
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
:is
\end_layout
\end_inset
- The exact size of the attribute.
\end_layout
\begin_layout Itemize
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
:within
\end_layout
\end_inset
- A range specifying the minimum and maximum size of the attribute.
\end_layout
\begin_layout Itemize
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
:in
\end_layout
\end_inset
- A synonym(or alias) for
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
:within
\end_layout
\end_inset
.
\end_layout
\begin_layout Itemize
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
:allow_nil
\end_layout
\end_inset
- Attribute may be nil; skip validation.
\end_layout
\begin_layout Itemize
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
:allow_blank
\end_layout
\end_inset
- Attribute may be blank; skip validation.
\end_layout
\begin_layout Itemize
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
:too_long
\end_layout
\end_inset
- The error message if the attribute goes over the maximum (default is:
"is too long (maximum is {{count}} characters)").
\end_layout
\begin_layout Itemize
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
:too_short
\end_layout
\end_inset
- The error message if the attribute goes under the minimum (default is:
"is too short (min is {{count}} characters)").
\end_layout
\begin_layout Itemize
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
:wrong_length
\end_layout
\end_inset
- The error message if using the :is method and the attribute is the wrong
size (default is: "is the wrong length (should be {{count}} characters)").
\end_layout
\begin_layout Itemize
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
:message
\end_layout
\end_inset
- The error message to use for a
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
:minimum
\end_layout
\end_inset
,
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
:maximum
\end_layout
\end_inset
, or
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
:is
\end_layout
\end_inset
violation.
An alias of the appropriate
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
too_long/too_short/wrong_length
\end_layout
\end_inset
message.
\end_layout
\begin_layout Itemize
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
:on
\end_layout
\end_inset
- Specifies when this validation is active (default is
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
:save
\end_layout
\end_inset
, other options
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
:create
\end_layout
\end_inset
,
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
:update
\end_layout
\end_inset
).
\end_layout
\begin_layout Itemize
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
:if
\end_layout
\end_inset
- Specifies a method, procedure, or string to call to determine if the
validation should occur:
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
:if => :allow_validation
\end_layout
\end_inset
\end_layout
\begin_deeper
\begin_layout Standard
\begin_inset Flex Emph
status collapsed
\begin_layout Plain Layout
The method, procedure, or string should return or evaluate to a true or
false value.
\end_layout
\end_inset
\end_layout
\end_deeper
\begin_layout Itemize
:unless - Specifies a method, procedure or string to call to determine if
the validation should not occur:
\end_layout
\begin_deeper
\begin_layout Standard
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
:unless => :skip_validation
\end_layout
\end_inset
\end_layout
\begin_layout Standard
\begin_inset Flex Emph
status collapsed
\begin_layout Plain Layout
The method, procedure, or string should return or evaluate to a true or
false value.
\end_layout
\end_inset
\end_layout
\end_deeper
\begin_layout Standard
We encourage you to read about validation helpers (what Rails calls functions)
in the many good Ruby on Rails references.
The following is a useful on-line reference:
\end_layout
\begin_layout Standard
\begin_inset CommandInset href
LatexCommand href
target "http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html "
\end_inset
\end_layout
\begin_layout Section
Tutorial 4 – Permissions
\end_layout
\begin_layout Standard
In this tutorial you will learn some elementary aspects of Hobo’s permission
system by changing what the admin user and users can do.
Specifically, you will determine whether a user is permitted to view, create,
edit or delete records in the database.
\end_layout
\begin_layout Subsubsection
Topics
\end_layout
\begin_layout Itemize
Experiment with altering user permissions.
\end_layout
\begin_layout Itemize
Naming conventions for database tables, models, controllers and views.
\end_layout
\begin_layout Subsubsection
Tutorial Application:
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
one_table
\end_layout
\end_inset
\end_layout
\begin_layout Subsubsection
Steps
\end_layout
\begin_layout Standard
1.
\begin_inset Flex Strong
status collapsed
\begin_layout Plain Layout
Create the Hobo application.
\end_layout
\end_inset
Create the
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
one_table
\end_layout
\end_inset
Hobo application by issuing the following command at the command prompt.
By adding the
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
--setup
\end_layout
\end_inset
command line parameter the application will bypass the setup wizard and
create the application with default settings, Then change directory to
the subdirectory
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
one_table:
\end_layout
\end_inset
\end_layout
\begin_layout Standard
\begin_inset Box Shadowbox
position "t"
hor_pos "c"
has_inner_box 1
inner_pos "t"
use_parbox 0
use_makebox 0
width "100col%"
special "none"
height "1in"
height_special "totalheight"
status open
\begin_layout LyX-Code
\backslash
tutorials> hobo new one_table --setup
\end_layout
\begin_layout LyX-Code
\backslash
tutorials> cd one_table
\end_layout
\begin_layout LyX-Code
\backslash
one_table>
\end_layout
\end_inset
\end_layout
\begin_layout Standard
Recall from Tutorial 1 that this sets up the Hobo directory tree and the
user model and controller.
\end_layout
\begin_layout Standard
\begin_inset Box Shaded
position "t"
hor_pos "c"
has_inner_box 1
inner_pos "t"
use_parbox 0
use_makebox 0
width "100col%"
special "none"
height "1in"
height_special "totalheight"
status open
\begin_layout Description
Note: Look at the file
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
\backslash
one_table
\backslash
app
\backslash
models
\backslash
user.rb
\end_layout
\end_inset
and at the database schema file
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
\backslash
one_table
\backslash
db
\backslash
schema.rb
\end_layout
\end_inset
.
There are more fields in the users table than in the user model.
This is because Hobo creates several user model fields for you automatically.
This will not be the case for models you create.
\end_layout
\end_inset
\end_layout
\begin_layout Standard
3.
\begin_inset Flex Strong
status collapsed
\begin_layout Plain Layout
Start the web server.
\end_layout
\end_inset
Open a new command prompt and navigate to the
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
\backslash
tutorials
\backslash
one_table
\end_layout
\end_inset
directory.
Fire up your web server by issuing the following command.
\end_layout
\begin_layout Standard
\begin_inset Box Shadowbox
position "t"
hor_pos "c"