Rails respond_to Made It Too Easy

13 Jul 2009
I recently had a spike in traffic (1-2 visits/day to 30-40 visits/day) over at CatechizeMe.com. Someone out on the internet came across it, found it useful, and linked to it. Yay! With this new traffic, came some new requests for features. The first was a request for a Google Gadget. I didn't know much about what I needed to create a Gadget, but after looking it up I realized I could use the CatechizeMe API that came automatically when I built the app. With just a few lines of code, the Daily Question service was created and returning JSON data. You gotta love it when things are this easy. BEFORE:
  def daily_question
    @question = @catechism.daily_question
    render :template => '/questions/show'
  end
AFTER:
  def daily_question
    @question = @catechism.daily_question
    respond_to do |wants|
      wants.html { render :template => '/questions/show' }
      wants.js { render_json @question.to_json }
    end
  end
So now I get a daily catechism question from the CatechizeMe website or via the Google Gadget using JSON:

blog comments powered by Disqus