When I was adding RSS to a site recently I knew what I needed to do was pretty straight forward but I ended up having to consult a fair few sites to get the lowdown on RSS and Rails. I’ve tried to make it easier for everyone else by summarizing RSS and Rails here.
URLs and respond_to
So first of all we need the rss feed url. If it contains content relating to a HTML page then the Rails way is to put the RSS stuff into the same page action and request the URL with the’.rss’ extension. This means we can use a respond_to block in the action and the feed url will be detected as an rss format request. So in your action you need something like…
respond_to do |format|
format.html
format.rss { render :action => "feed.rxml", :layout => false }
end
Build the RSS
I use the RSS2 specification it seems to be widely adopted up to date and does what I need. The best explanation of this that I found is here . New create the feed.rxml template in the correct folder. Here is an example of the rxml template.
xml.instruct! :xml, :version=>"1.0", :encoding=>"UTF-8"
xml.rss(:version=>"2.0"){
xml.channel{
xml.title(@title)
xml.link(url_for(:only_path => false, :controller => 'items'))
xml.description(@description)
xml.language("en")
for item in @items
xml.item do
xml.title(item.title)
xml.link(url_for(:only_path => false,
:controller=>'items', :action=>'show', :id=>item))
xml.description(item.summary)
xml.pubDate(item.created_at.rfc2822)
xml.guid(url_for(:only_path => false,
:controller=>'items', :action=>'show', :id=>item))
end
end
}
}
A couple of things to note here. First of all the tidy xml builder that rxml uses by default. It makes it super simple to build xml documents. Secondly how simple RSS2 really is its basically just a bunch of titles, links and descriptions, very generic. Thirdly notice the UTF-8 directive at the top this may not be necessary, it basically depends on the way your data is stored in the database but if you have UTF-8 data make sure this is set correctly or you’ll have all sorts of problems with strange characters.
Associating feed with a web page
If we are on the html page which has an associated feed we really want the browser to detect that there is a feed. To do this you need to add a link to your html head element.
<head>
...
<link href="<%= url_for :only_path => false,
:controller=>'items', :format=>'rss' %>"
rel="alternate" type="application/rss+xml" />
...
</head>
This is likely to be in your layout so you may be better using content for. In your rss associated template…
<% content_for(:rss) { url_for :only_path => false,
:controller=>'items', :format=>'rss' } %>
and then in the layout something like…
<head>
...
<link href="<%= yield(:rss) %>"
rel="alternate" type="application/rss+xml" />
...
</head>
This way we keep the action specific directives in the action template but still have the rss link in the correct place which is defined in the layout. Think thats about it. Hope it helps someone.





Sorry, comments are closed for this article.