Modulor. Custom Software Solutions.
Share.
Facebook | Twitter | LinkedIn
Posted by: Gustavo on January 12, 2011
Category: Ruby on Rails
Some sites with continuous feed of updates will require RSS feeds in order for them to become more accessible and allow for content to be easily redistributed.

We can create an RSS feed easily with Ruby on Rails. You just need to follow the next steps:

1. Create an action in your controller for the RSS feed.

def rss
@noticias = Noticia.find(:all)
render :layout => false
response.headers["Content-Type"] = "application/xml; charset=utf-8"
end

2. Create a view, in this case rss.rxhml that will show the feed contents. It should look something like this:

xml.instruct!

xml.rss "version" => "2.0", "xmlns:dc" => "http://purl.org/dc/elements/1.1/" do
xml.channel do

xml.title "Modulor News"
xml.link url_for :only_path => false, :controller => 'home'
xml.description "Modulor Daily News"

@noticias.each do |noticia|
xml.item do
xml.title noticia.titulo
xml.link url_for :only_path => false, :controller => 'home', :action => 'noticia', :id => noticia.permalink
xml.description noticia.descripcion
xml.guid url_for :only_path => false, :controller => 'home', :action => 'noticia', :id => noticia.permalink
end
end

end
end

3. If you want other websites of applications like Google Reader to auto-discover the feed, add the following html code to your views/layouts.



Just make sure your feed is valid and you're done.