Adding a rss feed to my Ruby on Rails blog
February 16, 2008 15:53
I built this website from scratch using Ruby on Rails so there is still room for improvement.
Today I decided to add a rss feed to the blog. This can be done pretty simple and I will show you what I did.
First I added a new action to my blog controller.
def rss
@posts=Post.find(:all, :order=> "id DESC")
render_without_layout
@headers["Content-Type"] = "application/xml; charset=utf-8"
end
Then I created a new view for the xml file. Here is how the rss.rxml file looks like:
xml.instruct!
xml.rss "version" => "2.0", "xmlns:dc" => "http://purl.org/dc/elements/1.1/" do
xml.channel do
xml.title "kolor-designs articles"
xml.link url_for :only_path => false, :controller => 'blog'
xml.description "kolor-designs.com: Mostly web related stuff"
@posts.each do |article|
xml.item do
xml.title article.title
xml.link url_for :only_path => false, :controller => 'blog',
:action => 'show', :id => article.id
xml.description truncate(article.body,100)
xml.guid url_for :only_path => false, :controller => 'blog',
:action => 'show', :id => article.id
end
end
end
end
That’s it.

Comments