How to make an RSS feed in Rails

2010-02-12 22:12:05 +0000

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. power Says:

    Positive site, where did u come up with the information on this posting? I’m pleased I discovered it though, ill be checking back soon to find out what additional posts you include.
    Sports News Australia

  2. power Says:

    Positive site, where did u come up with the information on this posting? I’m pleased I discovered it though, ill be checking back soon to find out what additional posts you include.
    Sports News Australia

  3. power Says:

    Positive site, where did u come up with the information on this posting? I’m pleased I discovered it though, ill be checking back soon to find out what additional posts you include.
    Sports News Australia

  4. jauy11 Says:

    i read a lot of stuff and i found that the way of writing to clearifing that exactly want to say was very good so i am impressed and ilike to come again in future..
    Galoor

  5. David Rivers Says:

    Pz, bitches (RSS)!!!!

    Love it when devs push back against shitty technologies.

  6. Derek Neighbors Says:

    Nothing wrong with being a little opinionated right?

  7. Paweł Gościcki Says:

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

  8. 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…)

  9. Mikel Lindsaar Says:

    @tiny and @pawel

    Done. Full text feeds are now in place :)

    Mikel

  10. Sohan Says:

    www.DrinkRails.com Linked your post

  11. 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?

  12. 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.

  13. 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!

  14. 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|

  15. Bernard Says:

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

  16. 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

  17. barcode system Says:

    I realy like this article. This is a hash of default values for any email you send, in this case we are setting from the header to a value for all messages in this class, this can be overridden on a per email basis..

    Mesin Kasir | Komputer Kasir | Jual Barcode | Printer Kasir

    Printer Kartu

  18. retail system Says:

    Nice Article, I feel strongly that love and read more on this topic. it’s very spectaculer.

    Mesin Kasir | Komputer Kasir | Jual Barcode | Printer Kasir

    Printer Kartu

  19. timeline covers for facebook Says:

    Finally I understand how to do it. Thanks!

  20. Javier Vidal Says:

    Going for ATOM instead of RSS. Thanks for sharing !!

  21. Javier Vidal Says:

    Going for ATOM instead of RSS. Thanks for sharing !!

  22. Javier Vidal Says:

    Mikel, something weird is going on with the comments. They are not ordered by date. I’m sorry I submitted my last comment twice.

  23. Karmaloop Promo Codes Says:

    I would like to say that this blog really convinced me to do it! Thanks, very good post.

  24. sdsadsd Says:

    Very nice article, I enjoyed reading your post, very nice share, I want to twit this to my followers. Thanks! gluten free

  25. superking Says:

    I have read your blog it is very helpful for me. I want to say thanks to you. I have bookmark your site for future updates.
    new horoscope signs

  26. oil plant Says:

    Great post, I always follow your blog since it is full of persuasive information about various things

  27. ranger Says:

    Excellent information on your blog, thank you for taking the time to share with us. Amazing insight you have on this, it’s nice to find a website that details so much information about different artists.
    Paphos hotels

  28. jay11 Says:

    Superbly written article, if only all bloggers offered the same content as you, the internet would be a far better place..
    http://fr.onlinebaccara.com

  29. asdsdsd Says:

    Superbly written article, if only all bloggers offered the same content as you, the internet would be a far better place..
    http://www.point-box.com

  30. azee1122 Says:

    I wanted to thank you for this great read!! I definitely enjoying every little bit of it I have you bookmarked to check out new stuff you post.
    cheap adjustable dumbbells

  31. Atik Says:

    I have to say this post was certainly informative and contains useful content for enthusiastic visitors.
    Zimmet Vein & Dermatology-Austin

  32. srinarax Says:

    The website is looking bit flashy and it catches the visitors eyes. Design is pretty simple and a good user friendly interface.PS3 Emulator

  33. MrEmo Says:

    Upon seeing those families having a trail, I envy because I never got a chance to have to. However, I am still happy that the parents are spending much of their time for the kids.Xbox Emulator

  34. lemiPieftof Says:

    Children’s tooth development begins while the baby is in the womb. Teething usually occurs between the ages of six and nine months. Children usually have their full set of 20 primary teeth (milk teeth, baby teeth or deciduous teeth) by the age of three years. At about the age of six years, the first permanent teeth erupt (push through the gum).

  35. string Says:

    Seriously worthwhile write-up. Spend interest string http://gotenerife-plus.com/hydrofory.xhtml

  36. drukarki bielsko biała Says:

    I seriously get pleasure from your posts. Many thanks drukarki bielsko biała http://taiyche.org.pl/?p=6065

  37. Victor1122 Says:

    Excellent information on your blog, thank you for taking the time to share with us. Amazing insight you have on this, it’s nice to find a website that details so much information about different artists.
    e cigarette reviews

  38. Dedepreno Says:

    Today’s houses are larger and more costly than ever. They load our sensible resources. After they are built they place an ever-growing trouble on our pocketbooks and forcefulness resources, because they are not built in an environmentally awake way.

    http://foro.spainlanuit.com/viewtopic.php?f=28&t=49143&p=89816
    http://delustu.org.pl/?p=5931
    http://www.jurekband.pl/pokaz.php?id=209
    http://www.linkworld.pl/firmy/hotel,opera,konferencje,w,sopocie,s,10038/
    http://www.jurekband.pl/pokaz.php?id=160

    http://kimanildcerdra.com.pl/?p=5680
    http://stripercup.com/viewtopic.php?f=12&t=15997
    http://rilim-ack.edu.pl/?p=5551
    http://www.metalfan.ro/forum/viewtopic.php?f=4&t=109032&start=0
    http://www.zielonykatalog.net.pl/firmy/hotel,opera,szkolenie,w,sopocie,s,3142/

  39. osiowanie maszyn Says:

    I am curious to find out what blog platform you have been working with? I’m having some small security problems with my latest blog and I’d like to find something more safeguarded. Do you have any recommendations? osiowanie maszyn http://urzadzenialakierniczemarius.pl/wywazanie-wirnikow/

Leave a Reply