Input for the assignment for Ruby on Rails:

barristerbedroomΛογισμικό & κατασκευή λογ/κού

28 Οκτ 2013 (πριν από 4 χρόνια και 15 μέρες)

61 εμφανίσεις

Input for the assignment for Ruby on Rails:

In preparation for doing the assignment, the following document may be
of significant help. I offer this as additional input based on the degree of
difficulty encountered in the longer assignments of this
course.

Here are the instructions for installing the sample Rails application, the
VRLS Administrative Interface
, described in
Web Application Architecture: Principles, Protocols, and Practices
. You will need
the book to get the full tutorial
benefit of the application.

These instructions assume that you have already downloaded and installed the MySQL database system and set it
up for use with the book's main application,
Virtual Reality Listing Services
. If y
ou have not,
this page

provides
useful information.

Instructions for installing this application in Linux, Unix, and Mac OS X environments can be found
here
.

NOTE:

Because Rails seems to change significantly from release to release, making it difficult to pin down a stable
set of instructions for installing it, we encourage you to visit the
Rails wiki

for up
-
to
-
date instructions and other related
information. In particular, the page on
Installing Ruby on Rails on Windows

is a usefu
l resource.

1.

First, download and install Ruby. The simplest approach is to use the
One
-
Click Ruby Installer for
Windows

(
http://rubyinstaller.rubyforge.org/wiki/wiki.pl

or
http://rubyforge.org/frs/?group_id=167
), which includes both the Ruby language
interpreter and the RubyGems package manager, but will require additional steps to install Rails and other
assoc
iated packages. (Make sure you check the box labeled
"Enable RubyGems"

when running the
installer.)

2.

Make sure the Ruby executables were added to your command "path" settings during installation by typing
path

at a DOS command prompt. If the string present
ed by this command includes
"C:
\
Ruby
\
bin"
,
you are fine. If not, add this directory to the command path by entering the following:

3.

C:
\
Users
\
yourname>
PATH=C:
\
Ruby
\
bin;%PATH%

To add
C:
\
Ruby
\
bin

to your command path permanently:

o

Open the
System

control panel.

o

Click on the
Advanced

tab.

o

Click on the
Environment Variables

button.

o

Select the line in the
System Variables

window labelled
Path

and click the
Edit

button.

o

Add the following at the end of the text in the
Variable Value

box:



;C:
\
Ru
by
\
bin

o

Click "OK" to dismiss this editing dialog.

o

Then click "OK" again to dismiss the
Environment Variables

window.

o

Finally, click "OK" one more time to confirm the
System Properties

changes.

4.

Make sure all the Ruby components are up to date, by entering the following command from a DOS
Command Prompt:

5.

C:
\
Users
\
yourname>
gem update
--
system

6.

Updating RubyGems

7.

Updating rubygems
-
update

8.

Successfully installed rubygems
-
update
-
1.3.3

9.

Updating RubyGems

to 1.3.3

10.

Installing RubyGems 1.3.3

11.

Installing RubyGems

12.


13.


...

14.


15.

RubyGems installed the following executables:

16.


c:/ruby/bin/gem

If you see a line at the end of the output that looks something like


'" update
--
system' is not recognized as an
internal or external
command, operable program or batch file.



it can be ignored.


Also, if you see this error when you run
gem
:



No such file or directory
-

H:/



execute the following command before attempting to run it again:



C:
\
Users
\
yourn
ame>
set HOMEDRIVE=C:



(A drive letter other than
"H"

may be displayed in the error message.) This is caused by a bug in
Ruby Installer version 1.8.6
-
27 and can also be circumvented by using the more stable 1.8.6
-
26
release.

17.

Then install Rails using the f
ollowing command:

18.

C:
\
Users
\
yourname>
gem install rails
--
include
-
dependencies
--
no
-
ri
--
no
-
rdoc

19.

INFO: `gem install
-
y` is now default and will be removed

20.

INFO: use
--
ignore
-
dependencies to install only the gems you list

21.

Successfully installed rake
-
0.8.7

22.

