Fork me on GitHub

Zeke Sikelianos

Mumblings from the code swamp. Find my more interesting online-ness at zeke.sikelianos.com.

A new twist on 'link_to_unless_current'

I’ve been thinking it would be nice to have a smarter alternative to Rails’ link_to_unless_current view helper: rather than being relegated to plain old text, the active link should remain a link and get a CSS class of active tacked onto it, so it can be styled to stand out from its siblings. It’s more intuitive for a link to always be a link, even if it’s a link to the current page. Dig?

At first I tried to do some alias_method magic to override the link_to function, but in the end I think naming the method link is not only shorter but safer!


Request methods in Rails

I seem to have a hard time remembering all the request methods in Rails. For anyone else who has the same problem, you might wanna bookmark the Rails API page on Class ActionController::AbstractRequest

And here’s a handy helper method for getting your application’s base URL:


Specifying CSS Class for a Textile list

A nice little trick I learned today. Works for ordered lists too..


Manipulating Checkboxes with jQuery

When selecting elements with jQuery, you can’t simply append .checked to a checkbox element to determine whether or not its checked; you have to use the attr array or the is expression evaluator, like so..

Find more info on this at Electric Toolbox


jQuery fadeToggle() Function

This little function works just like jQuery’s built-in slideToggle(). It’s a nice alternative to the toggle(‘normal’) function, which expands and collapses elements by squishing/stretching them horizontally and vertically.



A Nice Little View Helper for Generating List Items

Give this helper an array, and get back a string of <li> elements. The first item gets a class of first and the last, well.. last. This makes it easier to apply CSS styles to lists, be they ordered or unordered.


[Flash 9 is required to listen to audio.]

Creating Polymorphic Columns in a Rails (2+) Migration

This is a small convenience, but I just discovered it so I thought I’d share. The following migration will add an attachment_id column and a string attachment_type column with a default value of ‘Photo’.

You can also use belongs_to instead of references if that seems more intuitive; It will have the same effect. For more info see the Rails Guide on Migrations or this slightly more in-depth post.


Submitting a Rails Ajax form with Javascript or a Link

When using remote_form_for or its relatives, it won’t work to use javascript can’t call .submit() on the form; that will treat is a regular form submit. The following is an example of what not to do. (By the way, I use HAML and jQuery in my code samples.)

And here’s the workaround I’m using..


Recent Gems and Plugins I've Authored

Monkey Patches is a ruby gem that extends String, Arrary, and Object to provide handy methods like those found in ActiveSupport::CoreExtensions. Some goodies include truncate_preserving_words, ellipsize, permalinkify, replace_wonky_characters_with_ascii, remove_http_and_www, etc.

HAMLize Views is a ruby gem that uses the html2haml command line utility to convert rails’ ERB view files to HAML.

Textile Helpers provides some view helpers that enhance Textile support.

Flash Message Helpers automagically generates informative flash messages, and provides a view helper for displaying them.


ActiveRecord Associations: Count vs. Size vs. Length

Short answer: use size. It will give you a count of the objects without actually loading them, and it’s smart enough to avoid hitting the database if the association has already been loaded. Furthermore, it picks up on counter caches when you follow naming conventions. More info at Robot Has No Heart.


How to Validate Rails' Ajax Forms with Javascript

When using Rails’ Ajax helpers like remote_form_for and form_remote_tag, I like to do Javascript validation on the user’s input before submitting the form. This makes it easier to avert issues before they happen, and takes stress off the server.

Getting this to work is a little hacky, though. The trick is to put the call to the validating javascript function in the :before callback, and return false if the javascript function returns false. Simply returning true or false from within the javascript function is not enough. Also, don’t forget to return true within your javascript function if validation passes.

Thanks to In Eighty Days for this tip.


“You are not here merely to make a living. You are here in order to allow the world to live more amply, with greater vision, with a finer spirit of hope and achievement. You are here to enrich the world, and you impoverish yourself if you forget the errand.” Woodrow Wilson

Page 2 of 2