Wow, that was easy

I just got around to looking into creating RSS feeds for the news section of the CMS I’m building in Rails, so dug out my still-new Rails Recipes book and was pleasantly surprised to see how easy it was. Obviously, an RSS feed is just XML which is simple to churn out, but Rails’ XML Builder does it all in code and does it well.

The only bit I had to look up was how to get content:encoded and CDATA tags and I now have a basically-working RSS feed with this relatively short chunk of code in the .rxml view template:

xml.instruct!
xml.comment! “generator=\”infinisys/1.0\”"
xml.rss “version” => “2.0″, “xmlns:content” => “http://purl.org/rss/1.0/modules/content”, “xmlns:dc” => “http://purl.org/dc/elements/1.1/” do
xml.channel do
xml.title @site.title
xml.link url_for(:only_path => false,
:controller => ‘news’,
:action => ‘list’
)
xml.pubDate CGI.rfc1123_date(@news.first.created_at)
xml.description h(@site.description)
@news.each do |post|
xml.item do
xml.title post.title
xml.link url_for(:only_path => false,
:controller => ‘news’,
:action => ’show’,
:id => post
)
xml.description do
xml.cdata!(post.content)
end
xml.tag!(”content:encoded”) do
xml.cdata!(post.content)
end
xml.pubDate CGI.rfc1123_date(post.created_at)
xml.guid url_for(:only_path => false,
:controller => ‘news’,
:action => ’show’,
:id => post
)
xml.author h(post.user.firstname + ‘ ‘ + post.user.lastname)
end
end
end
end

TinyMCE doesn’t like pre-formatted text it seems and I can’t really be bothered faffing around, so I’m afraid it’ll all have to be in one ‘column’ for now

Nifty, non?

Sometimes this Rails lark just seems too easy, though luckily I’ve still managed to set myself a few tricky bits with this CMS so I’m not getting complacent (or feeling too redundant) just yet.

One Response to “Wow, that was easy”

  1. Ken’s Blog » Education Says:

    […] After reading a post Adam made I watched the video of Sir Ken Robinson presentation about education. I have to say I was fascinated by it and indeed a lot of it I feel strongly about. […]

Leave a Reply