Apr 24, 2009

Quick Tip: Fix RSS feeds in Rails 2.3

I saw a sudden drop in the RSS subscribers on one of my site's feeds and it led me to investigate what happened. Turns out that RSS feeds changed every so slightly in Rails 2.3. Actually, it might even be a bug. The symptom is that when you visit your sites rss feed you are prompted to download the file instead of viewing it in an RSS program.


If you download the file and inspect it, you'll likely see a bunch of stuff that doesn't belong in an RSS feed at all. Even more confounding is that emptying out your index.rss.builder file still makes your browser download a file. The culprit is that Rails 2.3 shows the layout file for RSS feeds just like it would for a respond_to html. To fix it add a render_layout => false to your controller like so:

 def index
@articles = Article.find(:all, :limit => 8, :order => 'created_at desc')
respond_to do |format|
format.html
format.rss { render :layout => false}
format.xml { render :xml => @articles.to_xml }
end
end


Tada!

1 comment:

  1. If your view files are following the naming convention of title.mime_type.interpreter (Ex: index.html.erb), you shouldn't need to have a respond_to block in your methods. The format is gleaned from the file name.

    ReplyDelete