Add Feed Discovery Links Easily

I'm working on discussion forums for NearbyGamers and I'm building the first feeds into the site. I worked up a clean way to add them from my controllers similar to my tidy stylesheets code. Here's how to do it.

In the <head> of your app/views/layouts/application.rhtml call the auto_discovery_link_tag to print the tags:

  <%- @feeds.each do |feed| -%>
    <%= auto_discovery_link_tag(:atom, *feed) %>
  <%- end -%>

In app/controllers/application.rb:

  def initialize
    @feeds = []
  end

  def add_feed title, options={}
    @feeds << [ { 
      :controller => self.controller_name, 
      :action => self.action_name, 
      :format => 'atom' 
    }.update(options), { :title => title } ] 
  end

And you're all set up. Where you want an action to present feeds, call add_feed. After title, it takes options for url construction.

Rails 1.2.1 Impression

I'm updating NearbyGamers to Rails 1.2.1. Nothing broke except my use of assert_tag in my tests; it's been long-regarded as squicky and has been replaced with assert_select. As I'm tidying up some deprecated code, it occurs to me that this makes for an interesting example of how I feel Rails is changing.

Rails is growing inwards and upwards, not just outwards. They're finding better, terser, more Rails-ish ways to express things. They're not piling on features, they're condensing. I've mentioned that this is what coding in Rails continually feels like: sometimes it just feels off even though it works and is nicer than other languages, and soon I realize a beautiful Right Way to do it.

Where Rails gained new features, the developers have redesigned functionality to make me think, "Wow, of course, that's obviously much nicer" and it does more because it's better-designed. Let me give you an example using assert_tag.

Barking Stapler

Barking Stapler logo
I've left Cambrian House to work full-time on my own company, Barking Stapler.

Over the next few months, I'm going to grow NearbyGamers into a business that can support me. Yes, I know it's not a pretty site. The graphic designer who did the Barking Stapler site, Gilles Maes, is now working on a facelift while I work on adding a great many features.

I expect entrepreneurship is going to be quite a ride, so I expect to start writing about it in addition to my usual technical posts. As usual, let me know if there's any topics you'd like me to cover.

GET and POST variable hashes in Ruby on Rails

In Rails, you access GET, POST, and routing variables through the params hash. This works almost all the time, except when you duplicate a variable name: routing overwrites GET overwrites POST.

For an app I'm working on I actually had to care where a variable comes from, so I dug for a while to find out how to access the raw hashes. It ain't pretty, but here it is in case anyone else ends up needing it:

get = CGIMethods.parse_query_paremeters(@request.query_string)
post = CGIMethods.parse_query_parameters(@request.raw_post) 
# you'd think you could use @request.query_parameters and
# @request.request_parameters, but they're update()d by route vars
route = @request.path_parameters

(Also, don't ask about this in #rubyonrails -- you'll just get lectured on how you don't really want to access the hashes, how you should rename all your variables and URLs, and how it simply isn't possible. This will be very frustrating and totally unproductive.)

Rails Makes Valid XHTML Easier

I'm working on a Rails site in my Copious Free Time and I wanted to share a little way that Ruby made my life easier. I'm making my pages valid XHTML 1.0 Transitional because it makes life easier to find bugs and it just feels good to know I'm meeting the spec.

The W3C Validator complained that I didn't have the rows and cols attributes on my <textarea> tags. My code for them looked like:

<%= text_area_tag :message, params[:message] %>

And I don't want to add the :size option because I use CSS to style all of them, it'd be confusing to see an unused size there. So I extended the text_area_tag method in my app/helpers/application_helper.rb to fill in a default:

module ApplicationHelper
  def text_area_tag(name, content=nil, options={})
    super(name, content, { :size => "40x10" }.update(options))

What does Ruby on Rails Have to Offer a "Java Guy"?

Alot....

I have been working as a "Java Guy" for roughly five years now. The majority of my work has been component / library development, and it has been enjoyable on the whole. However, I have also had a few unfortunate “Java web” development projects. These experiences have not been something I hope to repeat often. I am not going to re-describe the Java / J2EE deficiencies in the Web UI layer, but I will go over a few examples of how messy it can get and why Ruby on Rails makes me smile. :)

Tidy Stylesheets in Rails

It's very easy for a site's CSS to grow a single giant, brittle stylesheet. It becomes impossible to change anything because of bizarre interactions between elements, unexpected interactions, and simply because it's just too big for anyone to understand. Much of programming is managing complexity, and I'll share a nice technique in that vein.

I like to break down stylesheets so there's a site-wide stylesheet with global stylings like fonts, the site's template, and common elements. This is the file that metastasizes on you.

My solution is to break down stylesheets by controller and action, and Rails makes this quite easy:

# app/views/layout/application.rb
<%= stylesheet_link_tag *(['global'] + @stylesheets) %>
# app/controllers/application.rb
class ApplicationController < ActionController::Base
  before_filter :add_stylesheets

  def initialize
    @stylesheets = []
  end

  def add_stylesheets
    ["#{controller_name}/_controller", "#{controller_name}/#{action_name}"].each do |stylesheet|

Book review: Beginning Ajax with PHP by Lee Babin

Book Review
Apress: Beginning Ajax with PHP by Lee Babin

Book Site | Sample Chapter: 3 PHP and Ajax | Table of Contents

Although no stranger to Ajax, I received a review copy of Beginning Ajax with PHP expecting some watered down presentation of Javascript with some PHP thrown in. I was quite surprised to find a good presentation of using Ajax and PHP, easy enough for the beginner and still interesting for those who have done it for years.

The book starts out exactly how I would write it -- SIMPLE! The first time I did Ajax with XHR (xml http request), I used a plain text file, which I then read into a DIV at the click of a link. This takes a similar approach and has data stored in an array which is then accessed with a simple call to a PHP file. The following chapter, takes it a step further and this building upon previous chapters is a common theme in the book.

Book Review: PHP Hacks

Book Review: PHP Hacks by Jack D. Herrington, published by O'Reilly

I had borrowed a Perl Hacks book from and friend and really liked it, it was great! It had a lot of practical things as well as some fun things. I expected the same from PHP Hacks and I was not disappointed!

Here’s the table of contents: http://www.oreilly.com/catalog/phphks/toc.html

O’Reilly also has some sample hacks: http://www.oreilly.com/catalog/phphks/chapter/index.html

Here's some that I found interesting:

The Practical Stuff

Breadcrumbs
Not familiar with the term? Check it out. I think this hack may get you started on a breadcrumb function/method. In the end they suggest a xml file to show which page urls were parents of which. The way I've done this before was I had a class for each major section, and sub section, and had a method ->addCrumb($label, $url) which I had in the constructor…and the subsections of course would call the parent contructor and it kept the breadcrumb hierarchy intact. But hey, that’s the fun of programming - different ways to do the same thing to meet different needs! Definitely a good hack to get the juices flowing!

Just a bit of Chaos...

The server move is complete... keep an eye out for the news.

If you hadn't noticed, the site is acting up. After some downtime and a server move, things are still a bit screwy.