Saturday, October 13, 2007

How to get a working Rails 1.2.5

After today's

gem update


I repeatedly got the same annoying error message again and again:

Install required dependency activesupport? [Yn] Y
ERROR: While executing gem ... (Gem::GemNotFoundException)
Could not find activesupport (= 1.4.3.7843) in any repository


The fix is however simply - just specifiy a source server for the packages (where Rails 1.2.5 can already be found):

gem install rails --version '= 1.2.5' --source http://gems.rubyonrails.org/ -y


Problem sovled - let's get back preparing Rails 1.2.x application for the migration to Rails 2.0

Update 1 - There are again failing updates!
To resolve this fully, just get rid of everything old and reinstall:

gem uninstall rails (select "all")
gem install rails -y --source http://gems.rubyonrails.org/

Thursday, August 23, 2007

Classloader issues in rails

Once upon a time, everybody gets hit by a train!
The classloader train on rails!

That's when everything works on your development workstation, in development and production mode, but it won't work on your production system.
That's when you check every line of your controllers code, every line of your models code.

But don't fear, there is an easy way to debug this and an easy way to prevent this in the future!

How to debug this:
Since ruby is a really really code language, try this:
Put a puts statement before and after your class!

puts 'start loading account_class'
class Account < ActiveRecord::Base
# your code here
end


Once you're finished, just try this:

script/console


You will see something like this:

devws:/development/projects/demo art$ script/consoleLoading development environment.
start loading account
end loading account
start loading xxx
end loading xxx


Now login to your production, errr, integration system, and try this again.
One thing that really shocked me, was:
On Mac OSX (my workstation) every model was loaded.
On Linux (my production system) one model was missing!

Okay, I just had 19 models, so it was easy to track down the missing class :)

But, you can go even further - put a puts statement before and after your methods:

puts 'start loading account_class'
class Account < ActiveRecord::Base
puts 'before doSomethingWithADifferentClass'
def doSomethingWithADifferentClass
end
puts 'after doSomethingWithADifferentClass'
end


That's how you can track down, if your methods get fully loaded.


How to prevent this in the future:
1) Put your requires where the belong (environments.rb could be loaded after your model class)
2) Put your require statements in the classes that need them
3) Be careful when you use memcached -> when you unmarshal an object (get it out of memcached) the class needs to be loaded first!

One additional info:
In development mode, all classes are reloaded everytime. In production mode classes are only loaded once :)


My models and even my controllers have now their require statements - I hope this help a bit!


As usual, once you know what hits you, you know how to search for more information and find these resources:
http://duncandavidson.com/archives/285
http://mojodna.net/2007/02/12/classloading-in-rails/

Sunday, July 1, 2007

Everybody needs a javascript calendar (Calendariffic)

Ever tried to write a web application where an user can create an account? You will probably have done this in the past.
You will also have faced the problem, that some information has a different formatting in different countries. Those date columns are a good example for this. Here is what I usually encouter:
  • dd/mm/yyyy
  • mm/dd/yyyy
  • yyyy/mm/dd


So, what you're going to do to satisfy everyone? Tell them the format, they should you? Try to guess what they mean by looking at the input?

Why not make this really easy (at least) for date input fields? Here is how:
  1. Get a decent javascript calendar
  2. Integrate it with ruby on rails
  3. Make your users happy


Step 1: What is a decent javascript calendar?
Just google for "javascript calendar" and you will find: The coolest DHTML / Javascript calendar
A very nice and sexy javascript based calendar (LGPL).

Step 2: How to integrate this with a rails project?
How about: "script/plugin install http://opensvn.csie.org/calendariffic/calendariffic/"
And you're done?

Step 3: Help your users with your date fields
1. Add the javascript files to your view
<%= javascript_include_tag 'calendariffic/calendar.js', 'calendariffic/calendar-setup.js', 'calendariffic/lang/calendar-en.js' %>

