0.3.0: Adding RSS/xml feeds to our Blog demo

Ok, as you remember a while back we created a simple blog using mack, http://www.mackframework.com/2008/03/04/the-obligatory-blog-demo/. Well now it’s time to add the all important RSS/xml feed to it.

Mack 0.3.0 introduces xml rendering support natively, so this shouldn’t be so hard. First things first, let’s fire up the app, shall we:

$ rake server

Now let’s head over to http://localhost:3000/posts. We should see our beautiful posts index page. Now let’s try to go to http://localhost:3000/posts.xml you should see something that looks like this:

XML blog demo 1

Clearly, that’s not what we want, is it? I didn’t think so. The error is telling us that it’s looking for a file called index.xml.erb in the app/views/posts directory of our blog project. Obviously that file doesn’t exist.

Let’s take a second and talk about why Mack was looking for index.xml.erb. We haven’t changed anything in our controller. Our index method still looks something like this:

def index
  @posts = Post.find(:all)
end

No where in there does it mention xml. The only place xml is mentioned is on the the url itself, remember? We looked for /posts.xml. By adding .xml you’re telling Mack that you want to render, well… xml. So it goes looking for that. That’s also new in 0.3.0. The default is html, but if you append a format (.js, .xml, etc…), it will go looking for app/views/<controller_name>/<action_name>.<format>.erb and render it.

Ok, now that we understand why we’re looking for an xml file, let’s fire up our trusty text editor and create a new file called: app/views/posts/index.xml.erb. Let’s edit the file to look like this:

xml.instruct! :xml, :version=>"1.0"
xml.rss(:version => "2.0") do
  xml.channel do
    xml.title("My Mack Blog")
    xml.link(posts_index_full_url)
    xml.description("Find out about all the cool stuff happening on my blog!")
    xml.language("en-us")
    xml.copyright("Copyright Me")
    xml.pubDate(CGI.rfc1123_date(Time.now))
    xml.lastBuildDate(CGI.rfc1123_date(Time.now))
    @posts.each do |post|
      xml.entry do
        xml.title(post.title)
        xml.link(posts_show_full_url(:id => post.id))
        xml.description(post.body)
        xml.pubDate(post.created_at.strftime("%a, %d %b %Y %H:%M:%S"))
      end
    end
  end
end

Mack uses the standard builder gem library. I’m not going to go into explaining how that works, there are plenty of other tutorials and documentation that will show you that. I’m also not going to explain all the necessary pieces of an RSS feed. Instead I’ll point out in that code you’ll see we’re using the @posts instance variable that we set in the index action of our PostsController. Just like regular *.html.erb files we have access to all the instance variables from the controller, as well, helpers, etc…

So now if we go to http://localhost:3000/posts.xml we should see our RSS feed. If we did a view source we should see something that looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
 <channel>
  <title>My Mack Blog</title>
  <link>http://localhost:3000/posts</link>
  <description>Find out about all the cool stuff happening on my blog!</description>
  <language>en-us</language>
  <copyright>Copyright Me</copyright>
  <pubDate>Tue, 18 Mar 2008 17:18:05 GMT</pubDate>
  <lastBuildDate>Tue, 18 Mar 2008 17:18:05 GMT</lastBuildDate>
  <entry>
   <title>My New Post</title>
   <link>http://localhost:3000/posts/1</link>
   <description>This is my first post in my cool Mack blog!</description>
   <pubDate>Tue, 18 Mar 2008 11:58:30</pubDate>
  </entry>
 </channel>
</rss>

Awesome! All that’s really left is create one of those fancy RSS tags in the location field of our browsers that people can click and go straight to the RSS feed. Let’s do that now.

At the top of your app/views/posts/index.html.erb file add the following:

<%= rss_tag(posts_index_url(:format => :xml)) %>

Now, refresh the page in your browser, and there you go, you should now see the little RSS button in the location bar of your browser. If you click that you should be taken to your feed.

That’s all there is to adding not only xml, but an RSS feed to your new blog.

The code for this demo can be found here.

The History Of Mack, pt. 1

Let me start by answering the question at the top of your head, “why another ruby web application framework?” Great question.

I work for a company that for the time being shall be called Menderchuck. Menderchuck has been using Ruby on Rails since the company started two years ago. I am employee number three at Menderchuck, and was hired directly by the VP of Development. I was hired as the Senior Software Architect. I’m now the Director of Architecture for the company. :) Sorry about the bragging, I just like telling people I’m a director!

Prior to joining the company I had been using Rails for about 6 months, long before v1.0.0 of the now widely used framework. I loved Rails. I still do in fact, well, kind of. More on that later. Rails was the only choice for developing Menderchuck. The VP of Development and myself were huge fans, and having both come from Java backgrounds, we loved the flexibility and fun of Ruby as a language and Rails as a framework.

As I said we’ve spent the past two years developing Menderchuck using Rails, and for the most part things have been OK. I can’t say that they’ve been great, because, well, they haven’t. We’ve had scalability problems, deployment problems, and most importantly problems with the constraints that Rails places on development team.

The idea of an opinionated framework is great, in theory. If you follow and play by their rules things are great. The problem arises if you want to stray from the beaten path. Then you are left out on your own. Left to forge your own path, and as we’ve found at Menderchuck, you end up doing a whole hell of a lot of hacking!

Rails was designed for, and is incredible for building Web 2.0 applications. Menderchuck, although touted by the CEO as a Web 2.0 application, is really more of a complex and large scale web application, in the flavor of Web 1.0 applications. It’s also more of a portal, which can be a very difficult thing to build with the Rails framework.

Now don’t get me wrong, you CAN build a portal using Rails, but it really involves turning the framework on it’s head and kicking it in the neck. That just shouldn’t be the way build things.

Enter Mack. (More to come…)