Many people praise Sinatra's ability to create simple projects in a single file. This is great and all but what if I want to use Sinatra for slightly larger project?

I'm fairly new to Ruby as a whole but have already found a couple ways to create a Sinatra application with separate models files, partitioning different groups or URLs into individual files, etc... But is there any best practice for this?

closed as primarily opinion-based by user40980, user22815, durron597, Ixrec, GlenH7 Sep 18 '15 at 21:15

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.


The step to take is to partition the application into multiple parts at the file level. Instead of having all of the routes in one file, split them up into logically separated files with different functionality groups in different files, which are all then require'd back into the main Sinatra application/Rackup file.

Although this makes your file structure prettier, it does not implement any true compartmentalization or organization beyond superficial divisions. The solution to this that seems to have garnered the most popularity and acceptance is the Padrino framework, which runs on top of and expands upon the Sinatra core to add controllers (for compartmentalization) and a bunch of Django-like features (drop-in authentication, administration, etc.).


One thing that helps me keep the routes manageable is to make the lines more compact by using lambdas. As you can see this is just a single line. If you need more work done in the block then just put that in a helper file and require it.

get '/', &block = lambda { haml :index }

get '/my_markdowns', &block = lambda { markdown :"graph", to_erb }

"graph" is a markdown page and the to_erb method is in a helper to render it with erb layout. Haml has a :markdown tag but you still have to indent a lot for haml syntax.

Not the answer you're looking for? Browse other questions tagged or ask your own question.