Sinatra method `development?` Undefined

Sinatra docs say development? will return true when the environment is developing, but I get an error message indicating that the development? method development? equals undefined.

I tried to skip shorthand and test the ENV['RAKE_ENV'] variable, but it was just null.

This is the error I get:

 undefined method `development?' for main:Object (NoMethodError) 

and this is the code causing the error:

 require 'dm-sqlite-adapter' if development? 

I am using a modular application. The line above is a separate file that controls only the model. What's happening?

+5
source share
2 answers

I also struggled with this. Here is what I found along the way.

Do you need to be “inside” a class that inherits from Sinatra :: Base (for example, Sinatra :: Application, which inherits from Base) in order to use the development? method development? which is defined in base.rb.

In the classic Sinatra application, you already encode the “inside” class that inherits from Sinatra :: Base. development? will work "anywhere".

In the modular development? sinatra development? will only work in subclasses of Sinatra :: Base, for example:

 require 'sinatra/base' # Placing # require 'dm-sqlite-adapter' if development? # here will not work. class ApplicationController < Sinatra::Base require 'dm-sqlite-adapter' if development? # But here it works ... end # Placing # require 'dm-sqlite-adapter' if development?` # AFTER the above class will still not work class SomethingElse # nor will `development?` work here, since it is called inside # a class without Sinatra::Base inheritance ... end 

So you can use the ApplicationController class, which inherits from Sinatra :: Base and inside here, for development? . The same goes for subclasses that inherit from your ApplicationController class:

 class UserController < ApplicationController require 'dotenv' if development? ... end 

For the modular Sinatra in the text of the code (main: Object) to “outside” the Sinatra :: Base subclasses, you need to follow the Arup instructions :

 if Sinatra::Base.environment == :development require 'awesome_print' require 'dotenv' Dotenv.load ... end 
+3
source

Since you are using a modular style, you need to add the Sinatra::Base module namespace before this method.

So you can access Sinatra::Base.development? anywhere in the application.

+3
source

Source: https://habr.com/ru/post/1211283/


All Articles