Use Rails’ RESTful Helpers in Rake Tasks 1

Posted by Trey Connell on March 23, 2010

We were recently working on a rake task to generate an XML Sitemap of a site’s content for submission to the search engines. Because we are trying to remain as RESTful as possible, I wanted to take advantage of the helpers given to me automatically. For example, to generate the URL for an article, I simply have to do article_url(my_article). This would result in something like /articles/my-article-headline-is-cheesy.

I couldn’t figure out for the life of me how to make Rake aware of my routing and the RESTful helpers that I SHOULD be able to use to dry up my code and ensure very little manual maintenance when and if we decided to change our routing in the future. There had to be a way!

It turns out there indeed exists a way to make this happen.  You simply have to place “include ActionController::UrlWriter” at the top of your rake task.  Ultimately url_for is called when you do something like article_url(my_article), and url_for requires a host which it won’t have.  So change your code to read article_url(a, :host => “www.mysite.com”). Voila – route helpers inside your rake task.

Simple Captcha for Ruby on Rails

Posted by Trey Connell on February 01, 2010

ruby on railsI wanted to implement a simple captcha for for article comments on a site. And by simple, I mean both in aesthetics and in functionality. After looking around, I found this post that described how you could implement a plugin called Simple Captcha in 10 minutes or less.

The plugin requires RMagick and ImageMagick which takes a little while to install.  So if you don’t have those, then the title of the post probably won’t hold true for you.  After you have everything installed, it takes one line of code in your view and one line of code in your controller to implement the captcha.

Just what I was looking for!

Migrating from Java to Ruby on Rails

Posted by Trey Connell on October 23, 2009

java-versus-ruby-on-railsWe use Java as the foundation for our product framework at work. While we’ve always been pleased with Java compared to the P’s in LAMP (PHP and Perl), I’ve often wondered, “Is there an easier way? Do we really need all these pieces and parts that make up our stack? Can we build sites faster and with more predictable results?” While I think Java is a fantastic language, I think it tries to be all things to all people. We don’t need to write embedded systems. We don’t need to tie together disparate systems with middleware. We don’t need massive EJB deployments. We need to build feature-rich, dependable, scalable, and beautiful web sites. And we need to build them yesterday quickly.

Enter Ruby on Rails. I’ve been using Ruby on Rails outside of work for about a year now. The first thing I will say is that it’s FUN. At my age I don’t want to futz around with bits and bytes, memory allocation, and the like. Ruby on Rails seems to let me focus on building cool stuff and not on wrestling with the framework to bend it to my will.

The second thing I’ll say is it’s easy to get up and running and to deploy changes to your applications. We have a very intensive build and deploy process surrounding Java and our sites. Nothing makes me more frustrated than spending 30 seconds to fix a typo and having to go through a 15 minute deployment process. With Ruby on Rails, that pain point goes away since I can make changes to my code or my templates and simply refresh my browser to see those changes.

The last thing I’ll say is that I’ve been using Java for a long time and part of my infatuation with Ruby on Rails may simply be due to the fact that it’s new and exciting. Regardless, we’re going to continue to explore the Ruby on Rails framework for our next major iteration of our product. I think at the very least that it’s a good exercise, and we may just find that we need to take the plunge and move away from Java for our future web development efforts.

Tracks – GTD Systems in Ruby on Rails 2

Posted by Trey Connell on September 29, 2009

Tracks GTD Ruby on Rails

I’ve been using a free GTD system called Tracks for about a month now.  From their web site:

Tracks is a web-based application to help you implement David Allen’s Getting Things Done™ methodology. It was built using Ruby on Rails, and comes with a built-in webserver (WEBrick), so that you can run it on your own computer if you like. It can be run on any platform on which Ruby can be installed, including Mac OS X, Windows XP and Linux. Tracks is Open Source, free and licensed under the GNU GPL.

So far I’m really diggin’ it. I installed it on an Ubuntu server in my office so I can always access it from my laptops or my iPhone. The application is very, very straightforward and does a good job of giving me the tools I need and none of the tools that I don’t. The mobile version (included) is also implemented very well, allowing you to enter tasks and projects quickly from your phone.

If you’ve been looking for a simple task management system you can run locally and access from many devices, Tracks might just be for you.

TOG – Ruby on Rails Social Networking Plugins 1

Posted by Trey Connell on January 29, 2009

My movie site is coming along well so far. We’ve made lots of progess on all the core functionality you would expect to see:

  • Movie information
  • Reading movie reviews
  • Creating movie reviews
  • Seeing new members and their activity
  • Rating movies
  • Commenting on movie reviews by others
  • Weekly polls

I’m just about to start creating friendships on the site so that members can invite people to the site and request friendships from existing members. This seems easy enough to model – as does creating a “wall” where you can keep up with and communicate with your friends as well as allowing them to see all the movies you own and information of that sort. Before diving head-first into creation from the ground up, I went scouring the interwebs and ran across Tog.

Tog is a set of social networking plugins designed for sites / applications built in Ruby on Rails. Instead of trying to be a community in a box, the various plugins allow you to implement your site how you see fit and take advantage of the functionality provided by each plugin.

My goals in building the movie site are two-fold:

  • Get back to my development roots since my current career track doesn’t afford me the opportunity to crank out code and keep my chops solid
  • Learn a new language / platform / framework / development methodology

I haven’t yet decided if using third party plugins is a path I want to take on this path back to being a rockin code whore.  But if I do, Tog definitely looks promising.

Have any of you used Tog? What did you think? Are there other social networking plugins that you would recommend for a guy cutting his teeth on Ruby on Rails?

Fixtures and Model Associations in Ruby on Rails 1

Posted by Trey Connell on January 19, 2009

I’m forcing myself to write tests for all my development on the movie site.  (We still haven’t chosen a final name for it – lame…)  Once of the things I love the most in Rails is Fixtures.

Fixtures allow you to setup your test data in YAML or CSV.  That data is then loaded when you run your functional, unit, and integration tests.  You can also just load the data outside of any testing effort by running ‘rake db:fixtures:load’.  I can’t even tell you how handy that is.  I can populate my development site with data and starting banging around on pages that actually work – with real data – in the blink of an eye.  Got a bug?  Fix it and reload.  It’s niiiice.

As I started building my fixtures, I started with the easy models first.  Movie is an obvious choice.  This is my movies.yml file:

movie1:
    title: The Dark Knight
    director: Christopher Nolan
    release_date: 2008-07-18
    description: Batman Gordon and Harvey Dent are forced to deal with the chaos
    starring: Christian Bale
    rating: PG-13
    product_url: http://www.amazon.com
    thumbnail_path: /images/movies/thumbs/the-dark-knight.jpg
    image_path: /images/movies/full_size/the-dark-knight.jpg
    number_of_reviews: 5
    avg_rating: 8.8
    featured: 1
    permalink: the-dark-knight

movie2:
    title: Tropic Thunder
    director: Ben Stiller
    release_date: 2008-08-13
    description: Goofy movie with lots of bid-budget actors
    starring: Ben Stiller, Robert Downey Jr., Jack Black
    rating: R
    product_url: http://www.amazon.com
    thumbnail_path: /images/movies/thumbs/tropic-thunder.jpg
    image_path: /images/movies/full_size/tropic-thunder.jpg
    number_of_reviews: 10
    avg_rating: 7.5
    permalink: tropic-thunder

Now I have two movies that I can load at any time.  But wait.  My site is also going to have Movie Reviews in it.  Each review be associated with one and only one Movie.  In my Movie Review model, that relationship is established by the belongs_to :movie statement at the top of the class.  I also have a has_many :movie_reviews statement at the top of my Movie class.

MovieReview has a movie_id column that references the Movie it belongs to.  But how can I represent that in my movie_reviews.yml file where I’m entering data?  I won’t know the movie_id assigned to my new movies, and I don’t want to have to hardcode it into my movie_reviews.yml records anyway.  Enter more Rails magical deliciousness.

Because of the relationship setup in the models, Rails let’s you refer to the record names that you’ve specified in order to relate certain data rows across tables.  Here’s my movie_reviews.yml file:

review1:
    title: My great movie review
    movie: movie1
    rating: 7
    review: NO SPOILERS. I saw an advanced showing of this 2 nights before it came out.
    found_useful: 5

review2:
    title: Tropic Blunder
    movie: movie2
    rating: 5
    review: Great Premise! Started out hilarious...funny 1st half...Robert Downey was great!
    found_useful: 2

Notice I have specified the movie that each relates to by specifying movie: movie1 and movie: movie2 in each record.  Those movie names are the same that I specified in movies.yml.  When I run rake db:fixtures:load, I see that the movie_id column in the movie_reviews table has been populated with the correct movie_id from the movies table in each of the two rows inserted.

How cool is that??

Add Permalinks to Ruby on Rails Sites for Search Engine Friendly URLs

Posted by Trey Connell on January 19, 2009

While building the movie site in my spare time, I came across the need to implement permalinks for the different types of content – users, movies, reviews, etc.  I want the site to have clean, search engine friendly URLs.  After search around, I found a nifty plugin called permalink_fu.

This rails plugin allows you to specify any field in a model that a permalink should be created from and then store that value in a new column. For example, in my Movie model, I specified that the permalink would be created from the title field and stored in the permalink column. You can then modify your routing appropriately to use the new permalinks.

For information on how to install and use this plugin, read this article on seoonrails.com.

Ruby on Rails Build Environment

Posted by Trey Connell on January 07, 2009

In a previous post I talked about the movies web site I’m building in Ruby on Rails with MySQL as the backend database. A buddy of mine is now helping me build the site, and it is coming along quite nicely. I continue to be amazed at how easy it is to get things done and pump out code and features.

So far our environment looks like:

  1. Ubuntu 8.04.1
  2. nginx 0.5.33
  3. Rails 2.2.2
  4. Ruby 1.8.6
  5. MySQL 5.0.51a

We’re using WEBrick in development, but in production we’ll likely switch over to a combination of:

  1. nginx for static files (css, images, js, etc)
  2. Apache with modrails for the backend dynamic stuff
  3. Ruby Enterprise Edition for scalability

I’ll post some updates as we move along and find anything worth mentioning.

Movie Site with Ruby on Rails 1

Posted by Trey Connell on August 21, 2008

In order to learn and play with Ruby on Rails, I’m building a movie site where people will be able to talk about the movies they’ve seen and want to see, find others who like the same kind of movies they do, discuss movies with friends, and write reviews.

I anticipate this site is going to take me 6 to 9 months to build. I have several factors working against me:

  1. I have never built a web site in Ruby – on or off Rails
  2. I have a daughter, Addison, on the way in 7 weeks
  3. My job keeps me hella busy already so my free time is usually spent doing something around the house

On the other hand, I kick ass so this probably won’t be bad at all.