Internet Explorer frustrates the living hell out of me. Event though IE 7 is finally out, it doesn’t mean that IE 6 can be ignored. I really look forward to the day when my CSS(cascading style sheet) files aren’t full of unnecessary classes and hacks because the major browser vendors got it right and because everyone has casted away the garbage. Until then, I am still in search of tricks to simplifying my life.

One of these tricks is to make IE pretend like it knows how to use the :hover pseudo-class on all tags.

If you have ever tried to do a suckerfish dropdown then you have gone through the same thing: excitement as you have learned how to use structural markup to create a dropdown and then frustration as your work has been nullified by IE. The fix involves creating duplicate selectors of those using :hover, but instead of using :hover, replace it with a class name .hover. Making what are essentially duplicates and managing more content doesn’t sit well with me. The other trick to getting suckerfish to work in IE is to use JavaScript. The script will add a class definition to the tag when a user’s mouse if over the element and then removes the class when the users mouse is no longer over it. Since this method uses JavaScript to solve one problem, why not make it solve both?

Read the rest of this entry »

Nobody likes to see exceptions when running an application; it means something went wrong and you should have been on top of it. But with that, often times I find people will write code to prevent exceptions rather than just allowing them to occur and then using it in their favor.

Take this bit of ruby code in a rails controller as an example:

class ArticlesController < ApplicationController
  def show
    @article = Article.find(params[:id])
  end
end

That’s pretty solid, but what happens if someone passes in an ID that does not exist? Well in rails’ production mode, it would return a 404 error

Makes sense! That article doesn’t exist, so it can’t be found. I have dabbled with a little SEO(Search Engine Optimization) and usability and just a 404 isn’t cutting it for me. I think we can redirect our user back on a right track. For one, we know the user is trying to read an article, so why not redirect the user to a list of all the articles and give them a little warning?

Read the rest of this entry »

5

DRYing Up respond_to

Posted in Ruby on Rails at November 26th, 2006 / 5 Comments »

Now that that release candidate for rails 1.2 is out, people are going to have even more reasons to use the respond_to method in their controller methods. With that, there may come some unsettling feeling that you’re just repeating yourself over and over again.

def index
  @articles = Article.find(:all)
  respond_to do |format|
    format.html
    format.xml { render :xml => @articles.to_xml }
  end
end

def show
  @article = Article.find(params[:id])
  respond_to do |format|
    format.html
    format.xml { render :xml => @article.to_xml }
  end
end

If we want to use the respond_to, we have to make sure it’s in all of our methods. Wouldn’t it be easier to just do something like this?

def index
  @articles = Article.find(:all)
  format.html
  format.xml { render :xml => @articles.to_xml }
end

Doing it this way, we don’t have to write 2 extra lines per method, and we don’t increase our indentation. While that’s not a huge thing, I still just don’t like to constantly write out those lines when I don’t have to when I think we can build something a little trickier. Read the rest of this entry »

Expression is Microsoft’s suite of web development tools slated to replace the wonderful application known as Front Page. A quick visit to the site for this tool yields a fairly typical Microsoft webpage.

The “web” edition of this tool makes some hefty claims concerning creating valid xhtml/css based layouts. A normal person would reasonably expect a webpage promoting a tool used for webpage creation would most likely be built with the said tool. This would demonstrate the level of quality, the level of EXCELLENCE made possible by purchasing and using it.

Let’s have a look…

First we use a little browser called Firefox and a plugin called web developer to examine the layout of this page. By turning off CSS rendering we can examine the structure and determine how the creators approached the design process.

We clearly see that the developers of the expression site have chosen to use a table based, transitional approach (tables for the main layout, css to move things around within the columns). Well ok, I would have expected them to show off the “css layout” capabilities of the tool a bit more, but this in and of itself is still an acceptable practice.

With Expression web you can: “Validate your site with Compatibility reporting and use the Accessibility report to verify your site against Section 508 and W3C Content Accessibility Guidelines (WCAG).”

Well ok – let’s do some of that to the Expression Web “features” page and see what we get! (using Firefox’s handy developer tools of course).

WHOA! Did they not even listen to their own marketing garbage? 144 Errors! No DocType? Are you kidding me?

