Hey guys,
Lets today we create the rss feed with our existing rails application. Consider we have controller called post and have index method to show all the available posts. Now meanwhile we need to create feed for available posts as well.
controller:
def index
@posts = Post.find(:all, :order => "created_at desc")
respond_to do |format|
format.html { render :template => 'posts/index.rhtml' }
format.xml { render :template => 'posts/index.rxml', :layout => false
headers["Content-Type"] = "application/rss+xml"
}
end
end
view:
index.rxml
xml.instruct!
xml.rss "version" => "2.0", "xmlns:dc" => "http://purl.org/dc/elements/1.1/" do
xml.channel do
xml.title 'Available Posts'
xml.description h("Here is the posts ... blah blah")
@posts.each do |post|
xml.item do
xml.title post.title
xml.link url_for(:only_path => false,
:controller => 'posts',
:action => 'show',
:fishery => post.id)
xml.description post.description
xml.pubDate CGI.rfc1123_date(post.created_at)
end
end
end
end
index.rhtml
# Add auto discovery tag to access rss feed
<%= auto_discovery_link_tag :rss, {:controller => "posts", :action => "index"}%>
See your rss feed for posts is ready!! Whenever auto discovery tag found it enables the rss icon on browser. Keep in mind Either you have to use predefined xml node or need to create XSL template. You can also emend the stylesheet into XSL template.