William Denniss
William Denniss

Reputation: 16336

"No such middleware to insert before: Rack::Lock (RuntimeError)" after upgrading to Rails 4

I'm getting the following error after upgrading to Rails 4:

.../ruby-1.9.3-p125/gems/actionpack-4.0.0.rc2/lib/action_dispatch/middleware/stack.rb:125:in 'assert_index': No such middleware to insert before: Rack::Lock (RuntimeError)

The offending line is my "remove slashes" rack-rewrite rule:

config.middleware.insert_before(Rack::Lock, Rack::Rewrite) do
  r301 %r{^/(.*)/$}, '/$1', :headers => {'Cache-Control' => 'public, max-age='+2.week.to_s}
end

Any ideas?

Upvotes: 21

Views: 9653

Answers (1)

William Denniss
William Denniss

Reputation: 16336

As the error suggests ("No such middleware to insert before"), the issue is with the middleware you are trying to insert before (and not the middleware you are trying to insert, which was my initial assumption).

In Rails4, threading is enabled by default which removes Rack::Lock.

To find a replacement, you can run rake middleware from your rails project directory, and look for something near the start of the stack. I'm going to pick Rack::Runtime as it is early in the stack, and seems pretty standard.

So the rewrite config is now:

config.middleware.insert_before(Rack::Runtime, Rack::Rewrite) do
  r301 %r{^/(.*)/$}, '/$1', :headers => {'Cache-Control' => 'public, max-age='+2.week.to_s}
end

Upvotes: 43

Related Questions