Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3959942
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T02:47:35+00:00 2026-05-20T02:47:35+00:00

It seems that in Sinatra all route handlers are being written into a single

  • 0

It seems that in Sinatra all route handlers are being written into a single file, if I understand right it acts as a one large/small controller. Is there any way to split it into separate independent files, so when let’s say somebody calls "/" – one action is executed, and if something like "/posts/2" is received then another action – similar logic that is applied in PHP?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T02:47:35+00:00Added an answer on May 20, 2026 at 2:47 am

    Here is a basic template for Sinatra apps that I use. (My larger apps have 200+ files broken out like this, not counting vendor’d gems, covering 75-100 explicit routes. Some of these routes are Regexp routes covering an additional 50+ route patterns.) When using Thin, you run an app like this using:
    thin -R config.ru start

    Edit: I’m now maintaining my own Monk skeleton based on the below called Riblits. To use it to copy my template as the basis for your own projects:

    # Before creating your project
    monk add riblits git://github.com/Phrogz/riblits.git
    
    # Inside your empty project directory
    monk init -s riblits
    

    File Layout:

    config.ru
    app.rb
    helpers/
      init.rb
      partials.rb
    models/
      init.rb
      user.rb
    routes/
      init.rb
      login.rb
      main.rb
    views/
      layout.haml
      login.haml
      main.haml
    

     
    config.ru

    root = ::File.dirname(__FILE__)
    require ::File.join( root, 'app' )
    run MyApp.new
    

     
    app.rb

    # encoding: utf-8
    require 'sinatra'
    require 'haml'
    
    class MyApp < Sinatra::Application
      enable :sessions
    
      configure :production do
        set :haml, { :ugly=>true }
        set :clean_trace, true
      end
    
      configure :development do
        # ...
      end
    
      helpers do
        include Rack::Utils
        alias_method :h, :escape_html
      end
    end
    
    require_relative 'models/init'
    require_relative 'helpers/init'
    require_relative 'routes/init'
    

     
    helpers/init.rb

    # encoding: utf-8
    require_relative 'partials'
    MyApp.helpers PartialPartials
    
    require_relative 'nicebytes'
    MyApp.helpers NiceBytes
    

     
    helpers/partials.rb

    # encoding: utf-8
    module PartialPartials
      def spoof_request(uri,env_modifications={})
        call(env.merge("PATH_INFO" => uri).merge(env_modifications)).last.join
      end
    
      def partial( page, variables={} )
        haml page, {layout:false}, variables
      end
    end
    

     
    helpers/nicebytes.rb

    # encoding: utf-8
    module NiceBytes
      K = 2.0**10
      M = 2.0**20
      G = 2.0**30
      T = 2.0**40
      def nice_bytes( bytes, max_digits=3 )
        value, suffix, precision = case bytes
          when 0...K
            [ bytes, 'B', 0 ]
          else
            value, suffix = case bytes
              when K...M then [ bytes / K, 'kiB' ]
              when M...G then [ bytes / M, 'MiB' ]
              when G...T then [ bytes / G, 'GiB' ]
              else            [ bytes / T, 'TiB' ]
            end
            used_digits = case value
              when   0...10   then 1
              when  10...100  then 2
              when 100...1000 then 3
              else 4
            end
            leftover_digits = max_digits - used_digits
            [ value, suffix, leftover_digits > 0 ? leftover_digits : 0 ]
        end
        "%.#{precision}f#{suffix}" % value
      end
      module_function :nice_bytes  # Allow NiceBytes.nice_bytes outside of Sinatra
    end
    

     
    models/init.rb

    # encoding: utf-8
    require 'sequel'
    DB = Sequel.postgres 'dbname', user:'bduser', password:'dbpass', host:'localhost'
    DB << "SET CLIENT_ENCODING TO 'UTF8';"
    
    require_relative 'users'
    

     
    models/user.rb

    # encoding: utf-8
    class User < Sequel::Model
      # ...
    end
    

     
    routes/init.rb

    # encoding: utf-8
    require_relative 'login'
    require_relative 'main'
    

     
    routes/login.rb

    # encoding: utf-8
    class MyApp < Sinatra::Application
      get "/login" do
        @title  = "Login"
        haml :login
      end
    
      post "/login" do
        # Define your own check_login
        if user = check_login
          session[ :user ] = user.pk
          redirect '/'
        else
          redirect '/login'
        end
      end
    
      get "/logout" do
        session[:user] = session[:pass] = nil
        redirect '/'
      end
    end
    

     
    routes/main.rb

    # encoding: utf-8
    class MyApp < Sinatra::Application
      get "/" do
        @title = "Welcome to MyApp"        
        haml :main
      end
    end
    

     
    views/layout.haml

    !!! XML
    !!! 1.1
    %html(xmlns="http://www.w3.org/1999/xhtml")
      %head
        %title= @title
        %link(rel="icon" type="image/png" href="/favicon.png")
        %meta(http-equiv="X-UA-Compatible" content="IE=8")
        %meta(http-equiv="Content-Script-Type" content="text/javascript" )
        %meta(http-equiv="Content-Style-Type" content="text/css" )
        %meta(http-equiv="Content-Type" content="text/html; charset=utf-8" )
        %meta(http-equiv="expires" content="0" )
        %meta(name="author" content="MeWho")
      %body{id:@action}
        %h1= @title
        #content= yield
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It seems that runClasses() doesn't terminate the code being tested even after the test
It seems that often I find that my code when moving either from one
It seems that NHibernate Linq doesn't understand grouped .Where method conditions. I got the
I have a Sinatra::Base object that I would like to include in all of
Help me understand this; I'm learning Sinatra (and Rails for that matter, er, and
I'm trying to run a few Sinatra apps under sub-uri's, but it seems that
How can I run Rails on Maglev? It seems that it can run Sinatra,
Seems that it takes some time for a datatemplate to be set into a
Seems that This will be an easy question for you but this problem is
Seems that even after unchecking the option in the PyDev/Debug preferenecs pane to launch

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.