If You Thought Rails Development Was Fast Before...
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).
- Download and install JDK 6.
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:
- Download and install NetBeans 6.0 M7 or greater.
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:
- 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
- Create a new Ruby On Rails Application named BlogDemo. Wait while the project is generated and the WEBrick Web Server is started.
- Run the project.
- 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
- Right-click the Controllers folder and select Generate. Name the controller Blog.
- 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.
- Add the following to the BlogController class:
- Save BlogController and refresh the browser page. You'll now see a message indicating the index.rhtml template is missing.
- For now, try just adding the following to the index definition:
- 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:
Edit database.yml (found in the Configuration folder) and replace the development database configuration with the following:
- 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.
- Right-click the node again and select Execute Command.
Paste the following and execute:
CREATE TABLE posts (
ID INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
- 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).
- Switch back to the Projects tab, right-click the Models folder and generate a new model named post.
- Replace the index definition in BlogController with
- 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.
- Return to NetBeans and press F6 to run the project again, which will start the WEBrick server.
- Add blog to the end of the URL string (http://localhost:3000/blog) and test...
The Beauty of Rails
Let's add a new column to our table. Execute the following SQL statement as explained above:
ALTER TABLE posts ADD COLUMN body VARCHAR(256);
- 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
- Right-click the Views folder and generate a scaffold named Post referring to the Blog controller.
- Open _form.rhtml and change the text_field to a text_area for Body.
- 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:
- Open list.rhtml and delete the entire table.
- In it's place, add the following:
- Save list.rhtml and refresh your browser.
- Reverse the sort order by adding .reverse to the end of @posts in list.rhtml: