Archive for the ‘ruby’ category

Starting Ruby on Rails 2 with Netbeans and AWDWR book


After some break, I got back to Rails land. The return is not as smooth as I hoped: since Rails 2.0, many things work differently and quite a few steps learned from “The Book” do not work any more. The issues are more often than not quite easy to fix, but there is quite a few of them.

All the following is related to NetbeansRuby build 6327 from Jan 26th with JRuby 1.1RC1

After installing NetbeansRuby and updating the Gems (Tools->Ruby Gems), the attempt to generate Rails application fails:

/Users/miro/Applications/ `report_activate_error': Could not find RubyGem activeresource (= 2.0.2) (Gem::LoadError)

        from /Users/miro/Applications/ `activate'
        from /Users/miro/Applications/ `activate'
        from /Users/miro/Applications/ `each'
        from /Users/miro/Applications/ `activate'
        from /Users/miro/Applications/ `active_gem_with_options'
        from /Users/miro/Applications/ `gem'
        from /Users/miro/Applications/

Looks like the gem updater somehow missed activeresource – maybe a problem with dependencies ?To fix this, manually install activeresource 2.0.2 – Tools -> Ruby Gems, New Gems, search for resource, install. This is list of the installed gems:

  • actionmailer (2.0.2, 1.3.5)
  • actionpack (2.0.2, 1.13.5)
  • actionwebservice (1.2.6, 1.2.5)
  • activerecord (2.0.2, 1.15.5)
  • activerecord-jdbc-adapter (0.7.2, 0.7)
  • activeresource (2.0.2)
  • activesupport (2.0.2, 1.4.4)
  • jruby-openssl (0.1.1)
  • rails (2.0.2, 1.2.5)
  • rake (0.8.1, 0.7.3)
  • rspec (1.1.3, 1.1.1)
  • sources (0.0.1)

By default, the Rails 2 selects Sqlite3 database. This is great choice if you use C-Ruby, but not so great for JRuby – see this post. After few frustrated attempts to find workaround, I decided to go with Ruby 1.8.6, updated (again) the Gems in system installation (the first updated upgraded JRuby gems) and decided to try out the Depot sample – just have something that works, rather than fighting with issues of both Rails 2 and my own bugs. To keep with the book, I stayed with MySQL (quietly running in VMWare virtual machine).

By the book I have generated Product model and Admin controller, but instead of seeing familiar screen, Rails responded with error:

Routing Error

No route matches "/admin" with {:method=>:get}

This can be fixed by addingmap.connect ‘:controller/:action’to routes.rb, but this is probably bad idea because it goes against REST design principles which Rails 2 tries to promote. But to go ahead with the example, I hacked it 🙂 and added scaffolding. Next error I got was:

 NoMethodError in AdminController#index

undefined method `scaffold' for AdminController:Class

RAILS_ROOT: /Users/miro/Projects/RAILS2/Depot
Application Trace | Framework Trace | Full Trace

/Library/Ruby/Gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:203:in `load_without_new_constant_marking'
/Library/Ruby/Gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:203:in `load_file'
/Library/Ruby/Gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:342:in `new_constants_in'
/Library/Ruby/Gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:202:in `load_file'
/Library/Ruby/Gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:94:in `require_or_load'
/Library/Ruby/Gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:248:in `load_missing_constant'
/Library/Ruby/Gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:453:in `const_missing'
/Library/Ruby/Gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:465:in `const_missing'
/Library/Ruby/Gems/1.8/gems/activesupport-2.0.2/lib/active_support/inflector.rb:257:in `constantize'
/Library/Ruby/Gems/1.8/gems/activesupport-2.0.2/lib/active_support/core_ext/string/inflections.rb:148:in `constantize'
/Library/Ruby/Gems/1.8/gems/actionpack-2.0.2/lib/action_controller/routing.rb:1426:in `recognize'

Scaffolding is one of the parts removed in Rails 2. :-(. I tried to install ‘scaffolding’, which led to another error:

NoMethodError in AdminController#index

undefined method `paginate' for #

RAILS_ROOT: /Users/miro/Projects/RAILS2/Depot
Application Trace | Framework Trace | Full Trace

vendor/plugins/scaffolding/lib/scaffolding.rb:107:in `list'
vendor/plugins/scaffolding/lib/scaffolding.rb:95:in `index'
/Library/Ruby/Gems/1.8/gems/actionpack-2.0.2/lib/action_controller/base.rb:1158:in `send'
/Library/Ruby/Gems/1.8/gems/actionpack-2.0.2/lib/action_controller/base.rb:1158:in `perform_action_without_filters'
/Library/Ruby/Gems/1.8/gems/actionpack-2.0.2/lib/action_controller/filters.rb:697:in `call_filters'

Based on this post, I installed classic_pagination, which resulted in another error when trying to enter new Product

 ActionController::InvalidAuthenticityToken in AdminController#create


RAILS_ROOT: /Users/miro/Projects/RAILS2/Depot
Application Trace | Framework Trace | Full Trace

/Library/Ruby/Gems/1.8/gems/actionpack-2.0.2/lib/action_controller/request_forgery_protection.rb:79:in `verify_authenticity_token'
/Library/Ruby/Gems/1.8/gems/actionpack-2.0.2/lib/action_controller/filters.rb:469:in `send!'
/Library/Ruby/Gems/1.8/gems/actionpack-2.0.2/lib/action_controller/filters.rb:469:in `call'
/Library/Ruby/Gems/1.8/gems/actionpack-2.0.2/lib/action_controller/filters.rb:441:in `run'
/Library/Ruby/Gems/1.8/gems/actionpack-2.0.2/lib/action_controller/filters.rb:716:in `run_before_filters'
/Library/Ruby/Gems/1.8/gems/actionpack-2.0.2/lib/action_controller/filters.rb:695:in `call_filters'
/Library/Ruby/Gems/1.8/gems/actionpack-2.0.2/lib/action_controller/filters.rb:689:in `perform_action_without_benchmark'
/Library/Ruby/Gems/1.8/gems/actionpack-2.0.2/lib/action_controller/benchmarking.rb:68:in `perform_action_without_rescue'
/Library/Ruby/Gems/1.8/gems/actionpack-2.0.2/lib/action_controller/benchmarking.rb:68:in `perform_action_without_rescue'
/Library/Ruby/Gems/1.8/gems/actionpack-2.0.2/lib/action_controller/rescue.rb:199:in `perform_action_without_caching'

I backtracked, and tried to go with generated scaffold, rather with dynamic. The generated views were named edit.html.erb, rather than edit.rhtml and the generator did not allow to enter controller (second argument). This lead to error


Template is missing

Missing template admin/index.html.erb in view path /Users/miro/Projects/RAILS2/Depot/app/views

At this point I gave up and decided to:

  • read the Rails 2.0 documentation (after everything else failed, read the manual …)
  • reconsider using AWDWR as the guide – obviously, it needs to be updated to be useful with Rails 2. If you purchased the PDF from Pargmatic Programmers, there is an update available on their website, but it is not an update for Rails 2 …

I was not the only person to try AWDWR with Rails 2. The best resource I found was Ruby Plus website with series of screencasts, starting with this. The author – Bala Paranj did great job in putting together very useful collection of Rails screencasts. If you want to use the AWDWR with Rails 2.0, listen to them first – unless you are very experienced Rails developer.

Showstopper issue with JRuby ?


After some break, I dusted off the Ruby to try out one interesting idea Peter presented yesterday that literally ASKS for Rails. So I grabbed the latest and greatest RubyNetBeans from Ruby Hudson. For some reason it stopped updates on January 26th so the latest version I have got was build 6327. It is bundled with JRuby 1.1RC1 and the Rails version that comes preinstalled is 1.2.6. Using the menu Tools -> Ruby Gems, they can be easily upgraded to latest and greatest 2.0.2.

The trouble begins when you want to install database connectivity gems such as sqlite3-ruby. The installer fails with the message:

trying to install

INFO:  `gem install -y` is now default and will be removed
INFO:  use --ignore-dependencies to install only the gems you list
Building native extensions.  This could take a while...
extconf.rb:1: no such file to load -- mkmf (LoadError)
ERROR:  Error installing sqlite3-ruby:
    ERROR: Failed to build gem native extension.

/Users/miro/Applications/<a href="" class="linkification-ext" title=""></a> extconf.rb install sqlite3-ruby --no-rdoc --no-ri --include-dependencies --version &gt; 0

Gem files will remain installed in /Users/miro/Applications/<a href="" class="linkification-ext" title=""></a> for inspection.
Results logged to /Users/miro/Applications/<a href="" class="linkification-ext" title=""></a>

The missing file mkmf.rb is indeed missing from the JRuby distribution. This is entered as a bug in JIRA 1306 with resolution ‘Won’t fix’. Tough luck.

I understand the reasons and motivation for this decision – the JRuby team decided not to support native extensions in Gems, to keep the platform Java only. I also understand that in this particular case, there are workarounds – using ActiveRecord-DBC gem and JDBC drivers for the database will most likely work. Unfortunately, this decision makes choice of JRuby as platform very questionable.

I really liked JRuby for the comfort of having platform that is portable and safely wrapped within boundaries of the good old trusted JVM. I feel much more comfortable maintaining possibly several different versions of JRuby and corresponding Gems sets than maintaining same several configurations on the OS level and sudo-ing just to install Gems. I was more that happy to trade the lack of speed for this security.

The two main attractions of Ruby (from my point of view) are elegant, powerful language with beautiful syntax as well as sheer amount of code available as Gems to be reused. With the bug 1306, many of this code may not be available for JRuby – unless Gem authors make specific provisions for Java version of the Gem. I cannot think how this is a good idea and certainly not a good news for the future of the language.

One way out is use native Ruby interpreter, of course and make sure you do not mess up your installation by trying out new things. This does not allow the easy way into enterprise that JRuby was promising – by being basically ‘just another jar’ and running on Tomcat.
The other way out is to reconsider the Groovy. I still do not enjoy the syntax anywhere close to Ruby, but every Groovy class is Java class, there is no need for artificial bridges. It has own clone of Rails – GRails – that seems to provide lots of Rails magic and goodies and is based on Spring which I am very familiar and quite happy with. I still do not know whether the amount of “gems” in Groovy world is in the same league as Ruby (which is still limited compared to Python or even CPAN Perl bonanza) – but as long as I can find that what I need, it may be just enough.

For now, I will revert back to Ruby (no time to start learning Groovy+Grails), but I definitely will look at it later on.

JRuby – keeping your database.yml really DRY


I am experimenting in JRuby and use database in virtual machine (VMWare Fusion). This is great because you can have multiple versions of multiple databases ready without polluting the notebook. One downsize though is that IP address of the database will change between the VM restarts.

First important setting in Fusion is to use the ‘NAT’ option rather than ‘Bridged’. With NAT, you database host IP address will not follow the network. It was quite annoying to change it from 192.168.X to 192.168.Y everytime I reconnected at home from work or vice versa.

But even with this setting, the IP may change as you restart the VM and you will have to modify the database.yml. To keep the amount of editing minimal, here is my database.yml for the shoplet example:

common: &amp;common
adapter: jdbc
driver: com.mysql.jdbc.Driver
password: shoplet

&lt;&lt; : *common
url: /shoplet_dev
username: shoplet_dev

&lt;&lt; : *common
url: /shoplet_test
username: shoplet_test

&lt;&lt; : *common
url: /shoplet_prod
username: shoplet_prod

It will work in Ruby as well of course – with different (non-jdbc based) adapter..

JRuby trunk – issues with JDBC connectivity


Following the book on JRuby I am discovering small differences when trying the examples. After installing activerecord-jdbc gem which is available in version 0.5, the shoplet application fails with Rails 2.0, as soon as you click on the ‘About your application environment’ link.

=&gt; Booting Mongrel (use 'script/server webrick' to force WEBrick)
=&gt; Rails application starting on
=&gt; Call with -d to detach
=&gt; Ctrl-C to shutdown server
** Starting Mongrel listening at
** Starting Rails with development environment...
** Rails loaded.
** Loading any Rails specific GemPlugins
** Signals ready.  TERM =&gt; stop.  USR2 =&gt; restart.  INT =&gt; stop (no restart).
** Rails signals registered.  HUP =&gt; reload (without restart).  It might not work well.
** Mongrel 1.1.2 available at
** Use CTRL-C to stop.

Processing InfoController#properties (for at 2007-12-23 23:13:25) [GET]
Session ID: BAh7BiIKZmxhc2hJQzonQWN0aW9uQ29udHJvbGxlcjo6Rmxhc2g6OkZsYXNo%0ASGFzaHsABjoKQHVzZWR7AA%3D%3D--9791be23205677745e457b5bf62c50eba6e3813f
Parameters: {"controller"=&gt;"rails/info", "action"=&gt;"properties"}
Exception in thread "Ruby Thread11043409" java.lang.NoSuchMethodError: org.jruby.runtime.builtin.IRubyObject.setInstanceVariable(Ljava/lang/String;Lorg/jruby/runtime/builtin/IRubyObject;)Lorg/jruby/runtime/builtin/IRubyObject;
at JdbcAdapterInternalService.set_connection(
at JdbcAdapterInternalServiceInvoker$ Source)
at org.jruby.runtime.callback.FastInvocationCallback.execute(
at org.jruby.runtime.CallSite$
at org.jruby.runtime.CallSite$
at org.jruby.evaluator.ASTInterpreter.fCallNode(
at org.jruby.evaluator.ASTInterpreter.evalInternal(
at org.jruby.evaluator.ASTInterpreter.blockNode(
at org.jruby.evaluator.ASTInterpreter.evalInternal(
at org.jruby.evaluator.ASTInterpreter.eval(
at org.jruby.runtime.CallSite$
at org.jruby.runtime.CallSite$
at org.jruby.evaluator.ASTInterpreter.vcallNode(
at org.jruby.evaluator.ASTInterpreter.evalInternal(
at org.jruby.evaluator.ASTInterpreter.blockNode(
at org.jruby.evaluator.ASTInterpreter.evalInternal(
at org.jruby.evaluator.ASTInterpreter.rescueNode(
at org.jruby.evaluator.ASTInterpreter.evalInternal(
at org.jruby.evaluator.ASTInterpreter.eval(

I found out that there is newer version of activerecord-jdbc, which is also renamed to activerecord-jdbc-adapter. It is important to uninstall the old gem as well as install the new one, otherwise the application will not work.

$ jruby -S gem install activerecord-jdbc-adapter
Successfully installed activerecord-jdbc-adapter-0.7
1 gem installed
Installing ri documentation for activerecord-jdbc-adapter-0.7...
Installing RDoc documentation for activerecord-jdbc-adapter-0.7...
$ jruby -S gem install activerecord-jdbcmysql-adapter
Successfully installed jdbc-mysql-5.0.4
Successfully installed activerecord-jdbcmysql-adapter-0.7
2 gems installed
Installing ri documentation for jdbc-mysql-5.0.4...
Installing ri documentation for activerecord-jdbcmysql-adapter-0.7...
Installing RDoc documentation for jdbc-mysql-5.0.4...
Installing RDoc documentation for activerecord-jdbcmysql-adapter-0.7...

$ jruby -S gem list activerecord

*** LOCAL GEMS ***

activerecord (2.0.2, 1.15.6)
ActiveRecord-JDBC (0.5)
activerecord-jdbc-adapter (0.7)
activerecord-jdbcmysql-adapter (0.7)

$ gem uninstall ActiveRecord-JDBC
Successfully uninstalled ActiveRecord-JDBC-0.5
miroslav-adamys-macbook-pro:shoplet miro$ gem list ActiveRecord-JDBC

*** LOCAL GEMS ***

activerecord-jdbc-adapter (0.7)
activerecord-jdbcmysql-adapter (0.7)

After this, everything works and shoplet connects to database – as you can see from the console log.

Installation of Mongrel gem on JRuby


I bought the Ola Bini‘s eBook on JRuby and started to work through the examples. The installation of the Mongrel gem in JRuby (trunk revision 5341 fails with the following error code:

$ jruby -S gem install -y mongrel
INFO:  `gem install -y` is now default and will be removed
INFO:  use --ignore-dependencies to install only the gems you list
Building native extensions.  This could take a while...
extconf.rb:1:in `require': no such file to load -- mkmf (LoadError)
ERROR:  Error installing mongrel:
	ERROR: Failed to build gem native extension.

