Creating an RSS feed in Ruby on Rails
Creating an RSS feed for your website in Ruby on Rails is very simple.
To set it up on this site I added the following lines to my articles controller
def rss
@articles = Article.find(:all, :order => "id DESC")
render_without_layout
@headers["Content-Type"] = "application/xml; charset=utf-8"
end
In my views I created an rss.rxml file inside the articles folder and in it contains:
xml.instruct!
xml.rss "version" => "2.0", "xmlns:dc" => "http://purl.org/dc/elements/1.1/" 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 |article|
xml.item do
xml.title article.title
xml.link url_for :only_path => false, :controller => 'articles', :action => 'show', :id => article.id
xml.description article.content
xml.guid url_for :only_path => false, :controller => 'articles', :action => 'show', :id => article.id
end
end
end
end
You can check if your feed validates at feedvalidator.org.
To get browsers to auto discover your rss feed you only need to add one line to your layout template:
<link rel="alternate" type="application/rss+xml" title="RSS" href="url/to/rss/file" />
If you want different views to 'auto detect' different feeds you have created then you can set your path to your rss feed as an instance variable and pass it from the view to the template. I've written another short article on how this works.
About
Paul is a Rails developer for Kyan web agency.
Checkout his ramblings about music, what he's listening to and his other random thoughts.
He also writes on the Kyan blog.
More snippets
Take a look in the archive
Need a website?
Your best bet is to contact Paul's employer. Make sure to check out the portfolio of work.
Hosting
I recommend hostingrails.com
Recommended Reading
Contact
my name at gmail.com
24 comments made
Or you could use the auto_discovery_link_tag helper if you are using 1.2 or Edge.
Cheers,
Walter
Really, really helpful! Particularly for someone like me (beginner at RoR).
That’s amazing! This site is so useful!
Been using ruby for a day now, and overwhelmed by it’s awesomeness!
Switched to ruby after using PHP, rather than going to Cake because I wanted to learn something new, it’s all very sensible, particularly easier to create RSS feeds and consume them! Very good tutorial, quick ‘n’ dirty, many thanks!
This is a SMART STUFF!
I tried out the rss thing u explained above and tried to add it to my google home page.
However It says that the the RSS is not found.
Do I need to changes anything in the map.resources in the config file ?
Let me know if any ideas
forgot to mention that the field does not validate in the feedvalidator.org either..
he i tried to add the code but i got some error should i create any database or the xml will create it .
please i need your help
Thanks a lot for putting this together!
It seems RSS 2.0 doesn’t support relative URL’s which is kind of a pain. So
xml.description article.contentmay not be enough, depending whether it is full HTML or not.
Stephan
Also came across this one at
http://feedvalidator.org/docs/warning/MissingAtomSelfLink.html
Oh well.
Stephan
Great article, but i have to do 2 modifications ( i have rails 2.1.0).
First i have to change :
render_without_layout
by
render :layout => false
Because render_without_layout it’s deprecated in this version of rails (2.1.0)
and
@headers[“Content-Type”] = “application/xml; charset=utf-8”
by
response.headers[“Content-Type”] = “application/xml; charset=utf-8”
and that’s it, the rest works fine with the same logic,
cheers!.
Thats cool and all, but how do you make an .rxml file?? in aptana radrails there is no such layout available to me.
Thats cool and all, but how do you make an .rxml file?? in aptana radrails there is no such layout available to me.
What should be in the routes file? I have “map.resources :articles”, but I get this error
Couldn’t find Article with ID=rss
Found it…I changed the route to
map.resources :articles, :collection => {:rss => :get}
not sure if this is the best way, but it works.
I already create a table name articles and filleds are title,link,description,guid
You have a nil object when you didn’t expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each
Extracted source (around line #10):
8: xml.description “paulsturgess.co.uk Ruby on Rails and CSS articles”
9:
10: @articles.each do |article|
11: xml.item do
Works great, thanks for this guide :) Made setting up rss feeds a cinch (and even customised ones to allow subscribing to categories on my site)
test
Works but how do i use this. It generates an xml file. How will any user subscribe to it? Can someone add more clarity on how exactly to used the produced xml file?
nice example! also check out this one
http://media.railscasts.com/videos/087_generating_rss.mov
I have a requirement to take an RSS feed into a Rails app and automatically update a table from the data provided therefrom (foreign currency exchange rates). I have found the feed tools gem but this seems to be a little old and shows no sign of recent activity. Is there anything else that is current, works with Rails 2.3 and is presently favored by the community?
hi paul
ım new on rails
ı try to create rss feed on my own website.. ı looked your code but ı cant.
do u give more information to me?
hi paul
ım new on rails
ı try to create rss feed on my own website.. ı looked your code but ı cant.
do u give more information to me?
Got something to say?