Monday, May 25, 2009

Pushing to a Gitosis Git Repo for the first time

git push origin master:refs/heads/master

Saturday, May 09, 2009

Errors when Running GWT from Eclipse

I was able to get a preview release of the Google AppEngine SDK for Java last week, and finally had an opportunity to play with it. I installed Eclipse on my 64bit Ubuntu Linux box and all the plugins as suggested by Google's Java docs. Everything worked great until I tried to debug the default app as generated by the Eclipse Plugin.
I got an error in the GWT (Google Web Toolkit) packages saying (among other things)

libswt-pi-gtk-3235.so: wrong ELF class: ELFCLASS32 (Possible cause: architecture word width mismatch)

With a little digging, I found that this was related to 32bit compiled java libraries being run under the 64bit Java runtime. The solution is to install the 32bit Java runtime binary, and instruct Eclipse to use it.

#From Terminal
sudo apt-get install sun-java-6-bin ia32-sun-java6-bin
sudo apt-get remove gij
sudo update-alternatives --config java
# Select the 32bit runtime


From Eclipse go to Window -> Preferences -> Java -> Installed JRE's -> Add -> Standard VM and use the path /usr/lib/jvm/ia32-java-6-sun-1.6.0.13

Now you can Run -> Debug As -> Web Application

Tuesday, April 21, 2009

Rails and form_for() within Partials

This one tripped me up for almost 2 hours.

When using form_for() within a partial, use the variable reference instead of a symbol when specifying the form object. i.e. Use form_for(@user){|user_form|}, instead of form_for(:user){|user_form|}

I was creating a multi-model form with nested attributes using the new Rails 2.3 fields_for semantics, and the nested model field id's didn't have the numbered "*_attributes_0" suffix string. Ruling out syntax errors, I inspected the nested form objects and found that my user_form.object was nil, and thus the form_for method was not assigning the form object. When led me to find that my use of form_for(:user) was broken. form_for(@user) worked perfectly.

My conclusion may be incorrect, but it appears that using a symbolic reference as an object attribute for form_for() within a partial is broken.

The moral of the story is, just use the variable reference.

Saturday, March 28, 2009

Capistrano deploy.rb for Git and Phusion Passenger

Here is my Capsitrano deploy.rb script/recipe for use with Git repos on servers powered by Phusion Passenger (mod_rails).

http://gist.github.com/87207

Friday, January 09, 2009

Telnet SMTP Escape Character

When connecting to an SMTP server via telnet (i.e. telnet mail.example.com 25), you will be informed that "Escape character is '^]'." This means you need to press CTRL-] (control plus right-bracket)

I am embarrassed how long it took me to realize that.

Sunday, January 04, 2009

MySpace Mobsters Calculator and Strategy Helper

I wrote a Mobsters helper this weekend to help in choosing the best weapons configuration. Enjoy!

I am liking Google AppEngine so far, and this weekend project has really put me onboard with jQuery.

Friday, December 05, 2008

Error Updating RubyGems