This error is logged in JIRA with id JRUBY-1771Workaround (possibly applicable for other gems is to explicitly specify the plaform:

$ jruby -S gem install -y mongrel --platform jruby
INFO:  `gem install -y` is now default and will be removed
INFO:  use --ignore-dependencies to install only the gems you list
Successfully installed mongrel-1.1.2
1 gem installed
Installing ri documentation for mongrel-1.1.2...
Installing RDoc documentation for mongrel-1.1.2...

Free PDF ebook 'Build your own Ruby on Rails Application'


For sixty more days (actually 59 days and 17 hours as of, or if you prefer to speak Javaish rather than Rubyish, new java.util.Date()) you will be able to download free PDF version of the book by Patrick Lenz Build Your Own Ruby on Rails Web Applications – courtesy of nice guys of Sitepoint.

The book download page is here, no strings attached – all it will cost you is disclosing an email address (book download link will be emailed to you) and bandwidth to get the 20 MB PDF. Mine is downloading right now, so I cannot tell anything about the book yet. It has 447 pages and from the table of content it looks like solid beginner’s book.

As a nice surprise it covers Ruby installation under OS-X 10.5 (Leopard) :-). From the rumors I have heard, Leopard should come with Rails preinstalled and with current version of Ruby (1.8.6) – which will not stay current long because Ruby 1.9 as well as Rails 2.0 are quite close.

