Starting Ruby on Rails 2 with Netbeans and AWDWR book

2008/03/17

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/RubyNetBeans.app/Contents/Resources/nbrubyide/ruby1/jruby-1.1RC1/lib/ruby/site_ruby/1.8/rubygems.rb:379:in `report_activate_error': Could not find RubyGem activeresource (= 2.0.2) (Gem::LoadError)

        from /Users/miro/Applications/RubyNetBeans.app/Contents/Resources/nbrubyide/ruby1/jruby-1.1RC1/lib/ruby/site_ruby/1.8/rubygems.rb:311:in `activate'
        from /Users/miro/Applications/RubyNetBeans.app/Contents/Resources/nbrubyide/ruby1/jruby-1.1RC1/lib/ruby/site_ruby/1.8/rubygems.rb:337:in `activate'
        from /Users/miro/Applications/RubyNetBeans.app/Contents/Resources/nbrubyide/ruby1/jruby-1.1RC1/lib/ruby/site_ruby/1.8/rubygems.rb:336:in `each'
        from /Users/miro/Applications/RubyNetBeans.app/Contents/Resources/nbrubyide/ruby1/jruby-1.1RC1/lib/ruby/site_ruby/1.8/rubygems.rb:336:in `activate'
        from /Users/miro/Applications/RubyNetBeans.app/Contents/Resources/nbrubyide/ruby1/jruby-1.1RC1/lib/ruby/site_ruby/1.8/rubygems.rb:336:in `active_gem_with_options'
        from /Users/miro/Applications/RubyNetBeans.app/Contents/Resources/nbrubyide/ruby1/jruby-1.1RC1/lib/ruby/site_ruby/1.8/rubygems.rb:50:in `gem'
        from /Users/miro/Applications/RubyNetBeans.app/Contents/Resources/nbrubyide/ruby1/jruby-1.1RC1/bin/rails:18

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

app/controllers/admin_controller.rb:2
/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 #<AdminController:0x2cafe34>

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

ActionController::InvalidAuthenticityToken

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


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.


JRuby trunk – issues with JDBC connectivity

2007/12/28

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.

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

Processing InfoController#properties (for 127.0.0.1 at 2007-12-23 23:13:25) [GET]
Session ID: BAh7BiIKZmxhc2hJQzonQWN0aW9uQ29udHJvbGxlcjo6Rmxhc2g6OkZsYXNo%0ASGFzaHsABjoKQHVzZWR7AA%3D%3D--9791be23205677745e457b5bf62c50eba6e3813f
Parameters: {"controller"=>"rails/info", "action"=>"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(JdbcAdapterInternalService.java:122)
at JdbcAdapterInternalServiceInvoker$set_connection_FS1.call(Unknown Source)
at org.jruby.runtime.callback.FastInvocationCallback.execute(FastInvocationCallback.java:55)
at org.jruby.internal.runtime.methods.SimpleCallbackMethod.call(SimpleCallbackMethod.java:70)
at org.jruby.runtime.CallSite$InlineCachingCallSite.call(CallSite.java:158)
at org.jruby.runtime.CallSite$ArgumentBoxingCallSite.call(CallSite.java:103)
at org.jruby.evaluator.ASTInterpreter.fCallNode(ASTInterpreter.java:1092)
at org.jruby.evaluator.ASTInterpreter.evalInternal(ASTInterpreter.java:345)
at org.jruby.evaluator.ASTInterpreter.blockNode(ASTInterpreter.java:626)
at org.jruby.evaluator.ASTInterpreter.evalInternal(ASTInterpreter.java:293)
at org.jruby.evaluator.ASTInterpreter.eval(ASTInterpreter.java:168)
at org.jruby.internal.runtime.methods.DefaultMethod.call(DefaultMethod.java:147)
at org.jruby.runtime.CallSite$InlineCachingCallSite.call(CallSite.java:158)
at org.jruby.runtime.CallSite$ArgumentBoxingCallSite.call(CallSite.java:76)
at org.jruby.evaluator.ASTInterpreter.vcallNode(ASTInterpreter.java:1734)
at org.jruby.evaluator.ASTInterpreter.evalInternal(ASTInterpreter.java:474)
at org.jruby.evaluator.ASTInterpreter.blockNode(ASTInterpreter.java:626)
at org.jruby.evaluator.ASTInterpreter.evalInternal(ASTInterpreter.java:293)
at org.jruby.evaluator.ASTInterpreter.rescueNode(ASTInterpreter.java:1499)
at org.jruby.evaluator.ASTInterpreter.evalInternal(ASTInterpreter.java:442)
at org.jruby.evaluator.ASTInterpreter.eval(ASTInterpreter.java:168)

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.


Free PDF ebook ‘Build your own Ruby on Rails Application’

2007/10/02

For sixty more days (actually 59 days and 17 hours as of Time.now, 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

2007/10/01

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:
http://viewvc.rubyforge.mmmultiworks.com/cgi/viewvc.cgi/?root=tw-commons

Some guys used it to compare C-Ruby vs JRuby performance:
http://www.nabble.com/JRuby-vs-MRI—Petstore-shootout-t4289470.html


Netbeans Ruby IDE – great tool getting even better !

2007/06/24

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

2007/06/19

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.


Swing-ing back – to Netbeans

2007/06/11

It has been quite some time since I used a Swing based Java application. Compared to beauty and close-to-native feeling of Eclipse based application, Swing just did not feel right – it looked differently and behaved a bit differently. For long time, my desktop was Swing-free (not counting the JDK demo). Not any more, though.

What made me to reconsider, is new release of the Netbeans v 6. In the latest milestone builds (M6 to M9), Netbeans team built amazing Ruby development environment. Compared to Ruby plugin in Eclipse, it has way more functionality, better debugging, really impressive editing support and very cool Rails extensions. The full description of new features is here.

Best way how to start is to download pre-built IDE (inluding Mac friendly packaging) from NBExtras and give it a try. I was quite impressed. On modern hardware, the IDE is very responsive and Ruby development in Netbeans is even more fun as usual. The only small annoyance is frequent redraws and “flicker” of GUI when running on Windows – but it is likely one of the issues that may go away in final release.


Rails’ ideas everywhere

2007/05/09

Since I got through “Rails immersion”, I keep seeing the implementations of the same idea everywhere. Latest find is Subsonic, which implements Active Records in .NET space. Rather than writing more, see this screencast.


Best book for Ruby on Rails

2007/05/07

During last two weeks I have been reading and playing a lot with Rails.  Now I am in the state where I think I started to “get it” and understand how things work under the surface. Which means I am ready actually start using the framework for real work and become proficient as I get my hands dirty.

To get to this state, absolutely essential and by far the best book is the classic one – Agile Web development with Rails co-authored by the original Rails designer and one of the Pragmatic Programmers. Like other books, the first part is sort of step-by-step code tutorial, which leads you through creation of a Rails application, shows you how you access data, create UI, use scaffolding, etc etc. Unlike other books, this does not stop there and the second part of the book is an excellent coverage of Rails internals – ActiveRecord, ActiveView and templating system. Very well written and offering great balance between covering important features without becoming reference manual.

The best feature of this and other Pragmatic books is that you can purchase it as e-Book in the PDF format (without any DRM restrictions, just customized with  your name in text) which is cheaper, very portable 🙂 and very search-able. As added bonus, you get free updates when Rails changes or an error is discovered. As matter of fact, I just got such email, only few days after I bought the book.

The above book is not the only e-book I have purchased from Pragmatic Programmers. After 9 years, I have decided to buy a book covering such basic and boring thing as an editor. My previous (and first ever) book focusing  on how to use an editor was “Learning GNU Emacs“, back in 1998. You may say that if you need a book to use an editor, there must be something wrong with that piece of software. That is certainly one possible view. But the other explanation is that the editor is so powerful, extensible, customizable that it allows many ways how to be used and deserves the book. In my opinion, the later is very true about Emacs. I was using it for few years, but eventually switched back to Windows alternatives or IDE’s such as Eclipse or Visual Studio.

Now there seems to be another editor that has promise to be such great, powerful and  extensible tool as Emacs was  – but different way. It is TextMate – an editor available only on OS-X platform. I have got the license as part of the Mac Heist charity sale over Christmas and so far I was only scratching surface of its capabilities by using it similar way how I use Notepad++ on Windows. So I have decided to spend 20 bucks and few hours to “unlock the power” – editor is such basic tool used for hours every day that improving and optimizing your work habits actually can make a huge difference. Besides, TextMate is “the editor” for Rails development on OS-X and I am now in “wow-phase” of discovering the power of bundles. If you are on a Mac, give it a try – it is worth it.