RSS FEED on Ruby on Rails

Posted On November 13, 2009

Filed under ROR

Comments Dropped leave a response


RSS FEED:

rsslogoRSS stands for “Really Simple Syndication”. It is a way to easily distribute a list of headlines, update notices, and sometimes content to a wide number of people. It is used by computer programs that organize those headlines and notices for easy reading.

Purpose of RSS:

instant information information updated in realtime
single source all information aggregated in a single location
rapid scanning feed readers highlight unread headlines
categorizing information can be categorized by  theme

Creating RSS Feed:

Edit the  controller  file of a model which you like to  add the RSS Feed

my controller: /app/controllers/post_controller.rb

STEP 1:

In that write the following code instead  def index………   end and add def for rss

def index
@posts = Post.all
respond_to do |format|
format.html # index.html.erb
format.xml  { render :xml => @posts }
format.rss { render :layout => false }
end
end

def rss
@posts = Post.find(:all, :order => “id DESC”)
render_without_layout
@headers[“Content-Type”] = “application/xml; charset=utf-8”
end

STEP 2:

Edit the file app/views/posts/intex.html.erb for dispaly the link your page:

Add the line <p> <%= link_to “RSS Feed”, posts_url(:rss) %></p> in that.

STEP 3:

Create the new file index.rss.builder in app/views/posts/ directory. which contains the following code.

xml.instruct! :xml, :version => “1.0”
xml.rss :version => “2.0” do
xml.channel do
xml.title “Events”
xml.description “All Events”

@posts.each do |e|
xml.item do
xml.title e.name
xml.description e.content
end
end
end
end

create one more file with the name of rss.rxml and it contains the following

xml.instruct!

xml.rss “version” => “2.0”, “xmlns:dc” => “http://purl.org/dc/elements/1.1/&#8221; do
xml.channel do

xml.title       “paulsturgess.co.uk articles”
xml.link        url_for :only_path => false, :controller => ‘articles’
xml.description “paulsturgess.co.uk Ruby on Rails and CSS articles”

@articles.each do |posts|
xml.item do
xml.title       posts.title
xml.link        url_for :only_path => false, :controller => ‘posts’, :action => ‘show’, :id => posts.id
xml.description posts.content
xml.guid        url_for :only_path => false, :controller => ‘posts’, :action => ‘show’, :id => posts.id
end
end
end
end

Start the server using this command:

script/server

Note:

The smilies are  display because  of joining  the : and x ,like 😡 and : and 0 like 😮 , so you must type instead that  : and x, : and o.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s