Posted by
arungupta on May 16, 2007 at 3:22 PM PDT
UPDATE: Simplified steps for
GlassFish V2 are available
here
and for V3
here.
Follow up from
here.
In this post I show how a Rails app can talk to database. Here are the
steps I followed:
- Deploy a RoR application as WAR on GlassFish using
these steps.
- Modify the application such that "Hello World!" is read from the
controller instead of defined in the view (this is natural to RoR
programmers anyway!).
- Define a new variable in controller as '
@hello_string = "Hello
World!"'.
- Replace the string in view with '
<%= @hello_string %>'.
- Download and install MySQL Community
Server 5.0 and MySQL
Connector/J 5.0.5.
- Create database using the command: '
mysqladmin -u root create
helloworld_development'.
- Right select the NetBeans project, select '
Generate',
select 'model', specify the arguments as 'table1',
click 'OK'. This will generate, in NetBeans project, Database
Migrations, migrate, 001_create_table1s.rb.
- Open 001_create_table1s.rb, change self.up helper method such that it looks like:
def self.up
create table :table1s do |t|
t.column :data, :string
end
end
- Right-select NetBeans project, select '
Run Rake Target', 'db',
'migrate'. This generates the appropriate database tables.
- In NetBeans Runtime tab, right click on '
Databases', 'New
Connection ...'.
- In Name, select '
New Driver ...' and specify the location
of mysql-connector-java-5.0.5-bin.jar from the MySQL
Connector/J installation directory.
- In Database URL, give '
jdbc:mysql://localhost:3306/helloworld_development'.
- Expand the newly added database node, expand Tables, right-select '
table1s',
select 'Execute Command ...'.
- Enter the following SQL "
insert into table1s values (1, 'Hello
from database')". Run the SQL.
- Change the controller such '
hello_string' is initialized
from the database. This is a contrived example but still convey the concept.
Anyway, The code looks like:
@hello_string = Table1.find(1).data
- Start MySQL database by giving the following command:
mysqld --console
- Save all the files and refresh the page in the browser. If your
controller name is '
say' and view is 'hello', then
the URL will be 'http://localhost:3000/say/hello'.
The browser will now display 'Hello from database'.
In a later post, I'll describe how this same application can be deployed as
WAR in GlassFish V2.
Technorati:
glassfish rubyonrails
ror
netbeans