Fragement Caching Tip

Posted by Jonathan

We are in the process of testing the new version of MeinProf.de and crossed a very interesting bug.

We are using fragment caching to speed up the serving of the university homepages. So far this is working great and helps a lot with coping with extra load.

The university homepages are served by unis/show.html.erb:

  ...
  <%= render :partial => 'shared/user_info' %>
  ...
  <% cache "uni_page_homepage_#{@uni.id}", :expires_in => 1.hour do %>
    <%= render :partial => 'unis/header' %>
    <%= render :partial => 'tabs_header' %>
    <div class="uni_desc">
      <div class="tab-content">
       ...
       <!-- a lot of uni content here-->
  <% end %>

The partial unis/_header.html.erb looks like this:

  ...
<% content_for("breadcrumb") do %>
  <%= uni_breadcrumb(uni, department) %>
  <div class="breakcrumb">
    ...
  </div>
<% end %>

<div class="subcolumns">
  <h1><%=h uni.name %></h1>
  <div class="c08l badge">
  <p>
    <strong>Homepage:</strong>....
   ....
  </p>

So far so good. When we deployed to our staging host we noticed that the university header was not always right. On the first request it was correct and on every subsequent request the header was missing the breadcrumb. Errors like this sounds like caching issues and it took us a while to figure it out.

The problem is the content_for block inside the partial. Once you think about it, it makes a lot of sense. During the first request the show-template gets evaluated. It calls the partial and the partial executes the content_for block. This block access the template binding/variables to inject content. The partial finishes rendering and stores the rendered content in memcached. Everything fine.

On the next request Rails loads up the action and renders the view. During the view rendering the fragment will be loaded from memcached. No bug here.

So why is the breadcrumb not showing up? Because the content_for block will not be executed. It is a side effect of the partial-rendering and the partial will not be rendered once it is cached. Only the resulting HTML-fragment will be loaded from the cache but the code-block is not executed. This means that there is not call to inject extra content to the main view.

So before you wrap some view code in fragment caching remember: do not fragment cache content_for-blocks. They will not be called. Always check cached views for content_for-blocks and move them outside the cache-call.

MeinProf featured on the German Amazon Affiliate Blog

Posted by Jonathan

It is not a secret that I am a fan of the Amazon Web Services. Besides EC2 and S3, I also use the Amazon Ecommerce Services (ECS) API at MeinProf.

Our implementation is now featured on the German Amazon Affiliate Blog.

We use the ECS API to let professors and students search for books and study guides on Amazon and then recommend the books to other students. So besides rating a course a student can also recommend supporting literature to others. This is the first step in our plans to increase MeinProfs value to students. We introduced this feature only a short while ago but already have many professors who register and recommend books to their students through our system.

The implementation uses the amazon-ecs ruby library that internally uses Hpricot, the great HTML/XML parser by _why.

The basic usage looks like this:

require 'amazon/ecs'

Amazon::Ecs.options = {
  :aWS_access_key_id => SECRET_KEY,
  :associate_tag => ASSOCIATE_TAG,
  :response_group => 'Medium',
  :country => :de
}

result_set = Amazon::Ecs.item_search('Programming Ruby', {
  :search_index => 'Books' # use Blended if you want also foreign books (usefull in DE and FR)
})

if result_set.has_errors?
  raise "Problem with ECS!" 
end

result_set.items.each do |item|
  puts item.get('title')
  puts item.get('asin')
  puts item.get('detailpageurl')
end

The difficult part is filtering and pre-processing the search term entered by the user in order to reduce the result set.

MeinProf.de - New Site On Rails

Posted by Jonathan

MeinProf.de, our newest Rails project is online!

MeinProf.de is a German site by Juniter where students can evaluate their university courses and thereby their professors. The professor’s rating is the calculated average rating of his courses. The idea is inspired by ratemyprofessor.com and will help students to choose their courses and give recommendations to other students.

One main feature of the site is the nice reporting.

  • Want to know who is the funniest professor in Berlin?
  • Which course is the toughest in Germany?
  • Which professor gives the best grades in Bavaria?

Just use our TopFlop listings to query this information.

The site is build with Ruby on Rails and uses Script.aculo.us and Prototype for JavaScript Effects and AJAX. We have an AJAX live search and use AJAX for course and professor name suggestion when students add them. Thanks to Ruby on Rails we could implement the whole site in our free time in less than a month. In more complicated frameworks like J2EE we would never be finished yet. We want to thank the Rails community for this wonderful framework and hope to contribute to Rails’ popularity with our site.

We got nearly 1000 ratings over the last couple of days and hope at least to keep this pace. If you study in Germany or know German students go to the website, spread the word, and give us feedback.