So hurry up and get your copy until Sitepoint’s ISP does not disconnect them from excessive bandwidth usage :-). I am not joking, the first surge of download already did take down their server once :-).

After you download – spread the word.

Petstore for Ruby/JRuby


There is – after all – a Petstore implementation for Ruby. Somebody asked a question at the DZone and the response posted by Raphael Valyi was:

Re: Is there a Pet Store in Grails/Rails?

I’m speaking here for the Rails (JRuby) side:
there is actually a petstore, it can be found there:

Some guys used it to compare C-Ruby vs JRuby performance:—Petstore-shootout-t4289470.html

Ruby is in top ten programming languages


According to the TIOBE Programming Community index (which gives an indication of the popularity of programming languages), Ruby jumped 3 places and made it to top ten. Congratulations !

If you look closely on the trends down on the page, the dynamically languages popularity has risen over 3.6% during year period. Which is A Good Thing (TM).

Should I make my personal Top Ten of the programming languages, ordering languages by my degree of comfort of using the language, the language beauty (which is, in the eye of the beholder ;-)). Factoring in also how much I do enjoy reading/writing code in that language and it’s usefulness from both personal as well professional point of view, the index would look like this:

  1. Java
  2. C#
  3. Ruby
  4. SQL (both T/SQL and PL/SQL)
  5. Javascript
  6. Objective-C
  7. Python
  8. Shells (mostly BASH)
  9. Perl
  10. VB.NET

