Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

return different value from a before do block in sinatra

Tags:

ruby

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
like image 266
Prakash Raman Avatar asked Oct 06 '10 08:10

Prakash Raman


2 Answers

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

like image 135
BurmajaM Avatar answered Oct 03 '22 13:10

BurmajaM


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
like image 45
Shtirlic Avatar answered Oct 03 '22 14:10

Shtirlic