<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xml:base="http://townx.org" xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
 <title>townx - Ruby Tuesday: RSS feeds in Rails - Comments</title>
 <link>http://townx.org/blog/elliot/ruby_tuesday_rss_feeds_in_rails</link>
 <description>Comments for &quot;Ruby Tuesday: RSS feeds in Rails&quot;</description>
 <language>en</language>
<item>
 <title>How do you add belongs_to associations into builder</title>
 <link>http://townx.org/blog/elliot/ruby_tuesday_rss_feeds_in_rails#comment-39818</link>
 <description>&lt;p&gt;How would you do a belongs_to association.  For example if a person is in a photo and the photo could contain many people how do you display person.photo.title with xml builder.  Displaying the data from a has_many relationship is simple using for person in @photo.people but for a belongs_to relationship I cant figure out how to do @person.photo.title(obviously this isn&#039;t working for me) in xml builder.  This example is a little strange but for my purposes this is the type relationship i&#039;m trying to display in xml.  any ideas?&lt;/p&gt;

&lt;p&gt;Assumption: person only shows up in one picture. &lt;/p&gt;

&lt;p&gt;class Person &amp;lt; ActiveRecord::Base&lt;br /&gt;
  belongs_to :photo&lt;br /&gt;
end&lt;/p&gt;

&lt;p&gt;class Photo &amp;lt; ActiveRecord::Base&lt;br /&gt;
  has_may :people&lt;br /&gt;
end&lt;/p&gt;</description>
 <pubDate>Wed, 19 Aug 2009 13:27:02 -0500</pubDate>
 <dc:creator>Dugels</dc:creator>
 <guid isPermaLink="false">comment 39818 at http://townx.org</guid>
</item>
<item>
 <title>Not sure about that, as the</title>
 <link>http://townx.org/blog/elliot/ruby_tuesday_rss_feeds_in_rails#comment-39481</link>
 <description>&lt;p&gt;Not sure about that, as the source for the &lt;span class=&quot;caps&quot;&gt;RSS &lt;/span&gt;feed comes from the database. Are you caching db objects?&lt;/p&gt;</description>
 <pubDate>Wed, 27 May 2009 03:30:05 -0500</pubDate>
 <dc:creator>elliot</dc:creator>
 <guid isPermaLink="false">comment 39481 at http://townx.org</guid>
</item>
<item>
 <title>My rssfeed is not getting updatde</title>
 <link>http://townx.org/blog/elliot/ruby_tuesday_rss_feeds_in_rails#comment-39400</link>
 <description>&lt;p&gt;Hi i have adding rssfeed in my browser from my application. If add new updates those updates are not getting updated in my rssfeed...&lt;br /&gt;
Can you say me whats going wrong..&lt;/p&gt;

&lt;p&gt;Thanks in advance...&lt;/p&gt;</description>
 <pubDate>Tue, 19 May 2009 06:35:28 -0500</pubDate>
 <dc:creator>saranya</dc:creator>
 <guid isPermaLink="false">comment 39400 at http://townx.org</guid>
</item>
<item>
 <title>You&#039;re welcome, glad it was</title>
 <link>http://townx.org/blog/elliot/ruby_tuesday_rss_feeds_in_rails#comment-38453</link>
 <description>&lt;p&gt;You&#039;re welcome, glad it was useful.&lt;/p&gt;</description>
 <pubDate>Thu, 04 Sep 2008 14:00:08 -0500</pubDate>
 <dc:creator>elliot</dc:creator>
 <guid isPermaLink="false">comment 38453 at http://townx.org</guid>
</item>
<item>
 <title>Thanks a lot for the example.</title>
 <link>http://townx.org/blog/elliot/ruby_tuesday_rss_feeds_in_rails#comment-38450</link>
 <description>&lt;p&gt;Thanks a lot for the example.&lt;/p&gt;</description>
 <pubDate>Wed, 03 Sep 2008 20:50:43 -0500</pubDate>
 <dc:creator>grateful visitor</dc:creator>
 <guid isPermaLink="false">comment 38450 at http://townx.org</guid>
</item>
<item>
 <title>The simplest approach would</title>
 <link>http://townx.org/blog/elliot/ruby_tuesday_rss_feeds_in_rails#comment-38257</link>
 <description>&lt;p&gt;The simplest approach would be to give each user an authentication token (e.g. a sha1 hash of their username + password + salt) which they append to the &lt;span class=&quot;caps&quot;&gt;RSS URL &lt;/span&gt;to get an authenticated feed. The &lt;span class=&quot;caps&quot;&gt;RSS &lt;/span&gt;action only returns the feed if the token is present. That would probably do it.&lt;/p&gt;</description>
 <pubDate>Thu, 26 Jun 2008 01:24:09 -0500</pubDate>
 <dc:creator>elliot</dc:creator>
 <guid isPermaLink="false">comment 38257 at http://townx.org</guid>
</item>
<item>
 <title>What about authentication?</title>
 <link>http://townx.org/blog/elliot/ruby_tuesday_rss_feeds_in_rails#comment-38232</link>
 <description>&lt;p&gt;In my site a user has to login.  I&#039;ve made it work so that the &lt;span class=&quot;caps&quot;&gt;RSS &lt;/span&gt;feed is for them, but I&#039;m not sure how to get them to be able to loggin before hand.  Any ideas?  I&#039;m just using basic salt/md5 hashing and sessions to store the user.&lt;/p&gt;</description>
 <pubDate>Tue, 24 Jun 2008 11:50:39 -0500</pubDate>
 <dc:creator>Joseph</dc:creator>
 <guid isPermaLink="false">comment 38232 at http://townx.org</guid>
</item>
<item>
 <title>h not necessary</title>
 <link>http://townx.org/blog/elliot/ruby_tuesday_rss_feeds_in_rails#comment-38231</link>
 <description>&lt;p&gt;You don&#039;t need to use the h function here. Stuff you put in is automatically encoded.&lt;/p&gt;</description>
 <pubDate>Mon, 23 Jun 2008 18:30:29 -0500</pubDate>
 <dc:creator>Lawrence Pit</dc:creator>
 <guid isPermaLink="false">comment 38231 at http://townx.org</guid>
</item>
<item>
 <title>Glad you found it useful.</title>
 <link>http://townx.org/blog/elliot/ruby_tuesday_rss_feeds_in_rails#comment-14337</link>
 <description>&lt;p&gt;Glad you found it useful. Happy &lt;span class=&quot;caps&quot;&gt;RSS&lt;/span&gt;-feeding!&lt;/p&gt;</description>
 <pubDate>Thu, 06 Sep 2007 09:32:23 -0500</pubDate>
 <dc:creator>elliot</dc:creator>
 <guid isPermaLink="false">comment 14337 at http://townx.org</guid>
</item>
<item>
 <title>Thnks Helps me a lot</title>
 <link>http://townx.org/blog/elliot/ruby_tuesday_rss_feeds_in_rails#comment-14324</link>
 <description>&lt;p&gt;Hey thank u very much this helps me a lot.&lt;/p&gt;</description>
 <pubDate>Thu, 23 Aug 2007 07:44:43 -0500</pubDate>
 <dc:creator>Arun Agrawal</dc:creator>
 <guid isPermaLink="false">comment 14324 at http://townx.org</guid>
</item>
<item>
 <title>Hang on a minute: you&#039;re a</title>
 <link>http://townx.org/blog/elliot/ruby_tuesday_rss_feeds_in_rails#comment-14282</link>
 <description>&lt;p&gt;Hang on a minute: you&#039;re a tricksy spambot, aren&#039;t you? Go on, admit it.&lt;/p&gt;</description>
 <pubDate>Thu, 02 Aug 2007 09:38:24 -0500</pubDate>
 <dc:creator>elliot</dc:creator>
 <guid isPermaLink="false">comment 14282 at http://townx.org</guid>
</item>
<item>
 <title>You&#039;re welcome, glad it was</title>
 <link>http://townx.org/blog/elliot/ruby_tuesday_rss_feeds_in_rails#comment-14274</link>
 <description>&lt;p&gt;You&#039;re welcome, glad it was helpful.&lt;/p&gt;</description>
 <pubDate>Sun, 22 Jul 2007 01:31:43 -0500</pubDate>
 <dc:creator>elliot</dc:creator>
 <guid isPermaLink="false">comment 14274 at http://townx.org</guid>
</item>
<item>
 <title>You&#039;re quite right. I&#039;ve</title>
 <link>http://townx.org/blog/elliot/ruby_tuesday_rss_feeds_in_rails#comment-14273</link>
 <description>&lt;p&gt;You&#039;re quite right. I&#039;ve included your refinement above.&lt;/p&gt;</description>
 <pubDate>Sun, 22 Jul 2007 01:31:24 -0500</pubDate>
 <dc:creator>elliot</dc:creator>
 <guid isPermaLink="false">comment 14273 at http://townx.org</guid>
</item>
<item>
 <title>Great Post</title>
 <link>http://townx.org/blog/elliot/ruby_tuesday_rss_feeds_in_rails#comment-14272</link>
 <description>&lt;p&gt;This was a great example. Thank you very much.&lt;/p&gt;

&lt;p&gt;Do you need to parse the date? I&#039;m just using&lt;br /&gt;
myobj.created_at.rfc822&lt;/p&gt;</description>
 <pubDate>Sun, 22 Jul 2007 00:39:52 -0500</pubDate>
 <dc:creator>Guest</dc:creator>
 <guid isPermaLink="false">comment 14272 at http://townx.org</guid>
</item>
<item>
 <title>thank you.</title>
 <link>http://townx.org/blog/elliot/ruby_tuesday_rss_feeds_in_rails#comment-14016</link>
 <description>&lt;p&gt;thany You.&lt;br /&gt;
i&#039;m korean.&lt;/p&gt;

&lt;p&gt;i find rss feed make. examples. :D&lt;br /&gt;
you goood job :D thany you&lt;/p&gt;</description>
 <pubDate>Mon, 12 Feb 2007 00:41:11 -0600</pubDate>
 <dc:creator>LinDol</dc:creator>
 <guid isPermaLink="false">comment 14016 at http://townx.org</guid>
</item>
<item>
 <title>Ruby Tuesday: RSS feeds in Rails</title>
 <link>http://townx.org/blog/elliot/ruby_tuesday_rss_feeds_in_rails</link>
 <description>&lt;p&gt;Yesterday, I mentioned I have added &lt;span class=&quot;caps&quot;&gt;RSS &lt;/span&gt;feed capabilities to &lt;a href=&quot;http://flickrlilli.org.uk/&quot;&gt;FlickrLilli&lt;/a&gt;. I thought it might be worth sharing my experiences of this, as it took me a good couple of hours to iron out the wrinkles and get a valid feed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Update:&lt;/strong&gt; Thanks to Thomas Hurst (see comments) for pointing out that I can just use the output from the existing partial and escape it again.&lt;/p&gt;

&lt;h2&gt;Existing components&lt;/h2&gt;

&lt;p&gt;I had a lot of code for fetching photos from Flickr and displaying them as &lt;span class=&quot;caps&quot;&gt;HTML&lt;/span&gt;: I wanted to reuse these as much as possible. The examples below simplify, as FlickrLilli is a special case and doesn&#039;t have a database back-end; for the example, I have assumed a photo database which stores Flickr-like photo objects.&lt;/p&gt;

&lt;h3&gt;The model&lt;/h3&gt;

&lt;p&gt;FlickrLilli doesn&#039;t use ActiveRecord, but has objects corresponding to photos from Flickr. For the purposes of this example, let&#039;s assume a photo actually corresponds to a table in the database called &lt;code&gt;photos&lt;/code&gt;, and has a model that looks like this:&lt;/p&gt;



&lt;pre&gt;
class Photo &amp;lt; ActiveRecord::Base
  has_and_belongs_to_many :tags
  belongs_to :owner
  
  # Show tag names in a human-readable string.
  # (Thanks to Tim [see comments] for reminding me of this syntax.)
  def tag_str
    tags.map(&amp;amp;:name).join(&amp;quot;, &amp;quot;)
  end
end
&lt;/pre&gt;



&lt;p&gt;And that the model supplies these attributes (which mirrors Flickr metadata on a photo):&lt;/p&gt;


&lt;ul&gt;
&lt;li&gt;title&lt;/li&gt;
&lt;li&gt;description&lt;/li&gt;
&lt;li&gt;date_taken&lt;/li&gt;
&lt;li&gt;image_url (the &lt;span class=&quot;caps&quot;&gt;URL &lt;/span&gt;to the actual photo graphic file)&lt;/li&gt;
&lt;li&gt;tags (returns an array of Tag model objects)&lt;/li&gt;
&lt;li&gt;licence&lt;/li&gt;
&lt;li&gt;owner (an instance of an Owner model object, with a username and homepage &lt;span class=&quot;caps&quot;&gt;URL&lt;/span&gt;)&lt;/li&gt;
&lt;/ul&gt;



&lt;h3&gt;The standard partial for rendering a photo&lt;/h3&gt;

&lt;p&gt;FlickrLilli uses a single partial to render a photo as &lt;span class=&quot;caps&quot;&gt;HTML &lt;/span&gt;in different contexts (app/views/main/_metadata.rhtml). Paraphrasing a bit, this looks like:&lt;/p&gt;



&lt;pre&gt;
&amp;lt;h2&amp;gt;&amp;lt;%=h photo.title %&amp;gt;&amp;lt;/h2&amp;gt;
&amp;lt;p&amp;gt;&amp;lt;%= image_tag(photo.image_url) %&amp;gt;&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;&amp;lt;strong&amp;gt;Tags:&amp;lt;/strong&amp;gt; &amp;lt;%=h photo.tag_str %&amp;gt;&amp;lt;/p&amp;gt;
&amp;lt;div class=&amp;quot;photo_details&amp;quot;&amp;gt;
&amp;lt;p&amp;gt;&amp;lt;strong&amp;gt;Licence:&amp;lt;/strong&amp;gt; &amp;lt;%= photo.licence %&amp;gt;&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;&amp;lt;strong&amp;gt;Date and time taken:&amp;lt;/strong&amp;gt; &amp;lt;%= photo.date_taken %&amp;gt;&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;&amp;lt;strong&amp;gt;Owner:&amp;lt;/strong&amp;gt; &amp;lt;%= link_to photo.owner.username, photo.owner.homepage %&amp;gt;&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;&amp;lt;strong&amp;gt;Description:&amp;lt;/strong&amp;gt;&amp;lt;br/&amp;gt;
&amp;lt;%=h photo.description %&amp;gt;
&amp;lt;/p&amp;gt;
&lt;/pre&gt;



&lt;h2&gt;The &lt;span class=&quot;caps&quot;&gt;RSS &lt;/span&gt;feed generator&lt;/h2&gt;

&lt;p&gt;Given these two existing components (a model and an &lt;span class=&quot;caps&quot;&gt;HTML &lt;/span&gt;partial for a photo), I set about writing my feed generator. It needed:&lt;/p&gt;


&lt;ol&gt;
&lt;li&gt;A controller action to manage the feed&lt;/li&gt;
&lt;li&gt;An &lt;span class=&quot;caps&quot;&gt;RXML &lt;/span&gt;template for rendering it (RXML uses Builder to generate &lt;span class=&quot;caps&quot;&gt;XML &lt;/span&gt;output for controller actions)&lt;/li&gt;
&lt;li&gt;A route which makes it convenient to download the raw feed &lt;span class=&quot;caps&quot;&gt;XML &lt;/span&gt;as an &lt;span class=&quot;caps&quot;&gt;XML &lt;/span&gt;file with a .xml suffix&lt;/li&gt;
&lt;/ol&gt;



&lt;h3&gt;The controller action for the &lt;span class=&quot;caps&quot;&gt;RSS &lt;/span&gt;feed&lt;/h3&gt;

&lt;p&gt;This was pretty quick to write. Again, using a simplified version for the purposes of exposition, I added the action to my MainController class (in app/controllers/main_controller.rb):&lt;/p&gt;



&lt;pre&gt;
def rss
  # Get the 10 most recent photos
  @photos = Photo.find :all, :limit =&amp;gt; 10, :order =&amp;gt; &#039;date_taken DESC&#039;
  # Title for the RSS feed
  @feed_title = &amp;quot;10 most recent photos&amp;quot;
  # Get the absolute URL which produces the feed
  @feed_url = &amp;quot;http://&amp;quot; + request.host_with_port + request.request_uri
  # Description of the feed as a whole
  @feed_description = &amp;quot;10 most recent photos&amp;quot;
  # Set the content type to the standard one for RSS
  response.headers[&#039;Content-Type&#039;] = &#039;application/rss+xml&#039;
  # Render the feed using an RXML template
  render :action =&amp;gt; &#039;rss&#039;, :layout =&amp;gt; false
end
&lt;/pre&gt;



&lt;p&gt;There are a couple of key things to note here:&lt;/p&gt;


&lt;ol&gt;
&lt;li&gt;The Content-Type header should be set properly for the response.&lt;/li&gt;
&lt;li&gt;The &lt;span class=&quot;caps&quot;&gt;RXML &lt;/span&gt;template needs to be rendered without a layout, otherwise you get &lt;span class=&quot;caps&quot;&gt;XML &lt;/span&gt;wrapped in the controller&#039;s layout.&lt;/li&gt;
&lt;/ol&gt;



&lt;h3&gt;The feed &lt;span class=&quot;caps&quot;&gt;XML &lt;/span&gt;template&lt;/h3&gt;

&lt;p&gt;This uses the &lt;span class=&quot;caps&quot;&gt;RXML &lt;/span&gt;template provided by Rails to generate the &lt;span class=&quot;caps&quot;&gt;XML &lt;/span&gt;document. There is an example in the Rails documentation, but as far as I can see it doesn&#039;t produce a properly-valid feed, as it lacks an &amp;lt;?xml version=&quot;1.0&quot;?&amp;gt; declaration. Here&#039;s my template, which went into app/views/main/rss.rxml:&lt;/p&gt;



&lt;pre&gt;
xml.instruct!
xml.rss(&amp;quot;version&amp;quot; =&amp;gt; &amp;quot;2.0&amp;quot;, &amp;quot;xmlns:dc&amp;quot; =&amp;gt; &amp;quot;http://purl.org/dc/elements/1.1/&amp;quot;) do
  xml.channel do
    xml.title @feed_title
    xml.link @feed_url
    xml.description @feed_description
    xml.language &amp;quot;en-gb&amp;quot;

    for photo in @photos
      xml.item do
        xml.pubDate photo.date_taken.rfc822
        xml.title h(photo.title)
        xml.link photo.image_url
        xml.guid photo.image_url
        xml.description do
          xml &amp;lt;&amp;lt; h(render(:partial =&amp;gt; &#039;metadata&#039;, :locals =&amp;gt; {:photo =&amp;gt; photo}))
        end
      end
    end
  end
end
&lt;/pre&gt;



&lt;p&gt;Things of note:&lt;/p&gt;


&lt;ul&gt;
&lt;li&gt;A guid element is added for each item. This enables blog readers to uniquely identify items, meaning it can detect which items in a feed are new and which have already been seen. In this case, I&#039;m just using the image &lt;span class=&quot;caps&quot;&gt;URL&lt;/span&gt;; in the case of FlickrLilli, I use the &lt;span class=&quot;caps&quot;&gt;URL &lt;/span&gt;of the photo detail page on FlickrLilli, which ensures the feed only shows items which are new, not updates to existing items.&lt;/li&gt;
&lt;li&gt;The Time class in Ruby provides a handy instance method called &lt;code&gt;rfc822&lt;/code&gt;, which returns a string suitable for use inside the &amp;lt;pubDate&amp;gt; element of an &lt;span class=&quot;caps&quot;&gt;RSS &lt;/span&gt;feed.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;xml &amp;lt;&amp;lt; h(...)&lt;/code&gt; syntax directly appends some content to the &lt;span class=&quot;caps&quot;&gt;XML &lt;/span&gt;output, attaching it to the currently active element (in this case, the &amp;lt;description&amp;gt;).&lt;/li&gt;
&lt;li&gt;The partial is rendered using the standard &lt;code&gt;render&lt;/code&gt; method, passing the photo in as a local. The whole lot is then filtered through the &lt;code&gt;h&lt;/code&gt; method to escape all the &lt;span class=&quot;caps&quot;&gt;HTML.&lt;/span&gt; This will result in some parts of the &lt;span class=&quot;caps&quot;&gt;HTML &lt;/span&gt;being &quot;double escaped&quot;; however, when decoded by the reader at the other end, it should be proper single-escaped &lt;span class=&quot;caps&quot;&gt;HTML &lt;/span&gt;again.&lt;/li&gt;
&lt;/ul&gt;



&lt;h3&gt;A nice route&lt;/h3&gt;

&lt;p&gt;Applications like Firefox look at the filename suffix of a downloaded file to determine how to handle it. If we make the &lt;span class=&quot;caps&quot;&gt;RSS &lt;/span&gt;feed available at a &lt;span class=&quot;caps&quot;&gt;URL &lt;/span&gt;like &lt;strong&gt;http://localhost/rss&lt;/strong&gt;, the downloaded file will be called &lt;strong&gt;rss&lt;/strong&gt;, and some applications won&#039;t be able to work out what to do with it.&lt;/p&gt;

&lt;p&gt;So it gets properly treated as an &lt;span class=&quot;caps&quot;&gt;XML &lt;/span&gt;file, I add a custom route to config/routes.rb:&lt;/p&gt;



&lt;pre&gt;
map.connect &#039;/rss.xml&#039;, :controller =&amp;gt; &#039;main&#039;, :action =&amp;gt; &#039;rss&#039;
&lt;/pre&gt;



&lt;p&gt;And that&#039;s it!&lt;/p&gt;

&lt;h2&gt;Bonus sample code&lt;/h2&gt;

&lt;p&gt;Because I&#039;m feeling generous, and because I wanted to check all the above actually worked, I put together a really simple Rails application which fleshes this out and includes a sample &lt;span class=&quot;caps&quot;&gt;SQL&lt;/span&gt;ite database and migrations. It&#039;s attached below. To run it, just use script/server, then browse to &lt;a href=&quot;http://localhost:3000/rss.xml&quot; title=&quot;http://localhost:3000/rss.xml&quot;&gt;http://localhost:3000/rss.xml&lt;/a&gt;. When I tested it, it produced a valid &lt;span class=&quot;caps&quot;&gt;RSS &lt;/span&gt;feed.&lt;/p&gt;</description>
 <comments>http://townx.org/blog/elliot/ruby_tuesday_rss_feeds_in_rails#comments</comments>
 <category domain="http://townx.org/tech">tech</category>
 <enclosure url="http://townx.org/files/rss.zip" length="73656" type="application/zip" />
 <pubDate>Tue, 12 Dec 2006 15:35:00 -0600</pubDate>
 <dc:creator>elliot</dc:creator>
 <guid isPermaLink="false">471 at http://townx.org</guid>
</item>
</channel>
</rss>
