Provide valid 301 redirects using Rails routes.rb

I recently needed to handle some URL changes in a Rails application, basically modifying an existing route to point somewhere else. I wanted to make sure that the old route still existed so that users who might rely on it would not get left behind, not to mention search indexes that might have picked up the old location. I used the method outlined by Andrew Bruce, then tweaked it to make sure that params that were being passed by the user remained intact.

Basically you can follow his technique and use the code below in the controller.

Pretty simple, and very effective when you need to move things around but still provide access to the deprecated URLs.

Use Ruby to Truncate a String to Display Context Around a Search Term (plus term highlighting)

The following code will allow you to pull a search term and its context out of a larger string. It can be modified to use offsets as the start/finish points for the term rather than looking for the term itself. I'll end up doing this myself as the actual application this will be used in is semantic-based and the term a user searches for could have synonyms or relationships to other words which will also produce results. In this case it would be dangerous to highlight the original search term.

Invoking Thickbox from Javascript

I recently needed to use Thickbox functionality in a place where I wanted the Thickbox to appear without user interaction. This was mainly to keep the appearance of the dialog in line with what we use elsewhere. To do this, it's possible to create a url that will never be called and use Thickbox's built-in functionality for handling inline content.

The variable should store a url just as you would normally create for use with Thickbox links. This can include various options, including height, width, modal state, and the id for the div that holds the inline content.

The first step was to create the HTML portion that would be displayed in the modal Thickbox. For some reason Thickbox wouldn't display the text unless it was contained within paragraph elements.

Next we can use jQuery to create an event trigger for after the document is ready. I found that the Thickbox invocation didn't work, at least not reliably, without waiting for the document ready event, which makes sense if the DOM hadn't been built out yet.

Pretty simple, and very effective for my needs.