before block called in subclass invoked in parent class #1880
-
I have this parent class. I include # ApplicationController
class ApplicationController < Sinatra::Base
helpers ApiHelpers
before do
content_type :json
end
get '/hello' do
{ message: 'Hello!' }.to_json
end
end A subclass of class ArticlesController < ApplicationController
before do
authenticate!
end
get '/articles' do
articles = Article.all
articles.map { |article| article.to_json }
end
end And module ApiHelpers
def authenticate!
halt 403, { message: 'Unauthorized!' }.to_json unless authorized?
end
private
def authorized?
request.env['HTTP_AUTHORIZATION'] == 'Bearer 123qweasd'
end
end
When I call Actual behaviour
Expected behaviour
Is this a bug or am I doing something wrong? |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 1 reply
-
Can you also share your config.ru? |
Beta Was this translation helpful? Give feedback.
-
I posted my |
Beta Was this translation helpful? Give feedback.
-
You don't want to use Here's how you separate them (remove map '/' do
run ApplicationController
end
map '/articles' do
run ArticlesController
end |
Beta Was this translation helpful? Give feedback.
-
No worries :) Happy to help! |
Beta Was this translation helpful? Give feedback.
You don't want to use
ArticlesController
as a middleware, that's why your before filter runs for every request.Here's how you separate them (remove
articles
from the routes inArticlesController
)