Creating RSS feed for Rails website

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

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