Successfully installed activesupport
-
2.3.2

23.

Successfully installed activerecord
-
2.3.2

24.

Successfully installed actionpack
-
2.3.2

25.

Successfully installed actionmailer
-
2.3.2

26.

Successfully installed activeresource
-
2.3.2

27.

Successfully installed rails
-
2.3.2

28.

7 gems in
stalled

29.

Rails is no longer distributed with the MySQL database adapter.

The process of downloading and
installing the MySQL adapter differs depending on your operating system environment. For environment
-
specific details, go to the
database support

page in the
Rails wiki

and follow the instructions there.

What has worked for us in a Windows environment is a two
-
step process.

1.

Execute the
gem

command to install the MySQL gem:


C:
\
Users
\
yourname>
gem install mysql
--
no
-
ri
--
no
-
rdoc


2.

Copy the 5.0 version of MySQL's
libmySQL.dll

file into the
C:
\
Ruby
\
bin

directory. This file
can be downloaded from
here
.


The second step is necessary because Ruby's MySQL gem
does not work properly

with the version of this
DLL that comes with MySQL 5.1.

30.

Once you have installed and updated Ruby, Rails, and the MySQL adapter, you are ready to begin building
the application. Create a directory that will contain your Rails applications and "cd" into that directory. Then
run the

rails

command to build the application project, and "cd" into the project directory.

31.

C:
\
Users
\
yourname>
mkdir rails_apps

32.

C:
\
Users
\
yourname>
cd rails_apps

33.

C:
\
Users
\
yourname
\
rails_apps>
rails
-
d mysql vrlsadmin

34.


create

35.


create app/controllers

36.


create app/helpers

37.


create app/models

38.


create app/views/layouts

39.


create config/environments

40.


...

41.

C:
\
Users
\
yourname
\
rails_apps>
cd vrlsadmin

42.

C:
\
Users
\
yourname
\
rails_apps
\
vrlsadmin>

43.

Edit the database configuration file (
config/database.yml
)
-

make sure the "mysql" adapter is
enabled and provide appropriate credentials.

44.

development:

45.


adapter: mysql

46.


encoding: utf8

47.


reconnect: false

48.


database: vrlsadmin_development

49.


pool: 5

50.


username: root

51.


password:
your
-
mysql
-
root
-
password

52.


host: localhost

53.


54.

test:

55.


adapter: mysql

56.


encoding: utf8

57.


reconnect: false

58.


database: vrlsadmin_test

59.


pool: 5

60.


username: root

61.


password:
your
-
mysql
-
root
-
password

62.


host: localhost

63.


64.

production:

65.


adapter: mysql

66.


encoding: utf8

67.


reconnect: false

68.


database: vrlsadmin

69.


pool: 5

70.


username: root

71.


password:
your
-
mysql
-
root
-
password

72.


host: localhost

73.

Set up your MySQL database server with a
vrlsadmin_development

database. Create and populate
the tables by running scripts provided w
ith the original VRLS application. (Alternatively, you can use these
links to download the
dbschema.sql

and
populate.sql

files.)

74.

C:
\
Users
\
yourname
\
rails_apps
\
vrlsadmin>
mysql
-
u root
-
p

75.

Enter password:
********

76.

Welcome to the MySQL monitor.

77.

Commands end with ; or
\
g.

78.

Your MySQL connection id is 13

79.

Server version: 5.0.37

80.

MySQL Community Server (GPL)

81.

Type
'help;' or '
\
h' for help.

82.

Type '
\
c' to clear the buffer.

83.

mysql>
create database vrlsadmin_development
;

84.

Query OK, 1 row affected (0.00 sec)

85.

mysql>
use vrlsadmin_development
;

86.

Database changed

87.

mysql>
source dbschema.sql

88.


...

89.

mysql>
source populate.sql

90.


...

91.

Run the
"script/generate scaffold"

commands to build model/view/controller scaffolding for
objects.

NOTE TO WINDOWS USERS:


In Windows,
"script/generate ..."

commands must be entered as
"
ruby
script
\
generate

..."
.

