Skip to main content

If You Thought Rails Development Was Fast Before...

Posted by bleonard on March 5, 2007 at 2:32 PM PST

A while back I watched the Creating a weblog in 15 minutes video that everyone was raving about and I remember thinking, yuck, too much command line. Then my buddy Roman produced a webcast (to be published later this week) showing how to do the same in NetBeans 6.0 Milestone 7. So here for you, I produce the blow-by-blow, with a slight twist in that I use the Derby database that comes with JDK 6 in place of MySQL.

Setting Things Up

To continue we need NetBeans 6.0 M7 or greater and JDK 6 (because I'm going to use its bundled Derby database).

  1. Download and install JDK 6.
  2. Set your environment's CLASSPATH to the location of your derbyclient.jar. This is necessary in order for the WEBrick server to find the Derby library. On Windows this will default to:

         C:\Program Files\Java\jdk1.6.0\db\lib\derbyclient.jar    
  3. Download and install NetBeans 6.0 M7 or greater.
  4. Before launching NetBeans, customize your launcher to set your user directory to a location that doesn't contain spaces (this is to work around an issue with Rails), for example:

         --userdir D:\UserDirs\Ruby
  5. Install the Ruby module from the update center (found in the Features folder. Many dependant modules will come along). The Ruby module requires a restart of th IDE.

Create the Ruby Project

  1. Create a new Ruby On Rails Application named BlogDemo. Wait while the project is generated and the WEBrick Web Server is started.

  2. Run the project.
  3. Add blog to the end of the URL string (http://localhost:3000/blog). You should get "Recognition failed for "/blog".

    Let's create a blog controller.

Generate a Controller

  1. Right-click the Controllers folder and select Generate. Name the controller Blog.

  2. Once the controller is generated, switch back to the browser and refresh the page. It should now return an "Unknown action - No action responded to index".

    Let's define a simple index.
  3. Add the following to the BlogController class:

  4. Save BlogController and refresh the browser page. You'll now see a message indicating the index.rhtml template is missing.

  5. For now, try just adding the following to the index definition:

  6. Save BlogController and reload the browser page to see Hello World in your browser.

Using a Scaffold

One of Rails' strengths is how easy it is to work with data. Check this out:

  1. Edit database.yml (found in the Configuration folder) and replace the development database configuration with the following:

      adapter: jdbc
      driver: org.apache.derby.jdbc.ClientDriver
      url: jdbc:derby://localhost:1527/sample
      username: app
      password: app
  2. Switch to the Runtime tab and right-click the jdbc:derby://localhost:1527/sample node under Databases and select Connect. The default password is app.
  3. Right-click the node again and select Execute Command.
  4. Paste the following and execute:

     CREATE TABLE posts (
        title VARCHAR(255)
  5. If you expand the Tables node you'll see the POSTS table in the list (right-click and select Refesh if you don't see it).

  6. Switch back to the Projects tab, right-click the Models folder and generate a new model named post.
  7. Replace the index definition in BlogController with

  8. Unfortunately, this step requires a restart of the WEBrick server, and unfortunately, this isn't yet possible in NetBeans. If you know of the command line to shut down WEBrick, please let me know. So we need to kill the process, which on Windows can be a crapshoot. You want to find the JAVA.EXE process that's running at about 60K.

  9. Return to NetBeans and press F6 to run the project again, which will start the WEBrick server.
  10. Add blog to the end of the URL string (http://localhost:3000/blog) and test...

The Beauty of Rails

  1. Let's add a new column to our table. Execute the following SQL statement as explained above:

  2. Return to the browser and click on the Edit link to see how Ruby has recognized our new body field (we'll adjust the size next).

Generate a Scaffold to Customize the Display

  1. Right-click the Views folder and generate a scaffold named Post referring to the Blog controller.

  2. Open _form.rhtml and change the text_field to a text_area for Body.

  3. Save the file and refresh the browser.

Make the List Look More Like a Blog:

Add a few more entries, for example:

Let's modify our scaffold so it looks more like your typical blog:

  1. Open list.rhtml and delete the entire table.
  2. In it's place, add the following:

  3. Save list.rhtml and refresh your browser.

  4. Reverse the sort order by adding .reverse to the end of @posts in list.rhtml:

Related Topics >>