Prakash Raman
Prakash Raman

Reputation: 13933

return different value from a before do block in sinatra

is there a way to stop execution and return a different value in a before do block in sinatra ?

before do
   # code is here
   # I would like to 'return "Message"'
   # I would like "/home" to not get called.
end



// rest of the code

get '/home' do

end

Upvotes: 5

Views: 3724

Answers (2)

BurmajaM
BurmajaM

Reputation: 724

before do
  halt 401, {'Content-Type' => 'text/plain'}, 'Message!'
end

You can specify only status if you want, here's example with status, headers and body

Upvotes: 9

Shtirlic
Shtirlic

Reputation: 692

On http://www.sinatrarb.com/intro Filters section

Before filters are evaluated before each request within the context of the request and can modify the request and response. Instance variables set in filters are accessible by routes and templates:

  before do
    @note = 'Hi!'
    request.path_info = '/foo/bar/baz'
  end

  get '/foo/*' do
    @note #=> 'Hi!'
    params[:splat] #=> 'bar/baz'
  end

Upvotes: 2

Related Questions