How to make an RSS feed in Rails

Fri Feb 12 22:12:05 -0800 2010

Easy. Don’t. Use an ATOM feed instead. Rails is opinionated software, it has an opinion on feeds, there is an atom_feed helper, but no rss_feed helper? Omission? Nup.

I added the ATOM feed to Rails Plugins website very simply, by using the Rails atom_feed helper. It was a simple case of creating a FeedsController with just a single index action, which collected up all the time line events that needed to go onto the feed and assigned them to the view in an @events ivar.

So the controller looks like this:

class FeedsController < ApplicationController
  
  layout false
  
  def index
    @events = TimelineEvent.for_feed
    @title = "Rails Plugins Feed"
    @updated = @events.first.created_at unless @events.empty?
  end
end

Then in the view file app/views/feeds/index.atom.builder looks like this:

atom_feed do |feed|
  feed.title(@title)

  feed.updated(@updated)

  @events.each do |event|
    feed.entry(event) do |entry|

      entry.title(h(event.title))
      entry.summary(truncate(strip_tags(event.description), :length => 100))

      entry.author do |author|
        author.name(event.author_name)
      end
    end
  end
end

And then the route to wire it up:

# config/routes.rb (Rails 2.0)
ActionController::Routing::Routes.draw do |map|
  map :resources :feeds, :only => [:index]
end

And finally the auto_discovery_link_tag in the application.html.erb:

<%= auto_discovery_link_tag(:atom, "/feeds.atom") %>

Now this was all well and good, and I thought, why not add an RSS feed as well?

I started down on this path and the first thing that struck me was, there was no rss_feed helper in Rails, why? Seemed like an ommission…

Then I was looking around the web and found many examples on how to do an RSS feed in Rails, but none of them mentioned this omission of the helper, why?

I didn’t puzzle over this long, and just went ahead and added an RSS feed, it wasn’t hard, you can find this out from your friend google.

But then I got this IM from Sam Ruby after I let him know the RSS feed for Rails Plugins was now online:

“Ewwwww”

Which was not really the answer I was expecting, so I stated:

“I never did get the ATOM v RSS debate”

To which he replied:

“I started the Atom vs RSS debate.”

And that started an interesting chat! Let me tell you!

Turns out that ATOM is actually defined, with RFC 4287 that covers all the basics of what you need to know.

Also turns out that any modern reader will handle ATOM, just as easily as RSS.

Also turns out that with ATOM you can define things like (gasp) language settings, Internationalization, content types and several other points which make the data a lot more consumable.

So, then I added the following to the Rails Plugins website:

class FeedsController < ApplicationController
  def index
    # ...
    respond_to do |wants|
      wants.rss do
        redirect_to feeds_path(:format => :atom),
                    :status=>:moved_permanently
      end
      wants.atom
    end
  end
end

And decided to sum up my gained knowledge with: We are Rails, we are opinionated, ta-ta RSS http://bit.ly/c9eyNL – KTHXBAI

blogLater

Mikel

  1. David Rivers Says:

    Pz, bitches (RSS)!!!!

    Love it when devs push back against shitty technologies.

  2. Derek Neighbors Says:

    Nothing wrong with being a little opinionated right?

  3. Paweł Gościcki Says:

    Speaking of feeds, could I ask you to provide a full-text feed for your blog entries?

  4. Tiny Clanger Says:

    Can I second the call for a full-text feed? Excerpts just add to the time it takes to get through my feeds.

    (It’s not as if this is an ad-heavy site that needs to use this technique to force people to see the ads, after all…)

  5. Mikel Lindsaar Says:

    @tiny and @pawel

    Done. Full text feeds are now in place :)

    Mikel

  6. Sohan Says:

    www.DrinkRails.com Linked your post

  7. Hugo Baraúna Says:

    Great post. Recently I had to do the same thing and decided to go just with an ATOM feed.

    Just one thing, in your index action you have the following code:

    def index
    @events = TimelineEvent.for_feed
    @title = “Rails Plugins Feed”
    @updated = @events.first.created_at unless @events.empty?
    end

    But, inside the RFC we have:

    “The “atom:updated” element is a Date construct indicating the most recent instant in time when an entry or feed was modified in a way the publisher considers significant. Therefore, not all modifications necessarily result in a changed atom:updated value."

    So, instead of:

    @updated = @events.first.created_at unless

    I’m doing:

    @updated = @events.last.created_at unless @events.empty?

    What do you think about it?

  8. ribedop Says:

    Can I second the call for a full-text feed? Excerpts just add to the time it takes to get through my feeds.

  9. Chris Conley Says:

    Thanks so much! After giving up on the idea of running Wordpress on a subdomain, I gave in to rolling my own blog. This helped so much!

  10. Silvio Relli Says:

    I’m using pretty urls for the articles such mysite.com/en/articles/2010/11/26/title-of-the-article and I have an helper method for getting the url of the article.

    To override the article url you need to specify it as an entry :url option like:
    feed.entry(article, :url => get_article_url(article)) do |entry|

  11. Bernard Says:

    Nice post, will this work on Rails 3? I’ve been curious about adding rss to my site.

  12. Russell Garner Says:

    Sometimes, though, your customer is just adamant, and you’ve written all the atom_feed parts but they REALLY want RSS.

    So I was forced to write this: http://www.zephyros-systems.co.uk/blog/?p=179

  13. jaffa Says:

    RSS allows users to avoid manually inspecting all of the websites they are interested in, and instead subscribe to websites such that all new content is pushed onto their browsers when it becomes available. Thanks a lot.
    Regards,
    Douglasville bankruptcy lawyer

Leave a Reply