giant robots smashing into other giant robots

Written by thoughtbot

dancroak

Wildcards in Rails redirects

The captured wildcard in a Rails 3 route can be used in the redirect method:

match 'via/:source' => redirect('/?utm_source=%{source}')

This example is intended to improve metrics for customer acquisition campaigns. utm_source is for Google Analytics, which KISSMetrics logs as Ad Campaign Hits.

The URLs are now friendlier for sharing:

/via/email
/via/twitter
/via/search-ads
/via/blog-ads

When the user clicks them, they’ll be redirected to:

/?utm_source=email
/?utm_source=twitter
/?utm_source=search-ads
/?utm_source=blog-ads

The URLs are also encapsulated. If the Google Analytics params need to change, the developer can edit config/routes.rb and deploy. All past routes will still work.

Written by .

dancroak

Convert Ruby 1.8 to 1.9 hash syntax

In vim, for an entire file:

:%s/:\([^ ]*\)\(\s*\)=>/\1:/g

In the shell, for an entire project:

perl -pi -e 's/:([\w\d_]+)(\s*)=>/\1:/g' **/*.rb

Now, instead of those old-school hashes like this:

get '/', :agent => MOBILE_BROWSERS do

You’ll have new-school hashes like this:

get '/', agent: MOBILE_BROWSERS do

Written by .