Skip to content

Instantly share code, notes, and snippets.

@prepor
Created June 20, 2010 14:08
Show Gist options
  • Save prepor/445869 to your computer and use it in GitHub Desktop.
Save prepor/445869 to your computer and use it in GitHub Desktop.
RSS renderer for rails3
# Usage:
respond_to :rss, :only => [:index]
def index
@deals = Deal.by_city(@city).limit(15)
respond_with(@deals) do |format|
format.rss { render :rss => @deals, :title => "KupiKon", :item => { :title => :title, :description => :description }}
end
end
Mime::Type.register "application/rss+xml", :rss
ActionController.add_renderer :rss do |data, options|
self.content_type ||= Mime::XML
self.response_body = data.to_xml(options)
xml = Builder::XmlMarkup.new(:indent => 2)
xml.instruct!(:xml, :encoding => "UTF-8")
xml.rss "version" => "2.0" do
xml.channel do
xml.title options[:title]
xml.link root_path
xml.description options[:description]
data.each do |item|
xml.item do
xml.title item.send(options[:item][:title])
xml.link url_for(item)
xml.description item.send(options[:item][:description])
xml.guid url_for(item)
end
end
end
end
self.response_body = xml.target!
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment