-
Notifications
You must be signed in to change notification settings - Fork 52
Blog Tutorial generating RSS feed
Finally, before the application is deployed, let's set up RSS and Atom feeds for
our new blog so people can subscribe to our posts. For the feeds, we're going to
head back to the posts controller and make a few changes by appending a
provides option to our index block. This command below instructs the route
that it should respond to HTML, RSS and Atom formats.
# app/controllers/posts.rb
SampleBlogUpdated::App.controllers :posts do
# ...
get :index, :provides => [:html, :rss, :atom] do
@posts = Post.order('created_at DESC').all
render 'posts/index'
end
# ...
endNote that this route also instructs the rendering engine to avoid rendering the layout when using RSS or atom formats.
Back in the index.haml file, we'll use the auto_discovery_link_tag helpers to
generate the RSS feed using builder.
-# app/views/posts/index.haml
- @title = "Welcome"
- content_for :include do
= feed_tag(:rss, url(:posts, :index, :format => :rss), :title => "RSS")
= feed_tag(:atom, url(:posts, :index, :format => :atom), :title => "ATOM")
#posts= partial 'posts/post', :collection => @postsNext, let us add the templates for atom using builder templates:
# app/views/posts/index.atom.builder
xml.instruct!
xml.feed "xmlns" => "http://www.w3.org/2005/Atom" do
xml.title "Padrino Sample Blog"
xml.link "rel" => "self", "href" => url_for(:posts, :index)
xml.id url_for(:posts, :index)
xml.updated @posts.first.created_at.strftime "%Y-%m-%dT%H:%M:%SZ" if @posts.any?
xml.author { xml.name "Padrino Team" }
@posts.each do |post|
xml.entry do
xml.title post.title
xml.link "rel" => "alternate", "href" => url_for(:posts, :show, :id => post)
xml.id url_for(:posts, :show, :id => post)
xml.updated post.created_at.strftime "%Y-%m-%dT%H:%M:%SZ"
xml.author {}
end
end
endand also the template for rss using builder:
# app/views/posts/index.rss.builder
xml.instruct!
xml.rss "version" => "2.0", "xmlns:dc" => "http://dublincore.org/documents/dc-xml-guidelines/" do
xml.channel do
xml.title "Padrino Blog"
xml.description "The fantastic padrino sample blog"
xml.link url_for(:posts, :index)
for post in @posts
xml.item do
xml.title post.title
xml.description post.body
xml.pubDate post.created_at
xml.link url_for(:posts, :show, :id => post.id)
end
end
end
endPlease note, that you have to add builder in your Gemfile and run bundle.
Let's check out our changes. View the available feeds at
http://localhost:3000/posts.atom or http://localhost:3000/posts.rss.
You now have rss and atom feeds available for your blog!