Reputation: 5249
I want to define a global before filter that will run before every request that will set up some instance variables for all the methods.
I have set up the filter and have also set up some route specific before filters. It appears that my route specific filters are being executed prior to my global one and therefore crashing because expected instance variables are not set yet.
Is there a way to specify the order in which before filters are processed?
Upvotes: 3
Views: 2286
Reputation: 12906
This works for me, on Sinatra 1.3.2.
before do
@filter = [] << 'everything'
end
before '/filter' do
@filter << 'specific'
end
get '/filter' do
@filter.inspect
end
This gives me ["everything", "specific"]
which is what I would expect. Is it possible you do not have the catch-all filter before all the rest?
In Sinatra, routes are evaluated in order from the top, not by how well they match. Therefore, if you have the specific filters before the catch-all filter, it will evaluate those first, as seen here:
before '/filter' do
@filter = [] << 'specific'
end
before do
@filter << 'everything'
end
get '/filter' do
@filter.inspect
end # => ["specific", "everything"]
Upvotes: 2