Ask A Question

Notifications

You’re not receiving notifications from this thread.

How do I publish an RSS feed from my rails app?

Stan Smith asked in Rails

I have a simple blog setup with a posts model. Is there a gem that can publish a feed or should I create thus from scratch?

Reply

Hey Stan! Like an XML feed of them? If so, it's relatively easy. You've just got to create an index.xml.builder view which will let you render XML.

You can access all the instance variables you need from the controller just like a normal view so you can loop through the posts and print them out in the XML. I believe you'll want the tags and structure mentioned in the following example to conform to what the feeds need for tags.

Basecamp uses this for their feed, for example:

xml.rss("version" => "2.0", "xmlns:dc" => "http://purl.org/dc/elements/1.1/") do
  xml.channel do
    xml.title(@feed_title)
    xml.link(@url)
    xml.description "Basecamp: Recent items"
    xml.language "en-us"
    xml.ttl "40"

    @recent_items.each do |item|
      xml.item do
        xml.title(item_title(item))
        xml.description(item_description(item)) if item_description(item)
        xml.pubDate(item_pubDate(item))
        xml.guid(@person.firm.account.url + @recent_items.url(item))
        xml.link(@person.firm.account.url + @recent_items.url(item))

        xml.tag!("dc:creator", item.author_name) if item_has_creator?(item)
      end
    end
  end
end
Reply

Thanks!

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 86,946+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.