4

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?

Mike
  • 171

2 Answers2

5

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.).

dirk
  • 858
0

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.