Archive for the Programming Category

Sortable Tables

Posted in Programming with tags on April 29, 2009 by dialgforgerald

Rails has a helper function called sortable_element that generates ajax code to allow you to sort lists by dragging and dropping elements. The function defaults to handle lists and if you want to sort tables you should take note of the :tag option.

<%= sortable_element 'my_list',
:url => { :action => "order" },       
:tag => 'tr' %>

This should allow you to sort tables and hopefully save you from a big headache.


Attachment Fu

Posted in Programming with tags , on June 12, 2008 by dialgforgerald

Attachment_fu is an awesome Rails plugin which would make your life easier by taking care of all of your file upload needs. It even creates thumbnails for images and handles validation for you. Sweet. I won’t be getting into tutorial mode here because Mike Clark did a very good job with that and you should drop by his site if you want to learn more about how to use the plugin. Instead here are some stuff I encountered while developing with attachment_fu.

If you are developing in Windows, you might encounter a weird bug where attachment_fu will complain that “Size not included in list” and when you check the file it has a size of 0. Apparently you have to put a sleep before saving the image. So instead of doing:

@mugshot =[:mugshot])

You should do:

@mugshot =[:mugshot])
sleep 5

Sleep 1 should be fine but I used 5 just to be sure. I think this applies to Windows only because I haven’t encountered it anywhere before.

Coding in PHP Again

Posted in Programming with tags , on May 28, 2008 by dialgforgerald

After making a conscious effort to learn RoR by using it to do projects for the past year, I find myself using PHP again for another project. It’s like seeing your old crush/girlfriend from high school, and she’s still hot maybe even hotter now, but you just can’t shake the feeling that you have something better now so you catch up, share a few laughs and you look back in the past, but that’s mostly it. It will never be the same again.

It’s not a knock against PHP that I mostly use RoR nowadays. I like the language itself and I still use it for most of the small scripts I make, but the PHP frameworks out there pale in comparison to RoR. So when Cake finally shapes up and starts using objects and stops supporting PHP4, I’m there.

Globalize on Rails

Posted in Programming with tags , on May 22, 2008 by dialgforgerald

I had to convert some dates to Spanish for a recent project I was doing. Ordinarily, I would track down every piece of date text I have and edit them with their Spanish counterparts. Unfortunately the dates were dynamic and I didn’t really want to do a very big if-else statement. Fortunately globalize was there to save the day. This is really an awesome plugin that I have just started using but it’s really useful. Here’s a great tutorial for the globalize plugin.

You may encounter an error for the error_messages_for helper but don’t panic because here’s the fix for that.


RedCloth Bug

Posted in Programming with tags , on April 29, 2008 by dialgforgerald

I just experienced this weird bug in RedCloth 3.0.4 where line breaks are not handled properly. I talked to a friend about it and found out that it was a known bug which, unfortunately, hasn’t been fixed yet. Anyway, you guys can try this patch until a new RedCloth version comes out which will hopefully fix this. Just change the hard_break function to:

text.gsub!( /(.)\n(?!\n|\Z| *([#*=]+(\s|$)|[{|]))/, "\\1<br />" ) if hard_breaks

And this is just another reason why it would be wise to freeze your gems.

Alphabar on Rails

Posted in Programming with tags , on April 14, 2008 by dialgforgerald

I am working on this little personal project right now using Ruby on Rails and I ran into some trouble with the alphabar plugin. First, alphabar uses the with_scope method which, since Rails 2.0 I believe, has been protected, giving me some errors since I froze my Rails version. I was able to overcome this obstacle by using the send method instead. Just change the last part of the find method of the plugin from:

model.with_scope({:find => {:conditions => conditions}})
{model.find :all}


model.send(:with_scope, {:find => {:conditions => conditions}})
{model.find :all}

After I finally got that to work the plugin was very useful, although the alphabar was limited to letters and blank which is useful if you don’t need numbers. To accomodate numbers just add this to the alphabar helper:

('0'..'9').to_a.each do |i|
slots << i

Theoretically it should work for any character but I haven’t tried it yet.

So there you have it. Hoepfully somebody having trouble with this plugin will find this post useful.