If you get the following error:
undefined local variable or method `remote_gemspecs’

Then run:
sudo gem install rubygems-update
sudo gem update

Set JAVA_HOME on Mac OSX

If you want to install the rjb Ruby Gem, you will need to set the JAVA_HOME environment variable.

Add the following line to ~/.login or /etc/profile and restart your terminal.

setenv JAVA_HOME /System/Library/Frameworks/JavaVM.framework/Home

Tuesday, October 21, 2008

Rails Application in A Subdirectory

If you want to have a Rails application work under a subdirectory, as in http://www.example.com/rails_app , then add this line to your config/environment.rb.

ActionController::AbstractRequest.relative_url_root = "/rails_app"

Thursday, October 16, 2008

Using Ruby Java Bindings on Dreamhost

Getting rjb, also known as "Ruby Java Bindings' to work in a Dreamhost account can be somewhat problematic. Fortunately, it is also fairly straightforward. You just have to install all the dependencies in the user directory.

In my case, I was setting up a Rails Application that used the iText Java library to fill in PDF documents for user download. Of course, the server environment was not using Sun Java and the Java headers were not present, so gem install rjb failed. Joy.

The first course of action was to do a local install of Java.

Download jdk-6u7-linux-x64.bin and jre-6u7-linux-x64.bin from the Sun Java site. Then create an ~/opt directory and extract the JRE and JDK (you will need to chmod u+x both files then call them from the command line). Move the resulting folders to ~/opt . I renamed the folders to jdk and jre for simplicity.

Now you ensure that user gems are enabled in cPanel. Then add the following 3 lines to ~/.bash_profile

export JAVA_HOME=/home/username/opt/jdk
export GEM_PATH=/home/username/ruby/gems
export GEM_HOME=/home/username/ruby/gems

Run source ~/.bash_profile to load the paths.

Now you can run gem install rjb without any problems. You will likely have to re-install Rails and other gems because we will be telling our Rails app to load gems from the user directory. Just use the regular gem install syntax.

Add the following 2 lines to your config/environment.rb at the top

ENV['GEM_PATH']='/home/username/ruby/gems'
ENV['JAVA_HOME']='/home/username/opt/jdk'

That's pretty much it.

I will concede that these probably aren't the best instructions, but this is the real meat of the solution. If you keep getting "no such file to load" errors, you will need to extract the gems to the vendor/plugins directory. cd RAILS_ROOT/vendor/plugins and gem unpack gem_name for each problematic gem. I believe this is an issue with Passenger.

Please correct me if I am wrong about any of this, it was a very long day!

A big thank-you to the Planet

Thanks for getting hacked, you made my day a whole lot more interesting!

Saturday, September 27, 2008

Problems with IE7 Sessions Not Saved in Rails or PHP

If you are unlucky, your login routine will work in Firefox, Internet Explorer 6, Safari, Opera, or whatever, but it won't persist in Internet Explorer 7. x

I spent days on this one: Login worked great in Firefox, but in IE7 the same credentials resulted in a page that looked like I never attempted to log in. The logs showed me that the user credentials were accepted and the page redirected as expected, but the subsequent page load was absent any sign that I logged in.

Well, check your subdomain. You can't have an underscore in a subdomain. http://support.microsoft.com/kb/909264

In this instance, IE7 is inconsistently supporting standards. The browser will accept a badly formatted domain and show you the page in your browser, but will refuse to set the session cookie. Firefox and Safari are not quite so strict.

Thursday, July 03, 2008

Rails: Get controller route hash from url string

This is somewhat uncommon, but to get the routing hash from a URL string:

ActionController::Routing::Routes.recognize_path('/users/1')
# => {:controller => 'users', :action => 'show', :id => '1' }

Saturday, June 07, 2008

Firefox in Ubuntu 8.04

Is it just me, or is Firefox 3 Beta unusably unstable???

Saturday, May 17, 2008

Using mDNS / Zeroconf / Avahi and Firestarter Firewall on Ubuntu

About Multicast DNS
Multicast DNS (mDNS) allows machines on a local network to connect by name without the need for a local DNS server. Each machine must have a hostname set, and a mDNS client/server installed. Macs have it preinstalled (being called Bonjour), as well as modern releases of Linux including Ubuntu(called avahi). Windows clients can download and install Bonjour for Windows from the Apple website. It seems most modern printers use mDNS for easy printer setup. Even though I run a local DNS server using dnsmasq, mDNS makes DHCP client access easy.

The Problem
Long ago I installed Bonjour for Windows so I could access the machine via mDNS (multicast DNS). Today, my Ubuntu 8.04 machine would no longer connect to my Windows printer share. It turns out that Firestarter on the Ubuntu machine was the culprit. mDNS requests were being blocked regardless of my Firestarter config. So here is the fix:

sudo gedit /etc/firestarter/user-pre


Add the following two lines and save:

$IPT -A INPUT -p udp --dport 5353 -d 224.0.0.251 -j ACCEPT
$IPT -A OUTPUT -p udp --dport 5353 -d 224.0.0.251 -j ACCEPT


These two IP tables rules allow the mDNS requests in and out, and override any GUI configuration in Firestarter.

Restart avahi, then restart firestarter:

sudo /etc/init.d/avahi-daemon restart
sudo /etc/init.d/firestarter restart



If you are still having trouble, look at /etc/default/avahi-daemon and make sure it says: AVAHI_DAEMON_DETECT_LOCAL=1 . Also run sudo netstat -ltunp to verify that avahi-daemon is listening on port 5353.

Found here.

UPDATE: apparently this doesn't work for some.

Tuesday, March 18, 2008

Compile And Install FFMPEG on Redhat RHEL4

I just had the wonderful opportunity to set up a video sharing site on Redhat Enterprise Linux 4. Yeah. Four.

The worst part was getting ffmpeg to work, so here are my notes.

Requirements
(download, extract, etc.)

Configure, Compile, and Install FFMPEG
./configure --enable-libmp3lame --enable-libvorbis \
--enable-libxvid --enable-shared --enable-libfaac \
--enable-libfaad --enable-gpl --enable-libtheora \
--enable-libx264
make
make install

Create Symlinks


ln -s /usr/local/lib/libavformat.so.50 /usr/lib/libavformat.so.50
ln -s /usr/local/lib/libavcodec.so.51 /usr/lib/libavcodec.so.51
ln -s /usr/local/lib/libavutil.so.49 /usr/lib/libavutil.so.49
ln -s /usr/local/lib/libmp3lame.so.0 /usr/lib/libmp3lame.so.0
ln -s /usr/local/lib/libavformat.so.51 /usr/lib/libavformat.so.51
ln -s /usr/local/lib/libavformat.so.52 /usr/lib/libavformat.so.52
ln -s /usr/local/lib/libavdevice.so.52 /usr/lib/libavdevice.so.52
ln -s /usr/local/lib/libx264.so.58 /usr/lib/libx264.so.58
ln -s /usr/local/lib/libtheora.so.0 /usr/lib/libtheora.so.0
ln -s /usr/local/lib/libfaad.so.0 /usr/lib/libfaad.so.0
ln -s /usr/local/lib/libfaac.so.0 /usr/lib/libfaac.so.0

If you don't create these symlinks you will get errors like:
ffmpeg: error while loading shared libraries: libavdevice.so.52: cannot open shared object file
Done.

Run ffmpeg and it should spit out that insane list of options, wihout any errors.

Saturday, March 01, 2008

Online Flashcards and Quizzer


I just launched a new online flash cards and quizzing site called FB_Quizzr. The site is primarily intended to be used as a Facebook application.

Wednesday, February 27, 2008

How to Disable Asset Timestamps in Rails

By default, Rails appends a timestamp parameter to asset urls to aid in cache management. If you need to disable this, simply add the following line to your config/environment.rb

ENV['RAILS_ASSET_ID'] = ''

Tuesday, February 26, 2008

Advanced puts Syntax in Ruby

The "puts" command can be used with sprintf syntax using the percent (%) sign as follows:

puts "ABC %2.2f" % 3.567
=> "ABC 3.57
"

puts "ABC %2.2f DEF %5s" % [3.567, "abcd"]
=>
"ABC 3.57 DEF abcd"



Saturday, February 16, 2008

Load Balance Rails Mongrel Clusters With Apache

A simple solution for load balancing a Ruby on Rails Mongrel cluster with an Apache front end is to use the randomizing feature in Apache module mod_apache’s rewrite map feature. Say, for instance, you have 3 Mongrel servers, running on ports 4000 to 4002 on localhost. First create a file map.txt containing these numbers:

ports 4000|4001|4002


Then ensure the following directives are present inside the VirtualHost stanza in your Apache configuration:

ProxyRequests Off
ProxyPassReverse / http://localhost:4000/
ProxyPassReverse / http://localhost:4001/
ProxyPassReverse / http://localhost:4002/
ProxyPreserveHost On
RewriteEngine On
RewriteMap servers rnd:/path/to/your/map.txt
RewriteRule ^/(images|stylesheets|javascripts)/?(.*) $0 [L]
RewriteRule ^/(.*)$ http://localhost:${servers:ports}/$1 [P,L]

Restart Apache, and that is it.

I have had great success using both the Pound load balancer, and Nginx, which I suggest highly, but Apache does a great job when necessary.