Generating RSS feed in Rails 3
I'm looking for a best practice/standard pattern for generating feeds in Rails 3. Is http://railscasts.com/episodes/87-generating-rss-feeds still valid?
First of all, nowadays I recommend using an ATOM feed instead of RSS.
The specification of ATOM feed offers more value than the RSS one with internationalization, content types and other things and every modern feed reader supports it.
More info about ATOM vs RSS can be found at:
- the Wikipedia ATOM entry
- PRO Blogger and Free Marketing Zone blog posts about the subject
On to the coding:
This example assumes:
- a model called
NewsItem
with the following attributes:title
content
author_name
- a controller for that model (
news_items_controller.rb
), to which you'll add thefeed
action
We'll use a builder template for this and the Ruby on Rails atom_feed helper which is of great use.
1. Add the action to the controller
Go to app/controllers/news_items_controller.rb
and add:
def feed
# this will be the name of the feed displayed on the feed reader
@title = "FEED title"
# the news items
@news_items = NewsItem.order("updated_at desc")
# this will be our Feed's update timestamp
@updated = @news_items.first.updated_at unless @news_items.empty?
respond_to do |format|
format.atom { render :layout => false }
# we want the RSS feed to redirect permanently to the ATOM feed
format.rss { redirect_to feed_path(:format => :atom), :status => :moved_permanently }
end
end
2. Setup your builder template
Now let's add the template to build the feed.
Go to app/views/news_items/feed.atom.builder
and add:
atom_feed :language => 'en-US' do |feed|
feed.title @title
feed.updated @updated
@news_items.each do |item|
next if item.updated_at.blank?
feed.entry( item ) do |entry|
entry.url news_item_url(item)
entry.title item.title
entry.content item.content, :type => 'html'
# the strftime is needed to work with Google Reader.
entry.updated(item.updated_at.strftime("%Y-%m-%dT%H:%M:%SZ"))
entry.author do |author|
author.name entry.author_name
end
end
end
end
3. Wire it up with a route
Let's make the feeds available at http://domain.com/feed
This will call the action with the ATOM format by default and redirect /feed.rss
to /feed.atom
.
Go to config/routes.rb
and add:
resources :news_items
match '/feed' => 'news_items#feed',
:as => :feed,
:defaults => { :format => 'atom' }
4. Add the link to ATOM and RSS feeds on the layout
Finally, all that is left is to add the feed to the layout.
Go to app/views/layouts/application.html.erb
and add this your <head></head>
section:
<%= auto_discovery_link_tag :atom, "/feed" %>
<%= auto_discovery_link_tag :rss, "/feed.rss" %>
There may be a typo or two in that, so let me know if this works for you.
I did something similar but without creating a new action.
index.atom.builder
atom_feed :language => 'en-US' do |feed|
feed.title "Articles"
feed.updated Time.now
@articles.each do |item|
next if item.published_at.blank?
feed.entry( item ) do |entry|
entry.url article_url(item)
entry.title item.title
entry.content item.content, :type => 'html'
# the strftime is needed to work with Google Reader.
entry.updated(item.published_at.strftime("%Y-%m-%dT%H:%M:%SZ"))
entry.author item.user.handle
end
end
end
You don't need to do anything special in the controller unless you have some special code like i did. For example I'm using the will_paginate gem and for the atom feed I don't want it to paginate so I did this to avoid that.
controller
def index
if current_user && current_user.admin?
@articles = Article.paginate :page => params[:page], :order => 'created_at DESC'
else
respond_to do |format|
format.html { @articles = Article.published.paginate :page => params[:page], :order => 'published_at DESC' }
format.atom { @articles = Article.published }
end
end
end