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 %>‘.
- Define a new variable in controller as ‘
- 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 ofmysql-connector-java-5.0.5-bin.jarfrom 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.
- In Name, select ‘
- 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.
Related posts:
[Trackback] Follow up from here. In this post I’ll show how a Ruby-on-Rails (RoR) application, talking to MySQL database, can be deployed as a WAR file on GlassFish V2. Here are the steps I followed: Create a HelloWorld RoR app talking…
Comment by Arun Gupta's Blog — May 17, 2007 @ 7:47 am
[Trackback] Craig announced jMaki on Rails. Here I provide detailed steps to read data from a MySQL database and display it in a jMaki-wrapped Yahoo DataTable widget. Download NetBeans 6.0 M9 (Full version) and configured to install only "Base IDE", &quo…
Comment by Arun Gupta's Blog — May 17, 2007 @ 11:34 am
[Trackback] Get ready for an interactive afternoon of coding and discussion on JRuby and GlassFish on Aug 8th in San Francisco, CA. In this free event, JRuby on Rails: Power plus simplicity, learn how to code and deploy a JRuby…
Comment by Arun Gupta's Blog — August 7, 2007 @ 9:59 am
[Trackback] Earlier in a three-part series (part1, part2, part3) I showed how a JRuby application can be deployed on GlassFish. This screencast consolidates all the entries together and shows how such an application can be developed and deployed using NetBeans…
Comment by Arun Gupta's Blog — August 8, 2007 @ 9:46 am