Demystifying resource_feeder for RSS feeds – polymorphic URLs

Plugins, Ruby on Rails Add comments

If you’re here, then you’re trying to figure out how to serve out an RSS or Atom feed from your Ruby on Rails application. You’ve probably already read a post by Ryan Daigle covering the basics of using resource_feeder to do exactly what you want. After all, Ryan’s post is the #1 hit on Google for resource_feeder.

Unfortunately, there are a few extremely important points that are curiously absent from the posting regarding polymorphic URLs. For those that don’t know, polymorphic URLs are…well I don’t know either. All I know is that it has something to do with the routes.rb file and named routes. For my rails project, I luckily (by accident) had one set up for the first RSS feed I tried to create. For the second, no such polymorphic URL existed, causing it all to come crashing down.

The problem is due to the fact that each item in the feed is expected to have a URL. If you’re lucky enough to have set up your routes correctly, the URL will be automagically determined. If not, then you get a cryptic error about polymorphic URLs. There may be a way to create a URL to each item in the feed, but resource_feeder simply cannot figure it out automagically.

Luckily, resource_feeder was designed to allow you to skip polymorphic URLs until you can figure out what the heck they are and how they work. If that’s the boat you’re in, then read on and I’ll explain.

1. Read the howto on Ryan’s Scraps

Ryan does a great job of explaining the main concept of resource_feeder. I even borrowed his example model object, Post. What I cover here are just the frustratingly absent portions that bugged me.

2. Get the plugins

script/plugin install http://dev.rubyonrails.org/svn/rails/plugins/simply_helpful/
script/plugin install http://dev.rubyonrails.org/svn/rails/plugins/resource_feeder/

Of course, we here at Midnight Oil always prefer explicit vendor branches over the basic plugin installation. Spend 10 minutes today and save countless tomorrow.

3. Add the action to your controller

Using the standard blog post example for this howto, here’s how you would add an RSS feed for your blog postings:

class PostsController < ApplicationController
def rss
render_rss_feed_for Post.find(:all, :o rder => ‘created_at DESC’, :limit => 10)
end
end

However, this assumes that you’ve got a title and description field in your model, and you also have a polymorphic URL for Post objects in your routes.rb file. If you’re like me, you might have one or two of these, but not all, and especially not the route. It’s something on my list to add tomorrow ;)

3.5 Add the real action to your controller

class PostsController < ApplicationController
def rss
render_rss_feed_for Post.find(:all, :o rder => ‘created_at DESC’, :limit => 10), {
:feed => {
:title => ‘Recent Posts’,
:link => url_for(:controller => ‘posts’, :action => ‘list’, :o nly_path => false),
:pub_date => :updated_at
},
:item => {
:title => :name,
:description => :tagline,
:link => Proc.new {|post| url_for(:controller => ‘post’, :action => ’show’, :id => post.id)}
}
}
end
end

Note that it’s not too different from the original example, but it has fleshed out a few key points. The true magic happens in the :link part of the :item hash. resource_feeder will accept a Proc object as the value for :link. This Proc object is passed the “resource” in question when it is called. The resource passed is the model object being rendered as the item for the RSS feed, in other words, the items in the list passed as the first argument to render_rss_feed_for. In our example, each “resource” is a Post object. With our Post object in hand, it’s quite easy to create a URL using url_for.

As to the other items, this example assumes that you have :name instead of :title and :tagline instead of :description for your Post objects.

4. Have fun!

At this point, you should be able to create your RSS feed without worrying about polymorphic URLs or any such business. They’re probably super-easy to create, much easier than what I’ve described, but I just don’t know.

Also, this should all be applicable to the Atom feeds as well. I just used RSS as my example, since Ryan already covered the overlap between RSS and Atom as far as resource_feeder is concerned.

5 Responses to “Demystifying resource_feeder for RSS feeds – polymorphic URLs”

  1. Chris Says:

    Hi,

    I have used your example but I get a syntax error?

    Please help!

    Chris.

  2. Chris Says:

    app/controllers/posts_controller.rb:13: syntax error, unexpected ‘)’, expecting tASSOC

    thats what i get when I do copy and paste of what you have above.

  3. Micah Says:

    @Chris

    Sorry about that! I was sloppy with my parentheses and brackets. I fixed it up so it passes the syntax checker.

    Thanks for catching the bug.

  4. Chris Says:

    No problems I ended up fixing it myself, thanks for code!

  5. Ryan Daigle Says:

    Thanks for filling in my original post for me!

Leave a Reply

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Log in