Today, I worked on creating an RSS feed for news posts of amanzitel.com website. The procedure is simple:
Step 1:
Create a new method in a controller (like pages controller) called rss as follows:
def rss
@posts = NewsPost.find(:all, :limit => 10, :order => "updated_at DESC")
respond_to do |format|
format.rss { render :layout => false }
end
end
Step 2:
Create file called rss.rss.builder in the view folder as follows:
xml.instruct! :xml, :version=>"1.0"
xml.rss(:version=>"2.0"){
xml.channel{
xml.title("AmanziTel News")
xml.link("http://www.amanzitel.com/")
xml.description("Latest news from AmanziTel")
xml.language(‘en-us’)
for post in @posts
xml.item do
xml.title(post.title)
xml.description(post.short_news)
xml.author("AmanziTel.com")
xml.pubDate(post.publish_date.strftime("%Y-%m-%d"))
xml.link("/news_posts/#{post.id}")
xml.guid(post)
end
end
}
}
Step 3:
In the template head section of application.html.erb in layouts folder add the below code to allow browser to discover the RSS.
<%= auto_discovery_link_tag(:rss, :controller => "pages", :action => ‘rss’) %>
Now, the RSS url is /pages/rss