2. Add the css files to your view (there are many precreated layouts available: aqua, winter, blue, sommer, green, win2k-1|2, win2k-cold-1|2, system)
<%= stylesheet_link_tag 'calendariffic/calendar-win2k-cold-1.css' %>

3. Create a (readonly) date field in your view
< % = calendariffic_input(false, 'date_field_name_goes_here', 'calendariffic/date.png', 'start_cal', '%m/%d/%y', date_field_value_or_nil_goes_here, {:class => 'myfavoriteclass', :readonly => 'true'}, {:class => 'borderless'}) %>


Why should the date field be readonly? That's how you "help" your users (and yourself), and tell them that they have to click on the nice little icon to input their date value :)

Caution:
If you use a language different then "../lang/calendar-en.js", you will have to define one javascript variable inside the language javascript file:
Calendar._FD = 1; // First day of the week. "0" means display Sunday first, "1" means display Monday first, etc.

Thanks to the author of calendariffic (Paul Danese), who helped me with the javascript language issue, I was able to add good looking and "user guiding" date fields to my web application within a few minutes.

Thursday, June 14, 2007

Getting RMagick running on MAC OSX in 3 easy steps (with MacPorts)

After figuring out (with some help) how to get RMagick running on debian, I had to get it running on my development notebook - a MacBook with Mac OSX 10.4 (Intel).

To keep things simple I really like to use MacPorts to install new software onto the Mac.

So here we go:
1. Install ImageMagick with MacPorts (this takes some time - get a coffee NOW):
sudo port instll ImageMagick +graphviz +gs +jbig +jpeg2+lcms +mpeg +nox11 +wmf

MacPorts just installed ImageMagick in /opt/local/lib. Mac OSX includes by default /lib, /usr/lib, and /usr/local/lib as library path.
2. All we have to do, is to "show" gem where we installed imagemagick:
export LD_LIBRARY_PATH=/opt/local/lib

3. Now we can install RMagick on the Mac without any problems:
sudo gem install RMagick

Now check that it did really worked:
irb -rubygems -r RMagick
(now enter) puts Magick::Long_version

Done :)


BTW: I don’t get - After the installation, I started a completely new terminal and check it again:
irb -rubygems -r RMagick
(now enter) puts Magick::Long_version

And it works – but this time the LD_LIBRARY_PATH variable is not set!

Getting RMagick installed on a debian machine

After installing several packages with debian apt, I thought I just could do:

sudo gem install rmagick

But what I got was this:
Building native extensions. This could take a while...
ERROR: While executing gem ... (Gem::Installer::ExtensionBuildError)
ERROR: Failed to build gem native extension.

ruby gem_extconf.rb install rmagick

sh configure

Configuring RMagick 1.15.7
checking for gcc... gcc
checking for C compiler default output file name... a.out
checking whether the C compiler works... yes
checking whether we are cross compiling... no
checking for suffix of executables...
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ANSI C... none needed
checking for ruby... /usr/bin/ruby
checking for Magick-config... no
checking for GraphicsMagick-config... no
configure: error: Can't install RMagick. Can't find Magick-config or GraphicsMagick-config program.
RMagick configuration failed with status 1.


Gem files will remain installed in /usr/lib/ruby/gems/1.8/gems/rmagick-1.15.7 for inspection.
Results logged to /usr/lib/ruby/gems/1.8/gems/rmagick-1.15.7/gem_make.out


Not what I expected. Finally I decided to give the development version of imagemagick a try - it still didn't help. So I fired you firefox and search google for a solution and found this wonderful blog entry:
Quick Tip: Install Rmagick Gem on Ubuntu

And I did exactly what Vince W. wrote:
sudo apt-get remove --purge librmagick-ruby-doc librmagick-ruby1.8

after that:
sudo apt-get install libmagick9-dev ruby1.8-dev

and tried again:
sudo gem install rmagick

Guess what - it just worked!
Now check that it really works:

irb -rubygems -r RMagick
(then enter) puts Magick::Long_version

That's it! My thanks go to Vince W.