C:
\
Users
\
yourname
\
rails_apps
\
vrlsadmin>
ruby script
\
generate
scaffold Listing listing_id:integer listing_title:string
listing_desc:text listing_type_code:integer
listing_region:integer listing_offer_type_code:integer
listing_num_bedrooms:integer listing_nu
m_bathrooms:integer
listing_monthly_payment:integer listing_purchase_price:integer
referring_partner_id:integer listing_status_code:integer
listing_status_eff_date:date date_entered:date
date_last_modified:date


...


C:
\
Users
\
yourname
\
rails_apps
\
vrl
sadmin>
ruby script
\
generate
scaffold Customer cust_id:integer cust_login:string
cust_password_hash:string cust_first_name:string
cust_middle_name:string cust_last_name:string
cust_address1:string cust_address2:string cust_city:string
cust_state_province_c
ode:string cust_postal_code:string
cust_country_code:string cust_email_address:string
cust_phone:string cust_level:integer referring_partner_id:integer
cust_last_visited:date date_entered:date date_last_modified:date


...


C:
\
Users
\
yourname
\
rails_app
s
\
vrlsadmin>
ruby script
\
generate
scaffold Partner partner_id:integer partner_name:string
partner_desc:string partner_contact_name:string
partner_address1:string partner_address2:string
partner_state_province:string partner_postal_code:string
partner_count
ry_code:string partner_contact_email:string
partner_contact_phone:string partner_code:string
partner_prefix:string


...


C:
\
Users
\
yourname
\
rails_apps
\
vrlsadmin>
ruby script
\
generate
scaffold ListingImage listing_id:integer
listing_image_name:string l
isting_image_desc:string
listing_image_url:string listing_thumb_url:string
is_primary_image:boolean


...

NOTE:

The commands listed above can be downloaded as a single
.bat

file for execution on
your desktop by right
-
clicking on
this link

and selecting the appropriate option for file download in
your browser. For convenience, you will probably want to place the file in the directory your DOS
command

shell window is currently pointing to.

92.

Run
"script/generate model"

commands to build model/view/controller scaffolding for auxiliary
objects.

93.

C:
\
Users
\
yourname
\
rails_apps
\
vrlsadmin>
ruby script
\
generate model
ListingType

94.


...

95.


96.

C:
\
Users
\
yourname
\
ra
ils_apps
\
vrlsadmin>
ruby script
\
generate model
ListingOfferType

97.


...

98.


99.

C:
\
Users
\
yourname
\
rails_apps
\
vrlsadmin>
ruby script
\
generate model
ListingStatus

100.


...

NOTE:

The commands listed above can be downloaded as a single
.bat

file for execution on your desktop
by right
-
clicking on
this link

and selecting the appropriate option for file download in your browser. For
convenience, you will probably
want to place the file in the directory your DOS command shell window is
currently pointing to.

101.

