Home arrow Ruby-on-Rails arrow Page 3 - Adding an RSS Feed to an Online Book Catalog

Adding an RSS Feed to an Online Book Catalog

In this conclusion to a three-part series on building an online book catalog application with Ruby-on-Rails, we'll finish creating the search ability and add both a regular page and an RSS feed that lets users see the latest book titles to arrive at the store. This article is excerpted from chapter four of the book Practical Rails Projects, written by Eldon Alameda (Apress; ISBN: 1590597818).

Author Info:
By: Apress Publishing
Rating: 5 stars5 stars5 stars5 stars5 stars / 5
May 19, 2010
  1. · Adding an RSS Feed to an Online Book Catalog
  2. · Implementing the Get Latest Books User Story
  3. · Creating an RSS Feed

print this article

Adding an RSS Feed to an Online Book Catalog - Creating an RSS Feed
(Page 3 of 3 )

Creating an RSS feed in Rails is painstakingly easy. RSS feeds are essentially just XML files served like normal HTML pages. Rails supports three kinds of template files out of the box. You are already familiar with the HTML templates with the .rhtmlsuffix. The second type is the Builder XML template, with an.rxmlsuffix, which we will use for this case. The third type? You will learn about that in the next chapter, and boy will that be fun. But first we’ll create an RSS feed for Jill.

Once more, we’ll create another method in our integration test. Add the following method to theBrowsingTestDSLmodule intest/integration/browsing_and_searching_test.rb:

def reads_rss
get "/catalog/rss"
assert_response :success
assert_template "catalog/rss"
assert_equal "application/xml", response.headers["type"]

  assert_tag      :tag => "channel",
                  :children =>
                    { :count => 10, :only =>
                      {:tag => "item"}}
  Book.latest.each do |book|
    assert_tag    :tag => "title", :content => book.title

The method follows the familiar scheme. However, this time we also check that the response type is XML instead of HTML. It is also worth noting that we can use the sameassert_tagmethods here that we used for HTML documents, even though the output is XML.

Just as before, we call our new method from thetest_browsing_the_sitetest method:

def test_browsing_the_site
jill = enter_site(:jill)
jill.get_book_details_for "Pride and Prejudice"

Implementing the controller method is straightforward. We use the same information as in thelatestaction, so we can call it the same way we call any other method. After that, we just render our action normally, only this time, we don’t want to use the layout file (because we’re rendering XML).

def rss
  render :layout => false

The view file is where the most difference between a normal HTML page and a Rails-powered RSS feed lies. This time, we don’t use the standard.rhtmltemplates, but rather.rxmltemplates powered by the Builder library. With Builder, XML output is specified using nested code blocks. For our RSS feed, we’ll create theapp/views/catalog/rss.rxmlfile, as shown in Listing 4-3.

Listing 4-3. app/views/catalog/rss.rxml

xml.instruct! :xml, :version=>"1.0", :encoding=>"UTF-8"

xml.rss("version" => "2.0", "xmlns:dc" => "http://purl.org/dc/elements/1.1/") do
xml.channel do
    xml.title @page_title
    xml.link(url_for(:action => "index", :only_path => false))
    xml.language "en-us"
    xml.ttl "40"
    xml.description "Emporium: Books for people"

    for book in @books
xml.item do
        xml.description("#{book.title} by #{book.author_names}")
        xml.guid(url_for(:action => "show", :id => book, :only_path => false))
        xml.link(url_for(:action => "show", :id => book, :only_path => false))

Every code block started by anxml.tagcommand in a Builder template will result in a<tag>element in the output. Thus, the output of the code in Listing 4-3 would look something like this:

<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
    <title>Latest Books</title>
    <description>Emporium: Books for people</description>

      <title>The Idiot</title>
      <description>The Idiot by Fyodor Dostoyevsky</description>
      <pubDate>April 26, 2006 20:18</pubDate>

  ... more items ...


Note that we can use all the normal Rails helper methods, likeurl_for, in.rxmltemplates, just as in normal.rhtmlviews. However, because we’re not creating the XML code by hand, we can be sure that the output is always well-formed XML.

Running the integration test reveals that everything works fine. Encouraged, we openhttp://localhost:3000/catalog/rssin a browser that supports RSS feeds (such as Safari on Mac OS X or Firefox on other platforms) and show George how the feed functionality works for Jill, as shown in Figure 4-5. George is excited, and we can pat ourselves on the back. Another sprint is completed.

Figure 4-5.  Working RSS feed


In this chapter, we implemented the basic functionality of the online bookstore that is visible to a normal user like Jill. This consisted of four user stories: browsing the list of books, searching books, visiting pages for individual books, and seeing lists of latest books in the store in both a web page and an RSS form.

During the course of the chapter, we showed you how to use theincludeparameter in ActiveRecord finder methods to avoid unnecessary SQL queries and use layouts to avoid repeating view code. We also integrated the Ferret full-text search engine with our Rails application using the acts_as_ferret plugin. Finally, we created RSS feeds using Builder XML templates, which saved us a lot of time. In the next chapter we will create a shopping cart for Jill to fill. 

DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

blog comments powered by Disqus

- Ruby-on-Rails Faces Second Security Flaw in ...
- Ruby 2.0 Prepped for February 2013 Release
- Why LinkedIn Switched from Ruby on Rails
- Adding Style with Action Pack
- Handling HTML in Templates with Action Pack
- Filters, Controllers and Helpers in Action P...
- Action Pack and Controller Filters
- Action Pack Categories and Events
- Logging Out, Events and Templates with Actio...
- Action Pack Sessions and Architecture
- More on Action Pack Partial Templates
- Action Pack Partial Templates
- Displaying Error Messages with the Action Pa...
- Action Pack Request Parameters
- Creating an Action Pack Registration Form

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials