Monday, 4 June 2007

How to Automatically Link URLs in Rails

Let’s say you have a Rails app and you’d like any URLs in any plain text string to be automatically hyperlinked. Well, then you’ll need this:
.gsub(/((http|https)://[a-z0-9]+([-.]{1}[a-z0-9]+)*.[a-z]{2,5}(([0-9]{1,5})?/.*)?)/, '<a href='1'>1</a>')

For example:
I blog at http://garrickvanburen.com

Through:
"I blog at http://garrickvanburen.com".description.gsub(/((http|https)://[a-z0-9]+([-.]{1}[a-z0-9]+)*.[a-z]{2,5}(([0-9]{1,5})?/.*)?)/, '<a href='1'>1</a>')

Results in:
I blog at http://garrickvanburen.com

Thursday, 17 May 2007

Rails: Cookies, Frozen Gems, and TextDrive

Jon Steinhorst once told me a movies are rewritten from script to shoot, and again from shoot to editing. I spent a good chunk of this week re-writing a Ruby on Rails app to go from development to production.

Works fine in development. Not at all in production.

The issue was a combination of frozen gems and cookies.

First off, while TextDrive recommends depending on your vendor directory for any Ruby gems unique to your project, I found success with Geoffrey Grosenbach‘s Freeze Other Gems Rake task that sends them to /lib

Secondly, I wasn’t able to reliably set and retrieve cookies. Turns out I needed to access the cookie with a string, not a symbol.

All the resources I found say this should work in the controller
cookies[:the_timezone]

But it doesn’t, this does:
cookies['the_timezone']

Then in the view (specifically a select menu) I need to use this:
time_zone_options_for_select(cookies['the_timezone'].value,TZInfo::Timezone.us_zones,TZInfo::Timezone)

Friday, 13 October 2006

SSHKeychain.socket Error on Rake Remote:Setup

Still working through deploying a Rails app (if you’ve been following along, I got Subversion plugged in yesterday). Today was setting up the deployment by running:
rake remote:setup

The process kept erroring out saying:
No File or Directory - /tmp/501/SSHKeychain.socket

Turns out I had a bunch of cruft in ~/.MacOSX/properties.plist. After cutting out the offending xml and restarting. Everything worked as expected.

Wednesday, 11 October 2006

How To Set Up Subversion, svnX, for TextDrive on OS X

I’m walking through the latest beta version of Agile Web Development with Rails 2nd Edition, specifically the new bits on deploying Rails apps. Capistrano – the preferred and recommended deployment utility. Thing is – Capistrano hinges on Subversion. Not something covered in AWDwR or appropriate to be covered. There’s plenty of other books on the subject.

Though, that left me stuck on how to set up a Subversion repository and access it.

I do my hosting at TextDrive, and in the interest of making this process just a hair simpler, I set up my svn repository there. Here’s how I did it:

  1. Set up repository on TextDrive domain in webmin.[your-server].textdrive.com:80.
    There’s a ‘subversion repository’ menu option right when you sign-in.
  2. Assign svn access rights to a TextDrive domain user.
    It’s a radio button and select list selection in their profile.
  3. Install Subversion on your local Mac – I did via macports
  4. Install svnX and in svnX preferences, confirm your path to svn in svnX (macports uses /opt/local/bin)
  5. Import your project into the TextDrive repository
    svn import /Users/your/local/path/to/the/app http://[your-domain]/svn/[repository-name]/ -m 'initial import' --username=[your-svn-account-name]
  6. Connect svnX to the repository
    Path: http://[your-domain]/svn/[your-repository-name]
    User: [your-svn-user-name]
    Pass: [your-svn-user-name-pass]
  7. Check out your project back on to your mac by clicking the ‘svn checkoutexport‘ button and picking a directory to put it (not your original directory). I created an /svn directory within my main project directory.
  8. Now, if everything worked, double-clicking the repository should load it up in a new window.
  9. I’ve got a weird proxy between me and the internet right now, so I’ll make sure the commits work tomorrow. UPDATE: Yep, it works. Yah!

Thursday, 5 October 2006

Introducing the HijackingWP Script

One of my biggest problems with podcasting is the production process. Even without editing the audio, the process is far too manual to repeat without inherent discouragement (65 podcasts in year 1 and 20 in year 2 should speak to that).

In an effort to publish more and make podcasting as effortless as writing this post, I spent a couple hours last night writing the HijackingWP script.

It’s a little Applescript glue to connect AudioHijackPro (recording), Transmit (uploading), and WordPress (publishing and distribution).

More info at the HijackingWP page.

Oh, and yes, First Crack 86 proves it works. 😉

Thursday, 20 July 2006

Using Names in Rails Routes Instead of IDs

I spent tonight cranking through some Ruby on Rails has_many :through association oddities and after pounding my head against the keyboard, I decided to shift gears and figure out Ruby On Rails Routes.

By default, the Rails expects ID to be passed in URL strings. But that’s really lame, and passing words is much cooler so, how do you do that?

Turns out, it’s so simple no one talks about it (vs. so hard no one’s figured it out).

Take the following example:
http://mydomain/friend/show/1

What’s ‘show’? Who’s friend 1? Don’t they have a name?
Sure they do. Let’s say their name is ‘Wooster’

Here’s how to make turn the above url string into:
http://mydomain/friend/wooster

  1. Get the name of the database column storing your friends’ names (let’s say it’s ‘name’).
  2. In config/routes.rb add, somewhere above the default route:
    map.connect 'friend/:name', :controller => 'friends', :action => 'show'
  3. Now, in friends_controller, find def show and change it to:
    @friend = Friend.find_by_name(params[:name])
  4. Lastly, all the id-based urls pointing to our good friend Wooster need to be updated to reflect the name-based change. Like the one your Friends list.rhtml file.
    'show', :name => friend.name

Monday, 23 January 2006

Anyone Know How to Read the Treo PhoneCallDB.pdb?

Before I got the Treo, I had a SonyEricsson that worked real nicely with BluePhoneElite and my PowerBook.

Sure, Caller ID and answering the phone from the laptop was nice, but what I really miss is the integration w/ iCal.

Since the Treo keeps a record of the phone calls and their duration and backs it up to my PowerBook on sync, it seems I could read the PhoneCallDB file and send the data to iCal using AppleScript.

Flixton Software’s Treo Call Log can read the file, but I can’t do anything with the data. So it doesn’t really help.

Anyone know where I can find some documentation on how to read PhoneCallDB.pdb so I can use the data?

Thanks.

Thursday, 22 September 2005

Locally Running Multiple Rails Apps on OS X

There’s a couple of place describing how to support multiple Rails apps locally. They were either unavailable or way more complicated than I’d like (the HowtoDeployMoreThanOneRailsAppOnOneMachine at the RubyonRails wiki was both). Here’s how I was able to get multiple Rails apps running under Apache on OS X 10.4 Tiger.

  1. Created 2 Rails apps; AppOne and AppTwo
  2. In each of the apps’ public/index.html file I changed “Welcome to Ruby on Rails” to “Welcome to AppOne” and “Welcome to AppTwo” respectively. (You don’t need to do this, though it did seem to be the easiest way to see when it works.)
  3. Opened up and unlocked NetInfoManager, duplicated machines > localhost twice, renamed one “appone” and the other “apptwo”.
  4. Opened up Apache’s httpd/httpd.conf file, uncommented NameVirtualHost *:80 and added the VirtualHost blocks.

    NameVirtualHost *:80

    <virtualhost *>
    ServerName AppOne
    DocumentRoot /Users/garrickvanburen/Rails/AppOne/public/
    <directory /Users/garrickvanburen/Rails/AppOne/public/>
    Options ExecCGI FollowSymLinks
    AllowOverride all
    Allow from all
    Order allow,deny
    </virtualhost>


    <virtualhost *>
    ServerName AppTwo
    DocumentRoot /Users/garrickvanburen/Rails/AppTwo/public/
    <directory /Users/garrickvanburen/Rails/AppTwo/public/>
    Options ExecCGI FollowSymLinks
    AllowOverride all
    Allow from all
    Order allow,deny
    </virtualhost>

  5. Restarted Apache > sudo apachectl graceful
  6. Opened up two browers; one to http://appone/ and the other to http://apptwo/
  7. Clapped and victoriously declared “Yea” to an empty room.

As I should have known, this blew up my non-Rails localhosting – specifically phpMyAdmin. Repeating the steps above for phpmyadmin returned access to my database.

Thursday, 18 August 2005

What’s Your Favorite Open Source Project?

Hello gentle reader, I’m looking for your favorite open source projects; applications, websites, services. If the code is available for free and you’re a big fan, throw them in the comments.

    I’ll start:
    WordPress – weblog engine and content management system
    Ruby on Rails – web application framework
    CocoaMySQL – OS X desktop MySQL database manager
    Adium – Multi-protocol Instant Messaging client

Saturday, 14 May 2005

Setting up a Web Development Environment in Tiger

This message means the upgrade is complete.

Last week I started the upgrade Mac OS X 10.4 “Tiger” around the house. To my pleasant surprise, it went extremely smoothly. The most tedious and frustrating part was waiting for my newly enclosed external hard drive to copy tens of gigs of files back and forth for 2 machines.

The upgrade was an excellent opportunity to clean house and back up. Something my Powerbook was sorely in need of. After the install, the ease of copying my Home directory and a handful of required applications (NetNewsWire, Transmit, VooDooPad, BluePhoneElite, Skype, SubEthaEdit, Quicksilver) back to the laptop meant I was 85% back to normal almost immediately.

The tough part was getting the web development playground set up; I’ve still got a week left in the 21 days of Ruby, and I’m lost without a local install of PHP.

After un-commenting the PHP modules in Tiger’s default Apache install and setting the permissions on the items within the /WebServer/Documents directory to 666, PHP was working as expected.

After that, Ruby, Rails, and MySQL. For this, I highly recommend TextDrive’s About “Setting up a development environment on my Mac”. It walked me through everything and like everything at TextDrive – straight-forward and friendly. As of this writing, some of the items are specific to 10.3 “Panther” and I was able to skip over those with no consequence. Without it, I’d still be googling for a good tutorial on setting everything up. Now, I’m ready to rebuild my seemingly broken WishRSS.