Resource feeder is no more, but the atom feed helper is here to replace it
Rails is all about some open-ness and interopability and it has become incrementally easier to achieve this with the new Resource Feeder plugin. This plugin gives you an easy way to create RSS and Atom feeds in your controllers from a collection of model objects with the use of the rss_feed_for and atom_feed_for methods:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
class PostsController < ApplicationController # Build an rss feed def rss render_rss_feed_for Post.find(:all, :order => 'created_at DESC', :limit => 10) end # Build an atom feed def atom render_atom_feed_for Post.find(:all, :order => 'created_at DESC', :limit => 10) end end |
So how do these feed methods know how to pull information from each individual resource of the collection? It uses a combination of options passed into the feed methods and specific properties of the resource. If you don’t pass any options into these feed methods your model objects should have title, description and created_at reader methods (and updated_at for atom).
Options
There are several options you can pass into these nice little feed methods to customize the title of the feed and most of the other feed elements. Here’s my attempt at enumerating all of them for RSS:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
# Feed options (w/ defaults): options[:feed][:title] # Pluralization of the model class, i.e. "Posts" options[:feed][:link] # Url to this record options[:feed][:description] # omitted if not specified options[:feed][:language] # "en-us" options[:feed][:ttl] # 40 # Individual item options (w/ defaults). These are the method symbols used to # retrieve the proper values from each individual object: options[:item][:title] # :title options[:item][:description] # :description options[:item][:pub_date] # :created_at # General options: options[:url_writer] # an instance of UrlWriter |
Just pass in these options to your feed methods as needed:
1 2 3 4 5 6 7 8 9 10 11 12 |
class PostsController < ApplicationController # Build an rss feed def rss render_rss_feed_for(Post.find(:all, :order => 'created_at DESC', :limit => 10), { :feed => {:title => "All posts"}, :item => {:title => :name, :pub_date => :updated_at} }) end end |
Feelin’ sassy? Try taking advantage of this guy :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
class PostsController < ActionController # View a collection of posts. If "format" is specified # will return view of that type (of either rss or atom format) # /posts/list.rss => RSS # /posts/list.atom => Atom def list @posts = Post.find(:all, :order => 'created_at DESC', :limit => 10) options = { :feed => {:title => "All posts"}, :item => {:title => :name, :pub_date => :updated_at} } respond_to do |wants| wants.html wants.rss { render_rss_feed_for @posts, options } wants.atom { render_atom_feed_for @posts, options } end end end |
It just feels so right sometimes!
oh yeah, and to install this pup just do:
1 2 |
script/plugin install simply_helpful script/plugin install resource_feeder |
tags: rubyonrails, rails, rss, atom

David P. – I think you should submit a patch for this if that’s the case… :)
http://dev.rubyonrails.org/
It’s already there -> http://dev.rubyonrails.org/ticket/7468.
I’m having this problem as well and hoping it will get fixed up nice and quick.
Looks like Markdown has trashed your list of options in this post..
Great write-up and apparently the authoritative source according to Google. So very little has been written about this. All signs point to this article. Two suggestions:
1) As Joseph indicated, it looks like Markdown busted your options markup. Hovering over the URL indicates what you were trying to illustrate, but the text shown is broke.
2) To make the article more complete and self-contained, at the end where you mention script/install, you should also mention installing script/plugin install simply_helpful, as DHH pointed out.
jvp – excellent points. I’ve updated the post with some much needed formatting love. Thanks for the motivation!
David P. & Kjell – Looks like your issue has been patched: http://dev.rubyonrails.org/changeset/6150
I just tried out this plug-in, but it doesn’t seem to work for me….
first, I got a collection of the items (of type “page”) that I want to create an rss feed for, and in the controller, I added:
def rss
end
But, when I try to run this, I get an exception that it says I don’t have a “pages_url” method. After some preliminary investigation, I added a :link => “http://test” to the :feed variable, and then I got another exception of no “page_url” method….. So to fix this, I had to create a method called page_url in my controller. Rails passes one variable to this method from which I will need to figure out which page it is referring to return a url that points to this page. However, this seems like more work than just creating the rss feed manually… (although I am posting because I might be missing something simple
how can you explicitly declare the link for the items? rss.rb is automagically creating it to point back to the actual post page; suppose i wanted to go somewhere else (use in a non-blog app)? any ideas?
Okay figured that out… you add this line above the resource_link = lambda… code: options:item ||= lambda { |r| SimplyHelpful::RecordIdentifier.polymorphic_url(r, options[:url_writer]) }
from there, you can add :item :link => :link in your controller and that will access the link method in your model…
How do I test the RSS and Atom feeds?
sounds interesting, can put Feed generator and Anothr IM-based delivery. It is pretty cool!
Phil: You should only need to set options:item with a Proc object, no need to hack the plugin.
If anyone is having problems with polymorphic_url, I wrote up a small howto detailing how to get around this issue.
http://blog.aisleten.com/2007/05/14/demystifying-resource_feeder-for-rss-feeds-polymorphic-urls/
Resource_feeder is gone now :-( http://dev.rubyonrails.org/changeset/6835