# Display RSS Feeds

**URL:** <https://www.ruby-forum.com/t/display-rss-feeds/170179>\
**Category:** Rails\
**Created:** [June 13, 2009, 10:33am UTC](https://www.ruby-forum.com/t/display-rss-feeds/170179 "2009-06-13T10:33:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![vhenry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/df788c/32.png) [@vhenry](https://www.ruby-forum.com/u/vhenry)\
**Post date:** [June 13, 2009, 10:33am UTC](https://www.ruby-forum.com/t/display-rss-feeds/170179/1 "2009-06-13T10:33:45Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![vhenry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/df788c/32.png) [@vhenry](https://www.ruby-forum.com/u/vhenry)\
**Post date:** [June 14, 2009, 6:02pm UTC](https://www.ruby-forum.com/t/display-rss-feeds/170179/2 "2009-06-14T18:02:53Z")

</div>

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 of your layout

read about the RSS specifications here:

[http://cyber.law.harvard.edu/rss/rss.html](http://cyber.law.harvard.edu/rss/rss.html)

Hope that helps?

Gavin

> **[Ruby on Rails tips & Tutorials](https://handyrailstips.com)**
>
> Ruby on Rails tips and tutorials

---

<div class="post-metadata">

**Author:** ![vhenry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/df788c/32.png) [@vhenry](https://www.ruby-forum.com/u/vhenry)\
**Post date:** [June 14, 2009, 6:05pm UTC](https://www.ruby-forum.com/t/display-rss-feeds/170179/3 "2009-06-14T18:05:02Z")

</div>

forgot to add…

to parse HTML, check out Hpricot

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