Post St.
 
Photo by Terry Bain

Post/Redirect/Get (PRG) is a web application design pattern used to supply users with nice, GET-generated results pages that bookmark and reload with ease.

Say you have a results page with a filtering form at the top. The user might alter filtering options, and submit a form to winnow down her results. This is a POST, meaning params will be hidden from the user (not in the URL), making direct bookmarks to the filtered results impossible. Also, reloading the page will result in a “POSTDATA” warning message (“Do you want to resubmit POST data or would you rather we do a hurkey-jerky dance to confuse you?”) to the user. These are not things people like to deal with. Post/Redirect/Get says that your application should accept the POST command, and redirect to the results page using a GET request.

This is really quite simple to implement in Rails. Take this example action filter for an expense report controller, reduced to its bare essentials:

class ExpenseReportsController < ApplicationController
  def filtered
    redirect_to fitered_expense_report_path(params) and return if request.post?
    
    # Actual action work here.
  end
end

Very easy - the trick is really knowing the pattern more than anything. There has been some discussion on adapting this to deal with validation failures. In my experience, I’ve only made use of this pattern in non-RESTful controllers. You are sure to do better with it than I.

Jul 22, 2008 · Subscribe · Archive · Projects · Twitter · GitHub · Flickr