Page 4 of 5

Conditional partials in Ruby on Rails

Have you ever wanted to include a partial in Ruby on Rails only if the partial file actually exists ? This can be handy e.g. if you want to be able to include a special menu (as a partial) for some controller actions based on the action name (controller.action_name) but don’t want to make a dummy partial for every action (which is normally needed since Rails will throw an error if it can’t find a partial).

The following example will render a partial contained in the file _.rhtml only if this file exists:

def render_menupartial
render_partial(controller.action_name) if FileTest.exist?(File.join(RAILS_ROOT, 'app', 'views',controller.controller_name,'_'+controller.action_name+'.rhtml'))
end

remember to call this from your view (or probably layout) like < %= render_menupartial %> instead of < % render_menupartial %>.

I know: basic stuff, but since I already used this basic functionality quite often I’m sure someone out there will find this useful.

Paging file error after copy W2K partition

After copying a Windows 2000 installation (differend disk/partition, same machine) you get the following error:

Your system has no paging file, or the paging file is too small.

Followed by instructions on how to change the paging file settings.

Unfortunately, after you clicked ‘OK’ and are waiting for the desktop to appear, you get the login screen again and are thus unable to login to your Windows installation.

Googling finds the following page: http://support.microsoft.com/kb/249321/.
This page mentions a number of possible solutions, none of which seem to be really feasible:

If however you have another partition that still works on the same machine (probably the source partition you copied) and you can access the ‘new’ installation partition from this one, you can try the following much easier solution:

  • Boot Windows using the working partition
  • Start Regedt32
  • Choose HKEY_LOCAL_MACHINE
  • Choose ‘File/Load Hive’ from the menu
  • Open on the new, faulty partition Windows\System32\Config\Software and give it a name (e.g. tmp) when asked
  • Perform the change as mentioned under 3. at http://support.microsoft.com/kb/249321/ in this ‘tmp’ hive
  • Choose ‘File/Unload Hive’ to save your changes

Try to reboot your new installation again. Hopefully it will work for you. At least it did for me.
Now, follow the instruction in http://support.microsoft.com/kb/q223188/ to restore the correct drive assignment.

Apache – adding local virtual domains

Why ?
Because it’s nicer, handier and sometimes just plain necessary to refer to ‘test-sites’ on your development machine as http://project instead of http://localhost/project. (first time I needed it was when breaking my head over mod_rewrite)

I’m still running a Fedore Core 3 Linux installation here, but I guess this will be the same on 90% of standard Linux installations.

How ?
Add the fake domain to /etc/hosts, so your computer knows the ip addres (=local) and doesn’t need to query dns (which won’t work).
Also change apache httpd.conf to add the domain and the corresponding ‘root path’.

Add the domain to /etc/hosts
Add the domain to your /etc/hosts file, either as a new domain pointing to your local ip (127.0.0.1), or as an alias to your machine’s entry that’s already in there. Look for the following line (there’s probably only one in there):

127.0.0.1 localhost.localdomain localhost

Now add your ‘test’ domain, let’s call this one ‘project’:

127.0.0.1 localhost.localdomain localhost project

Now ‘ping project’ and you’ll see the ping going to 127.0.0.1. NO REBOOT NEEDED (only a save ofcourse), this isn’t windows!

Change Apache httpd.conf
Next, we need to add the virtual domain to apache’s config file, which is located at /etc/httpd/conf/httpd.conf (on FC3, otherwise ‘find’ it). At the end of httpd.conf, add the following (let’s pretend the root of our domain is located at /var/www/html/project on the filesystem):

<virtualhost *:80>
DocumentRoot /var/www/html/project
ServerName project
</virtualhost>

Use ‘service httpd restart’ to restart Apache with the new config. (if this doesn’t work, try ‘httpd -k restart’)
Open a browser, surf to’ http://project’ => bingo!

Running Windows ?
I haven’t tested this, but I guess instead of adding your domain to /etc/hosts, you’ll need to find a file called ‘lmhosts’ (no extension!) and add your domain pointing to 127.0.0.1 there. I *think* the apache httpd.conf modification will be the same.

Ruby/OCI8, Oracle and ORA-12705

After installing the Ruby/OCI8 module on a Windows XP installation where sqlplus is working fine I got the following error when testing the connection in irb:


