RSS feed is must have feature for any blog. RSS make simple for your readers to keep track of your posts and of course it would produce permanent traffic for you Rails blog. Isn't it great?
So lets start, it's really simple to implement, but could produce great effect.
First of all you should prepare content to feed. You should add new action to your controller:
def rss
@posts = Post.find(:all, :order=>"created_at DESC")
response.headers["Content-Type"] = "application/xml; charset=utf-8"
render :action=>"rss", :layout=>false
end
The second step is to create view for this action, rss.rxml that contains something like that:
xml.instruct!
xml.rss "version" => "2.0", "xmlns:dc" => "http://purl.org/dc/elements/1.1/" do
xml.channel do
xml.title "GuruOnRails Blog"
xml.link url_for :only_path => false, :controller => 'blog'
xml.description "Blog about Ruby On Rails and web development"
@posts.each do |post|
xml.item do
xml.title post.title
xml.link url_for :only_path => false, :controller => 'blog', :action=>'show_post', :permalink=>post.permalink
xml.description(truncate(post.body, 200))
xml.guid url_for :only_path => false, :controller => 'blog', :action=>'show_post', :permalink=>post.permalink
end
end
end
end
That's all, your feed is up and running, enjoy!
P.S. Just one more tip. You could also get browsers to auto discover you RSS feed by adding just one line to you template:
<link rel="alternate" type="application/rss+xml" title="RSS" href="url/to/rss/file" />