Search code examples
rubysinatrapadrino

Where to put Padrino controller files in standalone Sinatra application?


I'm attempting to use Padrino's routing as a standalone addition to a basic Sinatra application.

http://www.padrinorb.com/guides/standalone-usage-in-sinatra

The main reason I need/want the additional Padrino functionality is the ability to separate my routes into multiple controller files.

Is this something I can do without using the whole Padrino package? If so, where do I place the separate controller files? I've tried /controllers/main.rb and /app/controllers/main.rb, where my core Sinatra app is at /app.rb.


Solution

  • You don't quite need to use the padrino routing to achieve the idea of controllers in Sinatra. In your main .rb file for your Sinatra app you can do:

    Dir.glob("controllers/*.rb").each { |r| require_relative r }
    

    Place your "controllers" into a controllers directory and the above will require_relative all of them for you. The code all gets lumped into the main ruby file eventually but you gain the ability to logically separate the code.