Edit model definition files (
app/models/*.rb
) to point to correct table names and primary key columns.

app/models/listing.rb

class Listing < ActiveRecord
::Base


set_table_name "vrls_listings"


set_primary_key "listing_id"

end

app/models/customer.rb

class Customer < ActiveRecord::Base


set_table_name "vrls_customer_profile_data"


set_primary_key "cust_id"


belongs_to :partner, :foreign_key =>

"referring_partner_id"

end

app/models/partner.rb

class Partner < ActiveRecord::Base


set_table_name "vrls_partners"


set_primary_key "partner_id"


has_many :listing


has_many :customer


has_one :listing_image

end

app/models/listing_image.rb

class ListingImage < ActiveRecord::Base


set_table_name "vrls_listing_images"


set_primary_key "listing_id"


belongs_to :listing

end

app/models/listing_status.rb

class ListingStatus < ActiveRecord::Base


set_table_name "vrls_xref_listing_statu
s_code"


set_primary_key "listing_status_code"

end

app/models/listing_type.rb

class ListingType < ActiveRecord::Base


set_table_name "vrls_xref_listing_type"


set_primary_key "listing_type_code"

end

app/models/listing_offer_type.rb

class
ListingOfferType < ActiveRecord::Base


set_table_name "vrls_xref_listing_offer_type"


set_primary_key "listing_offer_type_code"

end

102.

Start server

103.

C:
\
Users
\
yourname
\
rails_apps
\
vrlsadmin>
ruby script
\
server

104.

Go to
http://localhost:3000/listings

in your Web browser. Try the "show", "edit", and "new"
pages as well.

105.

Make the edits described in Section 11.3.5 of the book to watch the application "evolve". Revisit

the various
pages to see how they've changed.

106.

Once you are satisfied with the state of the application, you can run it in production mode, against the
already established database used by the main VRLS application. Before doing this, edit the
config/data
base.yml

file to change the production database name to "vrls" (which was the name
given to the database used by the main application).

107.

development:

108.


adapter: mysql

109.


encoding: utf8

110.


reconnect: false

111.


database: vrlsadmin_development

112.


pool: 5

113.


username: root

114.


password:
your
-
mysql
-
root
-
password

115.


host: localhost

116.


117.

# Warning: The database defined as 'test' will be erased and

118.

# re
-
generated from your development database when you run 'rake'.

119.

# Do not set this db to the same as development or prod
uction.

120.

test:

121.


adapter: mysql

122.


reconnect: false

123.


database: vrlsadmin_test

124.


pool: 5

125.


username: root

126.


password:
your
-
mysql
-
root
-
password

127.


host: localhost

128.


129.

production:

130.


adapter: mysql

131.


reconnect: false

132.


database:
vrls

133.


pool: 5

134.


username: root

135.


password:
your
-
mysql
-
root
-
password

136.


host: localhost

137.

Then start the server in production mode. With the application pointing to the production database, any
changes made via the main application (e.g., new user signups and profile updates) will be reflec
ted in the
admin interface.

138.

C:
\
Users
\
yourname
\
rails_apps
\
vrlsadmin>
ruby script
\
server
-
e
production



Instructions for Downloading and Installing the Sample Application

These instructions are geared for installation in a Windows environment. Instructions for UNIX, Linux, or Macintosh
systems can be found
here
.

1.

Download the distribution package for
this application.

Unzip it into a directory of your choosing.

For purposes of this document, we will assume that you are on a Windows platform and have already
downloaded this package and extracted its contents under the
C:
\
myprojects

directory. If not, t
he link
for downloading is found below.

[
download the VRLS application distribution package

]

2.

Install the Java Development Kit (JDK) on your system.

This is needed both to com
pile the application
and to support the Web application server's servlet and JSP engines.

We recommend JDK version 1.5 or version 1.6 for this application. Both versions are available on Sun's
Web site.

[
download JDK 1.5

]


[
download JDK 1.6

]

3.

Install and configure Apache's Ant tool which is used to build the application.


We recommend using Ant 1.4.1 o
r better. Also, on systems running Windows, it is recommended that Ant
should be installed into a directory with no spaces in its name. (e.g.,
C:
\
ant
, not
C:
\
program
files
\
ant
)

[
download

Ant

]

4.

Install and configure your Web application server software.

You must use an application server (or
"Web container") that includes or supports the usage of a servlet engine (Java Servlet API 2.4 or better) and
Java Server Pages (JSP 2.0 or better). I
t should also support JNDI lookup of configured JDBC DataSources.

If you do not already have a J2EE Web container installed, we recommend Apache Tomcat. It is open
source, freely available, and supports all the functionality required by the application. E
ither version 5.5 or
version 6 is recommended.


Remember that Tomcat defaults to using port 8080, so either include the port number in your localhost
URLs (e.g.,
http://localhost:8080
) or change Tomcat's configuration to use port 80 for HTTP
requests. (Re
ad the Tomcat documentation for instructions on how to do this.)

[
download Tomcat 5.5

]


[
download Tomcat 6

]

5.

Install and configure your database management system (DBMS).

The data model for this application is
persisted in a relational database, thus a working database is required. You will also need a JDBC driver
appropriate for the DBMS that you choose.

If you do not already have a DBMS installed, we recommend MySQL. Like Apache Tomcat, it is open
source, freely available, and supports the functionality required by the application. In fact, the application's
data access services were written under the ass
umption that a simple DBMS like MySQL (which does not
support triggers or stored procedures or automated referential integrity) would be used. Version 5.0 is
recommended. The JDBC driver for MySQL is included in the download for the DBMS itself, but it is
also
accessible separately if desired.

[
download MySQL 5.0

]


[
download MySQL JDBC driver

]

6.

Execute the SQL queries that create and populate the database tables used by the application.

This
process varies depending on which DBMS you are using, so consult the documentation associated with your
DBMS software for appropriate instructions. Assuming
here that you installed MySQL into the directory
named
C:
\
mysql
\
, enter the following commands to execute these scripts.

7.

C:
\
>
cd
\
mysql
\
bin

8.

C:
\
mysql
\
bin>
mysql
-
u root
-
p

9.

Enter password:
********

10.

Welcome to the MySQL monitor. Commands end with ; or
\
g.

11.


12.

Type 'help;' or '
\
h' for help. Type '
\
c' to clear the buffer.

13.


14.

mysql>
use test

15.

Database changed

16.

mysql>
source
\
myprojects
\
vrlsapp
\
sql
\
dbschema.sql

17.


...

18.

mysql>
source
\
myprojects
\
vrlsapp
\
sql
\
populate.sql

19.


...

20.

mysql>
source
\
myprojects
\
vrlsapp
\
sql
\
mysqlgrant.sql

21.


...

The
dbschema.sql

and
populate.sql

query files in the above files may be imported into SQL
processing tools associated with other DBMSs to create and populate the application tables if necessary.
The
mysqlgrant.sql

query file performs S
QL grant statements needed if you are using a newly
installed MySQL installation, as shown below:

grant all on test.* to waabook identified by 'waabook' ;

grant all on test.* to waabook@localhost identified by 'waabook' ;

22.

Define the following environment
variables.

(Methods for doing this vary between Windows, Linux, and
Macintosh systems.)

ANT_HOME

-

the directory in which Ant was installed

e.g.,
c:
\
ant


JAVA_HOME

-

the directory in which the JDK was installed

e.g.,
c:
\
jdk


TOMCAT_HOME

-

the directory in which Tomcat was installed (if you are using Tomcat)

e.g.,
c:
\
program files
\
tomcat


23.

If you are using Tomcat,
copy the JAR file containing your DBMS's JDBC driver files into the
common/lib

directory under TOMCAT_HOME
.

24.

C:
\
>
cd

directory
-
containing
-
driver
-
file

25.

C:
\
...
\
>
copy mysql
-
connector
-
java
-
5.0.5
-
bin.jar
%TOMCAT_HOME%
\
common
\
lib



If you are using a DBMS other than MySQL, read the documentation for your DBMS to determine where you
can find the appropriate drivers.


If you are using a Web container other than Tomcat, follow the instructions regarding where JDBC drivers
used for JNDI
-
defined DataSources belong.

26.

Define a
DataSource

for the application to use within your application server environment.

This
process varie
s depending on which Web container software you are using, so we will concentrate here on
Tomcat. Consult the documentation associated with your application server software for appropriate
instructions.

In older versions of Tomcat, the most direct way to
define the DataSource was to edit the
server.xml

file
found in the
%TOMCAT_HOME%
\
conf

directory, adding a DataSource definition to the Tomcat
configuration manually.

<Context path="/vrls">


<Resource name="jdbc/vrls"


type="javax.sql.DataS
ource"


password="waabook"


driverClassName="com.mysql.jdbc.Driver"


maxIdle="2"


maxWait="5000"


validationQuery="select * from vrls_xref_listing_type"


username="waabook"



url="jdbc:mysql://localhost:3306/test?autoReconnect=true&amp;jdbcCompli
antTruncation=false"


maxActive="4" />

</Context>

Tomcat 5.5 and 6.x let you place a
context.xml

file containing the above text in a WAR file's
META
-
INF

directory. This lets you deploy applications with their own customized resource definitions without
having to modify the server's base configuration files. We include a
context.xml

in the application
distribution and place it in the appropriate place in t
he WAR file as part of the build process. If you are using
a different application server, a different DBMS, or different connection credentials, you will need to modify
this file before the application is built and deployed.

27.

Build the application using An
t.

This is accomplished using a command line interface (shell/terminal
window on UNIX/Linux, or DOS prompt in Windows), entering the directory into which you extracted the
contents of the application's distribution package, and running Ant.

28.

C:
\
>
cd myproj
ects
\
vrlsapp

C:
\
myprojects
\
vrlsapp>
%ANT_HOME%
\
bin
\
ant

This will cause Ant to look for the
build.xml

file found in that directory, and use the directives in that file
to invoke the Java compiler (
%JAVA_HOME%
\
bin
\
javac
) in order to build the application binaries.


Once the code has successfully compiled, build the web archive (WAR) file that will be deployed. This is
accomplished by running Ant from the command line with an argument ("war") that tells it to build the W
AR
file.

C:
\
myprojects
\
vrlsapp>
%ANT_HOME%
\
bin
\
ant war

29.

Deploy the WAR file.

This process varies depending on which Web container you are using, so we will
concentrate here on Tomcat. Consult the documentation for your Web container for specific instructio
ns.


In general, WAR files can be deployed to most J2EE Web containers by "dropping" them in the appropriate
directory. If the container is properly configured, it will automatically expand the WAR file's contents into an
appropriate place for deployment
within the container.


Tomcat is no exception: dropping the WAR file into the
webapps

directory under the Tomcat root, even
while Tomcat is running, will result in the WAR file being expanded and deployed. However, to ensure
proper deployment, we encourag
e you to stop Tomcat and clear out the "vrls" directory (if it exists) before
deploying the WAR file, and to restart Tomcat when you are done deploying.

30.

C:
\
myprojects
\
vrlsapp>
%TOMCAT_HOME%
\
bin
\
shutdown.bat

31.


C:
\
myprojects
\
vrlsapp>
rmdir %TOMCAT_HOME%
\
weba
pps
\
vrls /s /q

32.


C:
\
myprojects
\
vrlsapp>
copy vrls.war %TOMCAT_HOME%
\
webapps


C:
\
myprojects
\
vrlsapp>
%TOMCAT_HOME%
\
bin
\
startup.bat

We provide an Ant target in the
build.xml

file called "deploy", which should work if you are using Tomcat
5.5 and installed Tomcat 5.5 in the default location. If you have changed any of the defaults, you may need
to edit the
build.xml

file to reflect those changes. If you are unsure of how to do

this, go through the list
of manual steps described above.

C:
\
myprojects
\
vrlsapp>
%ANT_HOME%
\
bin
\
ant deploy

33.

Open the application in your web browser.

If necessary, start or restart your application server. (Live
deployment in Tomcat makes restart unneces
sary as Tomcat will expand and deploy the WAR file if it is
dropped into the
webapps

directory while Tomcat is running.) Then direct your web browser to
http://localhost:8080/vrls
, which will display a page describing the book and the associated
applicatio
n. Click on one of the "Try the app" links to use the application.

Debugging Tips

If you should experience problems getting the application to run:

1.

Check your server error logs (found in
%TOMCAT_HOME%
\
logs
) if you are having trouble connecting to
the database. This may give you insight into what the problem is.

2.

If database connectivity problems persist in Tomcat, remove the
"validationQuery"

line in the
context.xml

file and then rebuild and redeploy the ap
plication. (Note that this validation query will only
work once the application database tables have been populated, so make sure you have executed all the
provided SQL scripts before attempting to connect to the database.)

3.

When all else fails, go to
Google

and search for solutions to the problems you are seeing. Others have
probably faced similar problems. For example, searching on
"cannot load JDBC driver"

plus
"tomcat"

and
"mysql"

recently helped
solve
a nagging problem (that turned out to be a simple

configuration error
).