Display RSS Feeds

I'd like to be able to both display rss feeds from other sites on my
website and also to eventually produce rss feeds from my own content.

Can someone point me to a tutorial on how to do this? I found one that
had code examples, but neglected to tell me where/which files to put
the code.

vhenry wrote:

I'd like to be able to both display rss feeds from other sites on my
website and also to eventually produce rss feeds from my own content.

Can someone point me to a tutorial on how to do this? I found one that
had code examples, but neglected to tell me where/which files to put
the code.

Hi - here's a quick how-to...

create a controller called 'feeds'

create an action in this controller called "rss"

create a route to this action in your routes folder like so:

map.rss "rss", :controller => "feeds", :action => "rss", :format => :rss

Let's assume you have a model called Post and this feed is for all of
your posts.

The rss action in your feeds controller should look like:

def rss
  @posts = Post.all :order => "created_at DESC"
end

create a view template for this feed called "rss.rss.builder"

Note - the first rss is the name of the template, the second is the
format of the template.

the view should look something like this:

xml.instruct!
xml.rss "version" => "2.0",
        "xmlns:dc" => "htt://purl.org/dc/elements/1.1/" do
  xml.channel do
    xml.title "the title of your feed goes here"
    xml.description "write a short description"
    xml.pubDate CGI.rfc1123_date @posts.first.created_at if @posts.any?
    xml.link url_for :only_path => false, :controller => 'posts'
    xml.guid url_for :only_path => false, :controller => "posts"
    xml.lastBuildDate CGI.rfc1123_date @posts.first.updated_at if
@posts.any?
    xml.language "en"
    @posts.each do |post|
      xml.item do
        xml.title post.title
        xml.description post.body[0..500]
        xml.link url_for :only_path => false,
                          :controller => 'posts',
                          :action => 'show',
                          :id => post
        xml.pubDate CGI.rfc1123_date post.updated_at
        xml.guid url_for :only_path => false,
                         :controller => 'post',
                         :action => 'show',
                         :id => post
        xml.author post.user.username
      end
    end
  end
end

Now all you have to do is add <%= link_to "rss feed", rss_path %> to
your view and, if you like, <%= auto_discovery_link_tag :rss, rss_path
%> to the <head></head> of your layout

read about the RSS specifications here:

http://cyber.law.harvard.edu/rss/rss.html

Hope that helps?

Gavin

http://handyrailstips.com

forgot to add...

to parse HTML, check out Hpricot

http://www.rubyinside.com/parse-xml-quickly-and-easily-with-hpricot-166.html