Quick tip - Rails partials in RSS feeds

July 18th, 2006 Quick tipsRails

Are you using XML builder (rxml templates) for your RSS feeds? If so, it's likely that you're rendering partials for item descriptions. Beware that Builder 2.0 will automatically escape html tags. The trick is to use the builder's << operator to bypass the markup escaping.

Example:

xml.instruct!

xml.rss :version => '2.0' do
    xml.channel do
        xml.title 'My cool feed'
        xml.link url_for(:controller => 'posts', :action => 'index', :only_path => false)
        xml.pubDate CGI.rfc1123_date(@posts.first.entry_date) if @posts.any?
        xml.description "A feed of cool posts"
        @posts.each do |post|
            xml.item do
            xml.title post.title
            xml.link url_for(:controller => 'posts', :action => 'show', :id => post.id, :only_path => false)
            xml.description do
                xml.<< render(:partial => 'post', :object => post)
            end
            xml.pubDate CGI.rfc1123_date(post.created_on)
            xml.guid url_for(:controller => 'posts', :action => 'show', :id => post.id, :only_path => false)
        end
    end
end
--- --- ---

Commenting is closed for this article.