The validation report is littered with opening tags that are never closed, closing tags that were never opened, several tags that are not part of ANY of the w3 HTML specifications, dozens of properties on tags that are also not part of any specification, Don’t believe me – see for yourself

Bravo to our good friends at Microsoft for setting such a great example and leading the masses to a more standards compliant internet! (and for giving web standards geeks something to hate on)

This is one of those things that is probably obvious to some, and just not known to some. I was in that latter description up until a few minutes ago. It’s that you can easily validate any belongs_to relationship by simply using valdiates_presence_of

.

class Comment < ActiveRecord::Base
  belongs_to :article

  validates_presence_of :article
end

If you instantiate a new Comment, and don’t assign an Article to the comment instance, the record won’t save. I have to say, that seemed pretty obvious. But there is another interesting piece to this. Say I passed the article_id in the params and tried to assign that to our instance

comment = Comment.new
comment.article_id = params[:article_id]

If article_id corresponds to a real article in our database, then comment.article will have be assigned an Article with that id. Now if params[:article_id] happens to be some id which doesn’t exist, like 99999999999, then comment.article will be nil and thus the record fails validation. This means that you don’t have to test for the existence of the Article against your records because rails will do this for you when it checks to see if comment.article is nil or not.

3

Engrish Javascript

Posted in JavaScript at October 20th, 2006 / 3 Comments »

In what I can think of as the most important JavaScript that I have ever written, I have made something which will parse the DOM(Document Object Model) and replace all of the ‘l’s with ‘r’s. This idea came about after watching Team America: World Police about 1,000 times and after Kim Jong Il gave his apology for doing some nuke testing. Back to the script:

function engrish(n) {
	if(n.nodeType == document.TEXT_NODE) {
		n.nodeValue = n.nodeValue.replace(/l/g, 'r').replace(/L/g, 'R');
	} else if(n.hasChildNodes()) {
		for(var i=0; i<n.childNodes.length; i++) {
			engrish(n.childNodes[i]);
		}
	}
}

To execute this code, simply call engrish(document.documentElement);. Using recursion this function goes through the DOM, finds text nodes and replaces the characters.

Read the rest of this entry »

This is a fairly simple little tidbit, but still useful. Sometimes I want to have a script for my rails application, like running some reports on the data. There are two basic ways to do this: write a task(rake), or create a new ruby script file, like in the scripts directory.

If you want to do the latter, then there are two lines you my want to put at the top of your script.

ENV['RAILS_ENV'] = ARGV.first || ENV['RAILS_ENV'] || 'development'
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")

The first line allows us to supply which environment we would like to run the script in. Say the script is called process.rb, we could call the script like ./process.rb production and now the environment is set to production.

The second line loads the environment file. Now all of rails, your models, libraries, and plugins are available to you.

5

Loading Fixtures in a Migration

Posted in Ruby on Rails at September 28th, 2006 / 5 Comments »

I love migrations I think they are one of the best things about rails. I love that I can count on any changes I’ve done in the database to be consistent across all other databases that I use. Love it.

Sometimes I find that I want pre-load my database with some data, like categories that I have. For testing purposes, I have already created this data in my fixture; now I just want to load that fixture into my database from a migration call.

I know that there is a rake task for this, so I looked into that code and came up with a little something.

Read the rest of this entry »

0

Sleeping Easier with Migrations

Posted in Ruby on Rails at September 28th, 2006 / No Comments »

As a rails user who loves migrations, I have noticed that all of the migrations are prefixed with three decimal places, like 001_create_sessions.rb or something of that nature. That got me thinking, what would happen after I had 999 migrations?

That sets of the paranoia level up because who knows, I may need 1001 migrations! So to settle this once and for all, I wrote a script that would generate 999 migrations for me. I ran the 1000 migration and then it created 1001_test1001.rb.

Good news! Rails doesn’t constrain its migrations to a 3 decimal place prefix. So pointless…

I love TextMate. I love being able to load in a directory as a project, like I do with a lot of my rails applications. When I’m using Finder to navigate my filesystem, I sometimes want to open a directory into TextMate from Finder, and a contextual menu would be perfect.

Using some knowledge of Automator, this can be done quite easily.

Read the rest of this entry »