So you have this gee-whiz-bang web 2 dot oh app that even has it’s own reflective icon and gradient background, but yet, it’s still missing that something special. Chances are, that “something” pulling at your conscious is a need to provide Rss and/or Atom feeds for your data. I’m going to quickly walk through how you too can “feed” out your content to the world at large. Hold on tight….
First up, you’ll need the resource_feeder plugin and simply_helpful plugin..
for you non-piston users…script/plugin install resource_feederFor the piston users…
piston import http://dev.rubyonrails.com/svn/rails/plugins/resource_feeder vendor/plugins/resource_feeder
Ahhhh, so close. That was pretty much half the battle. After you’re done staring at your magificent mastery of your console window, feel free to continue.
Now, we code. Say you have a “books” controller which has an “index” action. The “index” view, lists out all of your “books”, ok? So in your “books” controller under the index method, add this (you can leave out the setting of the ”@books” instance variable if you already have it)..
@books = Book.find(:all)
options = options = { :feed => {:title => "Books, Glorious Books!"} }
respond_to do |format|
format.html # index.erb
format.xml { render :xml => @books.to_xml }
format.rss { render_rss_feed_for @books, options}
end
Woah. To see this, fire up your page and browse to your “books” controller and check out the “index” page. Everything should look exactly the same. To see your Rss feed, in the address window of your browser append ”?format=rss” (so your path looks something like http://localhost:3000/books?format=rss) to see your feed. That’s all fine and dandy but 103% of all people who surf the web, do not do so by typing whatever they feel into their address bar. So how do you link to this rss feed of your books?
First, pick up a spiffy Rss icon from feed icons and shove it into your Rails Project’s public/images folder. Now you can use the following link_to to link to your “books” feed…
<%= link_to "#{image_tag("/images/feed_icon.gif")}", formatted_books_path(:rss) %>
Double woah. Now, when you click on that link, you’ll get take to your books.rss page which will happily display an Rss feed of your books.
Now for displaying an atom feed…replace every instance of Rss on this page with Atom and you’re good to go. That’s it. Really.
Now say you wanted to change some of the standard attributes of your feed. Well, you’re in luck. You can stick all kinds of options in that options hash that you can see in the above controller code. You can get a pretty good idea of what the options default values are by looking in the actual plugin itself.
Here’s a good look at the default Rss option values…
options[:feed][:title] ||= klass.name.pluralize
options[:feed][:link] ||= SimplyHelpful::PolymorphicRoutes.polymorphic_url(new_record, options[:url_writer])
options[:feed][:language] ||= "en-us"
options[:feed][:ttl] ||= "40"
options[:item][:title] ||= [ :title, :subject, :headline, :name ]
options[:item][:description] ||= [ :description, :body, :content ]
options[:item][:pub_date] ||= [ :updated_at, :updated_on, :created_at, :created_on ]
and here’s a good look at the default Atom option values…
options[:feed][:title] ||= klass.name.pluralize
options[:feed][:id] ||= "tag:#{request.host_with_port}:#{klass.name.pluralize}"
options[:feed][:link] ||= SimplyHelpful::PolymorphicRoutes.polymorphic_url(new_record, options[:url_writer])
options[:item][:title] ||= [ :title, :subject, :headline, :name ]
options[:item][:description] ||= [ :description, :body, :content ]
options[:item][:pub_date] ||= [ :updated_at, :updated_on, :created_at, :created_on ]
options[:item][:author] ||= [ :author, :creator ]
And that’s really all there is to it. For super extra fun, re-read everything I just wrote and make the “air quotation marks” with your hands for every quoted word you read. Good times. Now go change the world.









Good post, thanks for the info!
You’re welcome. I do what I can ;)
@books.toxml produces all the elements. If I want to restrict to only certain elements of book in @books.toxml how do I do that.
If I understand what you’re asking, you can specify what attributes get used by using the options in the options hash.