Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rendering 404 in sinatra if file not found

I have a basic sinatra app that renders files from a directory. What I'd like is returns 404 if page does not exist. Currently it raise 500 error.

get '/:page' do
  erb :"pages/#{params[:page]}", layout: :"layouts/application"
end
like image 262
olimart Avatar asked Nov 05 '13 02:11

olimart


1 Answers

Try this ;)

# 404 Error!
not_found do
  status 404
  erb :oops
end

Make yourself a 404 page with whatever name you like (mine is oops.erb, for example), and this should work just fine.

not_found is Sinatra's error-handling helper for grabbing error 500s and 404 not-founds that it returns. You can then change the HTTP status and corresponding view using it. Check out the documentation for all of Sinatra's error handler's: they're super useful!

like image 199
Jordan Thornquest Avatar answered Oct 01 '22 07:10

Jordan Thornquest