Home Platform Services Portfolio Team Blog Contact us
GuruOnRails

Hot Item - Get a FREE Project Estimation! Click here to proceed!
 
How to create RSS feed
 
Add to Google Add to Delicious Digg it Add to FURL Add to Newsvine Add to Netscape Add to Reddit Add to STUMBLEUPON Add to MAGNOLIA Add to TECHNORATI

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" />
Konstantin
16 Oct 07:04
0
 
 
Add to Google Add to Delicious Digg it Add to FURL Add to Newsvine Add to Netscape Add to Reddit Add to STUMBLEUPON Add to MAGNOLIA Add to TECHNORATI
Only registered users are allowed to leave comments. Please, register.