LRBlog

Logical Reality Design: Web Design and Software Development

Using redirect_to :back to improve user interfaces

June 18, 2008

It’s a scourge that inflicts nearly every website: the dreaded empty page stating “Your search returned no results.” Or even worse, the search results page with a header “Search results” and zero content, the footer immediately following the header. This is not only useless, but also confusing to the user, because it takes the reader several seconds to even realize that the search returned no results.

For example, I just got this one on the otherwise lovely workingwithrails.com when I searched for RoR developers in Pasadena, CA:

A search results page with no results!

This is lame, lame, lame. A search results page with no results is guaranteed not to be useful to the user! From a user interface perspective, it’s akin to redirecting a user to a login page after they try to access some resource, and then sending them back to the front page or a super-lame “thank you for logging in” page afterwards.    The user should always see a useful page, and should never have to waste clicks getting back to what they wanted.

It’s not only bad UI, it’s also entirely unnecessary, because in Rails you can fix this for good with two lines of code.

Avoid wasted pages and annoyed users with redirect_to: back

If your user’s search doesn’t find anything, don’t make them waste their time at a whole page that tells them only that.  Instead just send them straight back to the search page.  Better yet, redirect them back to wherever they came from, since they might well be able to invoke search from any number of different contexts in your application. You don’t need a whole page to tell them the search didn’t find anything - a flash notice is more than sufficient:

Redirecting back with a flash after an empty search:

def search
  @results = YourModel.execute_some_search(params[:some_parameter]);
  if @results.size == 0
    flash[:notice] = "No items found matching '#{params[:some_parameter]}'"
    redirect_to :back
  end
end

Testing redirect_to :back

Testing redirect_to :back isn’t completely trivial, because of course there’s no previous page when you render a page in a test context. So, when you try to run a controller that redirects back, you’ll see an error. The easiest thing to do is to artificially set a previous page. I have these two methods in test/test_helper.rb:

def fake_referer
    @request.env['HTTP_REFERER'] = 'http://previous_page'
  end
  
  def assert_redirected_back
    assert_redirected_to 'http://previous_page'
  end

Then anytime I am working on a controller that involves back redirects, I drop fake_referrer in my setup method and then use assert_redirected_back. For example, in a search controller test (this is from a controller that has an action called ‘contents’ for searching items by text content - the search itself uses ferret):

def setup
    super
    fake_referer
  end

  def test_contents_search_fails
    get :contents, :contents => "some&junk*that(will@never)match$anything"
    assert_not_nil flash[:notice]
    assert_redirected_back
  end

Add A Comment