First place is practically tie – as the Java / C# are in reality more two dialects than two languages. They were very close at the beginning and with latest versions (C# 2.0, Java 5) they got even close. As it looks like, in the future they may diverge more with all the LINQ stuff coming in C# 3.0. I like Java portability, amazing variety of open source code available – and the development tools are IMHO still better on Java side (Eclipse, IDEA, Netbeans), but C# syntax of properties is nicer and there are slightly more commercial opportunities with Microsoft platform (at least during last 3-4 years).

The only “real” compiled language in my list is Objective-C, for which I have absolutely no commercial use for right now, but I hope that will change one day when the greatest of all cats finally arrives. Neither C or C++ made the list – the only encounters with C these days are during installations of Ruby GEM on Linux or Mac platform – which often comes with native part in C source – and the installer barfs at me with some gcc error :-). And despite using Mac for over a year now, I felt no need to write anything bigger than Hello World in AppleScript

Netbeans Ruby IDE – great tool getting even better !


I have spent last two days (and few evenings before) playing with Rails and using Netbeans Ruby IDE. What a great experience ! Unlike before, it is so much easier to have really good debugger that allows put breakpoints not only into the Ruby code in controllers and model – but even into the RHTML view files !

Netbeans team works hard on improving the tool. With latest build – 2434, I have noticed great addition – Rails console. The IDE had IRB window even before, but the environment did not load the Rails context and would not recognize e.g. ActiveRecord. Now with latest build, everything works like charm.

Using Rails and coding in Ruby is real pleasure and the only pain-point was (compared to using Eclipse with Java or Visual Studio 2005 with C#)  unavailability of a really good IDE. Yes – I tried RadRails and RDT plugin, but they still need lots of improvement and are not (yet) in the same league. Netbeans Ruby IDE is right now very likely best available free environment for Ruby and Rails environment. It allows work with both Ruby and JRuby with or without Rails – what more can you want 😉 ?

Thanks, Sun and the Netbeans team !

BOTD: Railscast


Today’s blog of the day is not really a blog, but a screencast. Dedicated to Ruby on Rails, the Railscast offers (as of today) 46 episodes of short, targeted practical advice on various aspects of Rails development.

Unlike some other screencasts who try to record the screen action as MPEG movie, this one is perfectly legible. In addition to the Rails enlightenment, you can also enjoy the speed and aesthetics of using TextMate  – with many great Ruby editing shortcuts and beautiful OS-X typography.

Available as subscription in iTunes – search for Railscast.