Monday, April 28, 2008

Hardy Heron Connection Problem

Just upgraded to Hardy two days ago. Is it nice? Yes. Is there any problems with it? Yes.

Installation was fast as usual, took around 20 mins. Video settings, sound card, network card ... are all setup and ready to go after the system rebooted itself. Firefox 3 looks better than ever, however, I can't seem to get a connection?!?

What's more weird is that I can actually goto google.com, but everything else will time out on me!
After hours of searching, some dude suggested that I should decrease the MTU (maximum transmission unit) from 1500 (standard for ethernet) to 1492 by typing the following command

sudo ifconfig eth0 mtu 1492

amazingly, my connection is back to normal! (if anyone know why that is, can you please educate me on this issue? I really like to know the reason behind this!)


J

Deployment: Part 2

As I mentioned in the last post, we got past SVN.

We finally came to Mongrel Cluster. This was a major headache. Apparently, Capistrano is broken when it comes to handling Mongrel Cluster. Simply following the Capistrano's site tutorial will not work.

The first step around this was to get the Palm Tree gem on the local machine:

>sudo gem install palmtree

This will include some recipes that we can use with our deploy.rb that you need to run Capistrano.

Next, add this to the top:

require 'palmtree/recipes/mongrel_cluster'

This will include the recipe needed to fix the deployment scripts. Basically, it rewrites the deploy script that comes with Capistrano. All you really need to know is that it works ;)

Next, generate a mongrel_cluster.yml by following the "Mongrel Cluster Setup" in this wiki. Check this into your repository as we'll need it.

Add this line to your deploy.rb:

set :mongrel_conf, "root_of_the_app/config/mongrel_cluster.yml"

Last thing we need to add is to tell Capistrano not to use sudo:

set :use_sudo, false

When all this is finally done, make sure that everything is in your repository. We can now attemtp a deploy:

cap deploy:cold

Make sure you use cold, as this should be the first time you're deploying. A deploy:cold is different than a simple deploy because it assumes that your servers are not running yet. Hopefully everything went right here. This should get you past Mongrel Cluster.

The next would be take a look at how to customize your deploy, such as starting other servers that you would potentially need.

We found the "after_stop", "after_start", and "after_restart" helpful for handling our other servers. Make sure that you put them in the deploy namespace ;)

Good luck!


W

Saturday, April 26, 2008

Deployment: Part 1

So we're nearing the stage of our deployment and decided to perform a dry run on J's spare computer. We installed the Ubuntu server on his machine, along with all the stuff we needed (SSH, Ruby, Rails, gems, etc.)

We then decided to go with Ubuntu server + Apache + Mongrel Cluster + Capistrano for an action-packed deployment. We decided on Ubuntu since we have had such a good impression with them and have had experience with troubleshooting on an Ubuntu machine. Apache + Mongrel Cluster seems to be a fast-growing standard, although there are many configurations to choose from. We had been doing research and found that this configuration would be the most painless. As well, this configuration is supposed to handle very well. We used Capistrano to help with our deployment and to make maintenance much easier when deploying on a remote server.

Basically, the whole setup revolved around Capistrano. So... off we went to figure out how to use it. This took us a very long time as we had to troubleshoot some problems.

The first problem that was not even mentioned on the Capistrano site was logging into the remote server with a particular username. In the examples, they simply say to just use:

role :app, my.server.com

However, this will log you into the remote server with the username you have with the local machine. This is not what we wanted, so we ended up having to attach the username in front:

role :app, username@my.server.com

Next, when the server was trying to checkout our repository from SVN, it crapped out. We added this to our deploy.rb to get around this:

default_run_options[:pty] => true

Now, our deploy ran fine, until we got to Mongrel Cluster T.T

Hopefully this will get you out of the rough, unless you're using Mongrel Cluster, which I'll cover in the next post.


W

Friday, April 25, 2008

IE Address Bar

This is really starting to annoy me. In FF, if you type in a URL in the address bar without the http:// and no www, it is smart enough to put them in for you.

However, in IE, without the www, it doesn't know how to prepend the http:// to your address. This gets really annoying when you're constantly trying to go to localhost or a specific IP address for testing.

Sorry for the rant...


W

Thursday, April 17, 2008

Responds_to_parent loc error

If the rjs inside your responds_to_parent block ended up removing the iframe from your html page, the loc variable will no longer be defined.

Besides having an ugly error message on your firebug console, it can actually cause problems for the javascripts on that page as well.

In order the get around this problem, you'll need to do the following patches:

in parent_selector_assertion.rb and responds_to_parent.rb

replace all occurrence of
(3 in parent_selector_assertion, 1 in responds_to_parent)
loc.replace('about:blank');

with
if (window.loc) { loc.replace('about:blank'); }
and that's it!


J

Pluralize

Have you ever had to write some sort of if/else condition to display a sentence involving an object count? Something like 1 person, 2 people, etc.

Well, RoR has a nifty little method called pluralize. All you do is feed it the number and a string, and it will attempt to figure out exactly what you want.

For example:

pluralize(1, 'person') # '1 person'
pluralize(2, 'person') # '2 people'