irb(main):012:0> cn = OCI8.new("username","password","database")
OCIError: ORA-12705: invalid or unknown NLS parameter value specified
from c:/ruby/lib/ruby/site_ruby/1.8/oci8.rb:158:in `begin'
from c:/ruby/lib/ruby/site_ruby/1.8/oci8.rb:158:in `initialize'
from c:/ruby/lib/ruby/site_ruby/1.8/oci8.rb:158:in `do_ocicall'
from c:/ruby/lib/ruby/site_ruby/1.8/oci8.rb:158:in `initialize'
from (irb):12:in `new'
from (irb):12
from :0

This one really took me several hours of google-hunting, finding 0 direct solutions. I was almost throwing my hat in the ring when I finally found the solution.

The Problem
In this case the ‘NLS parameter’ I was having a problem with turned out to be NLS_LANG.

This parameter is supposedly set during installation (I can’t remember choosing this, but this must be the only Oracle client installation I’ve ever done on MS Windows). The ORA-12705 error was cause by my client Ruby/OCI8 sending an incompatible NLS_LANG language parameter when connecting with this specific Oracle db.
Sqlplus, enterprise manager,… were all working fine.

Where is NLS_LANG stored on the client ?
The value was in my case stored in 3 different locations in the windows registry, always in a key ‘NLS_LANG’, once at HKEY_LOCAL_MACHINE\SOFTWARE\ORACLE, and 2 more times in sub-keys of this location. Just do a search in regedit for ‘NLS_LANG’ and you’ll find the culprits. 1 of these NLS_LANG had the value ‘NA’, the other 2 were ‘DUTCH_BELGIUM.WE8MSWIN1252’.

If you want to test if this is the source of your issue, then apparently you can override this value using an ‘NS_LANG’ environment variable. To test this, just do ‘set NLS_LANG = ‘ in your dosbox before starting irb.

How do I know what language the Oracle db expects ?
Open a connection to the db with sqlplus (or whatever it is you’re using) and issue the following:

SQL> select USERENV('LANGUAGE') FROM DUAL;

Result:

USERENV('LANGUAGE')
----------------------------------------------------
AMERICAN_AMERICA.WE8ISO8859P1

To view all NLS parameters, issue:

select * from NLS_DATABASE_PARAMETERS;

When doing ‘set NLS_LANG=AMERICAN_AMERICA.WE8ISO8859P1’ before starting irb, connecting to the db went fine.

Will this work with rails ?
I tried ‘set’ting NLS_LANG before starting webrick, but to no avail. I really had to change registry values for rails to work when running onder webrick.
I guess (hope) it will be the same when running under a more production-level server too.

What if I need to connect to several different db’s which expect different language settings ?
God knows.
I’d start by trying to set all NLS_LANG registry entries to ‘NA’. If that doesn’t work, I’d try to find out if it’s possible to set this as a parameter when connecting. But how you would do this in Rails: ???

Please let me know if you find out!

installing fxruby on ubuntu

Did you get the following error on your ubuntu 5.10 when installing fxruby (or probably any other gem) via rubygems:

Building native extensions. This could take a while...
extconf.rb:4:in `require': no such file to load -- mkmf (LoadError)
from extconf.rb:4

then you probably need to install the ruby1.8-dev package.

Komodo with Ruby support

By the way… I started looking into ruby a while ago (after checking out what all the fuss concerning ‘Ruby on Rails’ was all about and being pleasantly surprised by both Rails and Ruby).

Now, seeing that Activestate Komodo is my favorite tool for both perl and php (it even works on both windows AND linux… and even mac os x, but I don’t use os x (yet)), I was pleasantly surprised (again) to see the next version would have ruby support.

Check the beta/alpha here.

Installing scons on Fedora Core 3

I downloaded the rpm scons-0.96.1-2.1.fc3.rf.noarch.rpm from http://dag.wieers.com/packages/scons/

If scons (I needed it for the kdissert mind mapping tool), gives the following error:

Checking for uic : uic was not found - set QTDIR put it in your PATH ?


You probably need to install the Qt gui toolkit headers with ‘yum install qt-devel’

If you get:

Checking for the kde includes : The kde includes were NOT found

You need a ‘yum install kdebase-devel’ (or at least something else this is dependent on)

Hope this help someone some minutes googling or maybe prevents some hairpulling. In any case, kdissert is a nice tool though. I was just evaluating a commercial mindmapper on windows (Visual Mind) when it caught my eye. It’s got basically the same features so I guess I can stop evaluating it. I must admit Visual Mind looks real nice, but my wallet can’t see the difference.

I just wonder… why is it you can’t have 2 branches arrive at the same node… say, you’ve got 2 possible paths giving the same result and you haven’t made up your mind yet about which one you’ll be using. This doesn’t seem to be possible in either kdissert or Visual Mind.

FXRuby – require keeps returning false

You installed the fxruby gem, installed the FOX GUI library, checked dependencies, compiled and rechecked everything several times again, breaking your head why the ‘quick sanity check’ (require ‘fox14’) as suggested on http://www.fxruby.org/doc/gems.html keeps returning ‘false’ ?

Well, instead of:

require 'rubygems'
require 'fox14'

try this:

require 'rubygems'
require_gem 'fxruby'

Supposedly both should work, but somehow only the ‘require_gem’ way works for me.

…I hope this saves some people some headaches…

Problem booting copied Fedora partition with Grub

Situation:
After copying a Fedora Core 3 partition to a new harddrive (using Knoppix) I was at first unable to boot the new partition (using Grub).
I got the following error:

switchroot: mount failed: 22
umount /initrd/dev failed: 2
Kernel panic - no syncing: Attemped to kill init!

I vaguely remembered having this situation before, but really couldn’t remember how I dealt with it.
Googling around didn’t really produce much results at first (only more people having the same issue, but no real answers), but I was able to puzzle a solution together.

Problem:
It turned out that both my grub.conf and /etc/fstab files contained a reference to a ‘LABEL’ that seemed to be missing. Relevant entry of grub.conf:

title Fedora Core (2.6.9-1.667)
root (hd0,5)
kernel /boot/vmlinuz-2.6.9-1.667 ro root=LABEL=/ rhgb quiet
initrd /boot/initrd-2.6.9-1.667.img

entry of fstab:

LABEL=/ / ext3 defaults 1 1

Solution:
For as far as I understand you’d normally put a device here (in my case I would have been able use /dev/hda6 in both files), but the ‘LABEL=/’ here means as much as ‘use the partition with label /’.
Putting a label on a partition is possible using ‘e2label’. I labelled my /dev/hda6 as follows (using Knoppix):

su
mount -o dev,rw /mnt/hda6
(to mount my new partition writable)
e2label /mnt/hda6 /

Apparently it’s also possible to set the partition label/volume name using ‘tune2fs’, but since the above worked for me I didn’t really look into this.