Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I give Sinatra a catchall, default route?

Tags:

ruby

sinatra

For a small developer documentation app, I'd like to set up a Sinatra app to just serve HAML files. After routes for CSS files and images, I want a route that tries to load a HAML file for any path you request.

For example:

  • /index loads views/index.haml, if it exists
  • /this/page/might/exist loads views/this/page/might/exist.haml, if it exists

How would I specify this route?

like image 211
Nathan Long Avatar asked May 29 '12 20:05

Nathan Long


1 Answers

Looks like this will do it:

get '/*' do
  viewname = params[:splat].first   # eg "some/path/here"

  if File.exist?("views/#{viewname}.haml")
    haml :"#{viewname}"

  else
    "Nopers, I can't find it."
  end
end
like image 119
Nathan Long Avatar answered Oct 17 '22 22:10

Nathan Long