 |
If You Thought Rails Development Was Fast Before...
Posted by bleonard on March 05, 2007 at 02:32 PM | Comments (10)
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:
C:\Program Files\Java\jdk1.6.0\db\lib\derbyclient.jar
- 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:
--userdir D:\UserDirs\Ruby
- 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:
adapter: jdbc
driver: org.apache.derby.jdbc.ClientDriver
url: jdbc:derby://localhost:1527/sample
username: app
password: app
- 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,
title VARCHAR(255)
);
- 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:

Bookmark blog post: del.icio.us Digg DZone Furl Reddit
Comments
Comments are listed in date ascending order (oldest first) | Post Comment
-
Good to know that NetBeans is supporting Ruby/Rails now; thanks for the post. Mind if I point out a few things that, while not wrong in any absolute sense, are considered deprecated in Rails development these days?
WEBrick: A standalone Rails app will now prefer Mongrel and lighttpd as servers over WEBrick, if they are installed. WEBrick is a last resort, but at least it is guaranteed to be available since it comes with Ruby. Maybe a post on how to make NetBeans aware of these other options?
SQL/database creation: Migrations are the preferred technique here. There's lots of material available on the web about them (Google is your friend!). The Rails Wiki entry on migrations is as good a starting point as any. Again, if you can post any information on how to use migrations from NetBeans, that would be cool.
Scaffolding: Great for quick-hit examples like this. Bad for anything else. Unfortunately, we geeks tend to lift example code and use it as a starting point for stuff we intend to go live with at some point in the future. Avoid scaffolding like the plague. Even in example code, if possible.
Again, I'm glad to see this capability in NetBeans, and I hope you'll take this comment in the spirit intended. Keep up the good work! :-)
Posted by: davidrupp on March 06, 2007 at 08:16 AM
-
A quick follow-up: this code
<% for post in @posts %>
can be written more idiomatically in Ruby as
<% @posts.each do |post| %>
In fact, the 'for item in collection' syntax is just syntactic sugar for '.each'. They do exactly the same thing. The .each syntax deals directly with the collection's iterator and a corresponding block, though, so it's more idiomatic. It's totally a preference thing, but using .each may get you some extra Ruby cred. :-)
Posted by: davidrupp on March 06, 2007 at 10:06 AM
-
Great post - if only I could get it to work. I realize this may not be the proper forum for this discussion, but I was hoping you or one of your readers have overcome the following. Everytime I try to generate the "post" model, NetBeans throws the following error:
STARTING
C:/Documents and Settings/bcarde01/.netbeans/dev/jruby-0.9.2/lib/ruby/gems/1.8/gems/activerecord-1.15.2/lib/active_record/connection_adapters/abstract/connection_specification.rb:209:in `establish_connection': database configuration specifies nonexistent jdbc adapter (ActiveRecord::AdapterNotFound)
from C:/Documents and Settings/bcarde01/.netbeans/dev/jruby-0.9.2/lib/ruby/gems/1.8/gems/activerecord-1.15.2/lib/active_record/connection_adapters/abstract/connection_specification.rb:202:in `establish_connection'
from C:/Documents and Settings/bcarde01/.netbeans/dev/jruby-0.9.2/lib/ruby/gems/1.8/gems/activerecord-1.15.2/lib/active_record/connection_adapters/abstract/connection_specification.rb:195:in `establish_connection'
from C:/Documents and Settings/bcarde01/.netbeans/dev/jruby-0.9.2/lib/ruby/gems/1.8/gems/rails-1.2.2/lib/initializer.rb:229:in `initialize_database'
from C:/Documents and Settings/bcarde01/.netbeans/dev/jruby-0.9.2/lib/ruby/gems/1.8/gems/rails-1.2.2/lib/initializer.rb:88:in `process'
from C:/Documents and Settings/bcarde01/.netbeans/dev/jruby-0.9.2/lib/ruby/gems/1.8/gems/rails-1.2.2/lib/initializer.rb:43:in `send'
from C:/Documents and Settings/bcarde01/.netbeans/dev/jruby-0.9.2/lib/ruby/gems/1.8/gems/rails-1.2.2/lib/initializer.rb:43:in `run'
from ./script/../config/../config/environment.rb:46
from C:/Documents and Settings/bcarde01/.netbeans/dev/jruby-0.9.2/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require'
from C:/Documents and Settings/bcarde01/.netbeans/dev/jruby-0.9.2/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require'
from C:/Documents and Settings/bcarde01/.netbeans/dev/jruby-0.9.2/lib/ruby/gems/1.8/gems/rails-1.2.2/lib/commands/generate.rb:1
from C:/Documents and Settings/bcarde01/.netbeans/dev/jruby-0.9.2/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require'
from C:/Documents and Settings/bcarde01/.netbeans/dev/jruby-0.9.2/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require'
from script\generate:2
RUN COMPLETED
Posted by: bobcardenas on March 10, 2007 at 10:19 AM
-
Hi Bob,
Yes, the Ruby plugin is updated almost daily, and this bit of configuration changed since I posted the entry. You now need to add the following to your environment.rb:
if RUBY_PLATFORM =~ /java/
require 'rubygems'
RAILS_CONNECTION_ADAPTERS = %w(jdbc)
end
You want to place it right before the line that reads:
Rails::Initializer.run do |config|
Previously this configuration was already set for you, but now you need to add it manually.
/Brian
Posted by: bleonard on March 11, 2007 at 03:31 PM
-
Great job :D
After the "Generate a Scaffold to Customize the Display" section I must restart Deby (a error message is displayed) and after that I receive the newly message:
stack level too deep
Extracted source (around line #15):
12:
13:
14:
15: 'show', :id => post %>
16: 'edit', :id => post %>
17: 'destroy', :id => post }, :confirm => 'Are you sure?', :method => :post %>
18:
Cutting out all the link_to resolve, but is not a nifty solution :)
I have installed a March 12 Daily Build and updated to jruby-0.9.8 and rails-1.2.3.
Thank you in advance.
Max
Posted by: max_dev on March 13, 2007 at 09:14 PM
-
A jRuby issue has been filed for the stack level too deep error, but marked as wontfix! For this reason, I've switch to Ruby. My next blog entry will cover using Ruby to do the same application.
Brian
Posted by: bleonard on March 15, 2007 at 08:27 AM
-
I too encountered the "stack level too deep" error, but have had good success with build 200703111900. I am now able to completely build and run the Blog demo application. Starting with this build you do not need to modify the environment.rb for the jdbc adapter.
If you're having problems, confirm that the folder path to NetBeans and your UserDir do not contain any spaces.
Posted by: bobcardenas on March 15, 2007 at 01:25 PM
-
Well... I am not sure, but it is something in the rails 1.2.3: with build 200703151900 and rails 1.1.6 (installed with build 200703111900) the app work fine, with rails 1.2.3 throws the "stack level too deep" error.
Posted by: max_dev on March 16, 2007 at 01:47 AM
-
Have you tried using the -Xss switch to increase the stack size? According the comment on this jRuby issue, -Xss512k does the trick. /Brian
Posted by: bleonard on March 19, 2007 at 11:35 AM
-
Update: As part of this bug fix, the jruby scripts are no longer used for starting the WEBbrick server. As a result of this change, the WEBrick server is no longer picking up the derbyclient.jar from the classpath. For now you need to copy the derbyclient.jar into the jruby\lib directory in your userdir. /Brian
Posted by: bleonard on March 28, 2007 at 04:16 PM
|