4

DRYing Up respond_to

Posted in Ruby on Rails at November 26th, 2006 / 4 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.