How to create an RSS feed with Ruby on Rails tutorial

by: koloa on August 23 2008

This tutorial will show you what I did to create an RSS feed for my rails applications. I’m sure there are much better ways to do this now with Rails 2.0.2 out, but here it is.

Call your RSS method

Create a simple action in any controller that populates your variable with what you want to be displayed in your feed. In my case, I am using post.

  def rss
    @posts = Post.find(:all)
  end

Create a rss.rxml file in your view directory

I put comments in the code. Enjoy.

xml.instruct! :xml, :version=>"1.0" 

xml.rss(:version=>"2.0") {

  xml.channel {

    xml.title("url.com")

    xml.link("http://www.url.com")

    xml.description("articles and tutorials about something")

    xml.language("eng-us")

    for post in @posts

      xml.item do

        xml.title(post.title)

        xml.description(post.description)

        xml.pubDate(post.created_at.rfc2822)
      xml.link("http://url.com/category/#{post.category.title}/#{post.id}")

xml.guid("http://url.com/category/#{post.category.title}/#{post.id}")

      end

    end

  }

}