Shan Sunny
Shan Sunny

Reputation: 92

sinatra 'namespace' not working when trying modular way

Here is my code , but 'namespace' is not taking , If I write without namespace it is working , also it will work with 'namespace' if I remove class declaration and execute directly .

require 'sinatra'
require 'sinatra/namespace'

     class MyApp < Sinatra::Base
        namespace "/v1" do
          get "/" do
          "Hello World!"
          end  
        end
      end

1: from test1.rb:4:in <main>' test1.rb:5:in': undefined method `namespace' for MyApp:Class (NoMethodError)

Upvotes: 1

Views: 212

Answers (1)

Sara Fuerst
Sara Fuerst

Reputation: 6068

Based on the Sinatra documentation you need to register the extension:

require 'sinatra'
require 'sinatra/namespace'

class MyApp < Sinatra::Base
    register Sinatra::Namespace

    namespace "/v1" do
        get "/" do
            "Hello World!"
        end  
    end
end

Upvotes: 3

Related Questions