I thought this was pretty neat and wanted to share with you guys. There are a lot of little methods tucked away in the RoR API, and I encourage you to look through them to help minimize your development time on mundane things like this. Enjoy!


W

Wednesday, April 16, 2008

respond_to + render :partial

RoR has a very quick way to turn your web application into a web service that supports API hooks into your application. It does this using the respond_to method. It's as simple as doing something like:

respond_to do |format|
format.html # automatically calls the corresponding view
format.js #automatically calls the corresponding rjs
format.xml { render :xml => @user.to_xml # or other XML stuff }
end

It's that simple!

Well, there's one odd thing that got me for a little bit. I'm not sure if it's a bug, but when I tried to render a partial in the format.js block, I got an error that it could not find the corresponding template:

respond_to do |format|
format.js { render :partial => 'my_partial' }
end

I found that very odd, but not fixable... I tried adding '.html.erb' to the end of my_partial:

format.js { render :partial => 'my_partial.html.erb' }

And magic! It worked! I'm almost sure this isn't the way RoR is suppose to handle this, but in the meantime, it works! Small hack, but I'm happy :D If any one else runs into this problem, drop a comment, or even better, if you know why or how to solve this, let me know!


W

Tuesday, April 8, 2008

Relatively Absolute Positioning

So I came across a bug in our layout that was driving me nuts. I was trying to lay a div ontop of another div, so I was using relative positioning. Seems reasonable. However, when using relative positioning, it leaves a white gap where the div used to be.

I finally came across this nice little CSS tutorial about positioning and I learned something new. Apparently, you can have an outer div with relative positioning, and have the inner div absolutely positioned inside: a relatively absolute positioned div! Amazing! Since absolute positioning removes the div from the flow completely, no white gap is left behind!

One problem in IE though (as usual), is that anything with relative positioning inside the absolute div is covered up (doesn't show). FF is perfectly fine though (go figure...). I got around this problem by using margins instead.

You may probably know this, but for those that didn't... enjoy!


W

Friday, April 4, 2008

Gedit

So after playing around with Gedit for a while, I'm loving it. It's super fast and there are so many nice plugins to make it very similar to an IDE. There are some things that aren't present, such as refactoring, but that never worked properly in RadRails anyways!

Before, I mentioned that there was no SVN or debugging support. As I continue to develop using Gedit, I have not encountered any problems with this. I'm currently using SVN from the terminal and it's perfectly fine. Lucky I've had experience with using CVS in the terminal ;) Also, I installed a graphical diff program called Meld so that I don't have to look through the classic diff. I haven't had to do too much diffing, so I can't say too much about it.

As for debugging, I realized that for RadRails, I was already using ruby-debug, which runs straight off the terminal! So far, for what little debugging that I have had to do, I found the terminal easier to work with. It requires a bit more typing though (compared to the mouse clicking in RadRails). However, it works... unlike RadRails. The debugger in RadRails was very unpredictable and I could never access the variables without some sort of error popping up. It was so annoying! I'm quite happy with the terminal, so for now, I'm sticking with it.

Apparently, there are new plugins still being developed for Gedit as it gets more popular. Perhaps someone, in time, will come out with an SVN plugin or a debugger plugin. We'll just have to wait and see ;)

In the meantime, I'm super happy with my current setup. There are little things here and there that are bugging me, such as when I hit the home button, it jumps to the beginning of the line, rather than the beginning of the words. I'm hoping there's a configuration that I just haven't found yet.

After playing with it some more, I'll post up the plugins I have installed.


W

Thursday, April 3, 2008

Eager Loading + Order By

So I was trying to cut down on the number of queries that I was making on a particular page and ran into a slight problem.

Basically, I had a relationship where a User has many Notifications:

class User
has_many :notifications, :order => 'created_at DESC'
end

In my controller, I wanted to cut down on the queries, so I did some eager loading:

@user = User.find(user_id, :include => :notifications)

However, when I went to go view my page, the notifications were not in the order as I specified. I went to go take a look at the query, and lo-and-behold, there was no ORDER BY stated in the query.

After some thought, it became apparent why no ORDER BY was included. This makes sense because if you eager load many tables, each with its own ORDER BY, it would be difficult, if not impossible, to have everything returned properly.

I would assume, without much thought, that one would assume that this would work. With further thought though, this becomes the correct thing to do.

Just a heads up ;)


W

Tuesday, April 1, 2008

BackgrounDRb

There are often times when you want to perform scheduled tasks, such as email reminders. These tasks are usually triggered at a specific time, and not necessarily by a user action.

On a unix machine, cron jobs do exactly this. They are triggered at a particular time, do their work, then go away. I looked around for a bit to see if there was a rails plugin to handle jobs like these.

Behold: backgrounDRb. This is a rails plugin that runs its own server to do a multitude of jobs. It supports cron jobs, but at the same time, it handles jobs in the background. For instance, if you are generating a large document, you don't want the user to wait for a long time. Instead, it will spawn a job in the background, allow the user to continue doing his/her stuff, and periodically check when the job is done.

For my purposes, I only needed the simple ability to create cron jobs, so I have yet to experiment with the background tasks. However, I found it to be super easy to use and everything is very straightforward.

If you're looking for a scheduler type plugin, look no further. This baby is amazing!


W