The relevant parts of my controller in a nutshell (on Rails 3.0,
Ruby 1.9.2):
class SessionsController < ApplicationController
caches_action :index, cache_path: ->(c) {c.params}
def index
# Do stuff to load the @sessions variable based on passed parameters
# ...
unless @sessions
redirect_to sessions_path, alert: "Sorry, your search failed. Please try again." and return
end
end
end
This works great so long as the action succeeds. But in the
event of a redirect, I get this:
Started GET "/sessions?utf8=%E2%9C%93&type=all&terms=piedmont&scope=both" for 127.0.0.1 at 2010-09-08 15:07:16 -0400
Processing by SessionsController#index as HTML
Parameters: {"utf8"=>"✓", "type"=>"all", "terms"=>"piedmont", "scope"=>"both"}
Read fragment views/localhost:3000/sessions?utf8=✓&type=all&terms=piedmont&scope=both (0.7ms)
Redirected to http://localhost:3000/sessions
Completed 302 Found in 31ms
[2010-09-08 15:07:17] ERROR NoMethodError: You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each
/usr/local/Cellar/ruby/1.9.2-p0/lib/ruby/gems/1.9.1/gems/actionpack-3.0.0/lib/action_dispatch/http/response.rb:155:in `each'
/usr/local/Cellar/ruby/1.9.2-p0/lib/ruby/gems/1.9.1/gems/actionpack-3.0.0/lib/action_dispatch/http/response.rb:102:in `body'
/usr/local/Cellar/ruby/1.9.2-p0/lib/ruby/gems/1.9.1/gems/rack-1.2.1/lib/rack/response.rb:102:in `close'
/usr/local/Cellar/ruby/1.9.2-p0/lib/ruby/gems/1.9.1/gems/rack-1.2.1/lib/rack/handler/webrick.rb:68:in `ensure in service'
/usr/local/Cellar/ruby/1.9.2-p0/lib/ruby/gems/1.9.1/gems/rack-1.2.1/lib/rack/handler/webrick.rb:68:in `service'
/usr/local/Cellar/ruby/1.9.2-p0/lib/ruby/1.9.1/webrick/httpserver.rb:111:in `service'
/usr/local/Cellar/ruby/1.9.2-p0/lib/ruby/1.9.1/webrick/httpserver.rb:70:in `run'
/usr/local/Cellar/ruby/1.9.2-p0/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start_thread'
^C^C[2010-09-08 15:08:22] ERROR NoMethodError: undefined method `shutdown' for nil:NilClass
/usr/local/Cellar/ruby/1.9.2-p0/lib/ruby/gems/1.9.1/gems/activesupport-3.0.0/lib/active_support/whiny_nil.rb:48:in `method_missing'
I've confirmed, FWIW, that Webrick is not the problem.
I get similar errors in Thin and Unicorn. Operating theory for now
is that something in the 'after' filter on the caching is ignoring
the fact that a non-200 status was passed, and triggering the
standard processing for a response with a body. Will investigate
more later, and present a test if I can, but this seemed
significant enough that I wanted to report the issue right
away.