For one my recent project need RSS feed. So I have created myself.
This will show you how to create a RSS feed that the Feed Validator considers valid.
First create a FeedsController to host the RSS feed.
class FeedsController < ApplicationController layout false def rss @posts = Post.published_posts.limit(50) end end
Then create a view in views/feeds/rss.rss.builder for RSS.
xml.instruct! xml.rss :version => '2.0', 'xmlns:atom' => 'http://www.w3.org/2005/Atom' do xml.channel do xml.title 'Feed title' xml.description 'Feed description' xml.link root_url xml.language 'en' xml.tag! 'atom:link', :rel => 'self', :type => 'application/rss+xml', :href => posts_url for post in @posts xml.item do xml.title post.title xml.category post.category.name xml.pubDate(post.created_at.rfc2822) xml.link post_url(post) xml.guid post_url(post) xml.description(h(post.body)) xml.image_url post.image_url end end end end
Create a route to the feed in config/routes.rb:
get 'feed.rss', to: 'feeds#rss', :format => 'rss'
You can get browsers to auto-detect your Rails blog rss feed with a single line of Ruby on Rails code:
<%= auto_discovery_link_tag(:rss, "http://example.com") %>
That’s it.
Happy Coding 🙂