Rendering 404 in sinatra if file not found Rendering 404 in sinatra if file not found ruby ruby

Rendering 404 in sinatra if file not found


Try this ;)

# 404 Error!not_found do  status 404  erb :oopsend

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!


You could do something like:

get '/:page' do  requested_erb = File.join(root, 'pages', params[:page])  pass unless File.exists?(requested_erb)  erb :"#{requested_erb}", :layout: :"layouts/application"end

I haven't tested this, so there might be some issues with the above code, but that's the general idea in my head.