Sinatra 方法`开发?`未定义
Sinatra method `development?` undefined
Sinatra 文档说在开发环境中 development?
将 return 为真,但我收到一条错误消息,指出方法 development?
未定义。
我尝试跳过 shorthand 并测试 ENV['RAKE_ENV']
变量本身,但它只是零。
这是我遇到的错误:
undefined method `development?' for main:Object (NoMethodError)
这是触发错误的代码:
require 'dm-sqlite-adapter' if development?
我正在使用模块化风格的应用程序。上面的行是一个单独的文件,只管理模型。怎么回事?
我也在这个问题上挣扎过。这是我一路上的发现。
您需要成为 "inside" 继承自 Sinatra::Base 的 class(例如继承自 Base 的 Sinatra::Application)才能使用 development?
方法,定义在base.rb.
在 classic Sinatra 应用程序中,您已经在编写 "inside" 继承自 Sinatra::Base 的 class。所以 development?
会起作用 "anywhere".
在模块化的 Sinatra 中,development?
只能在 Sinatra::Base 子 class 中工作,例如:
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
所以基本上您可以使用继承自 Sinatra::Base 的 ApplicationController class,并在此处检查 development?
。继承自 ApplicationController class:
的子 classes 也是如此
class UserController < ApplicationController
require 'dotenv' if development?
...
end
对于模块化Sinatra,在(main:Object)代码文本"outside" Sinatra::Base子classes中,需要遵循Arup's说明:
if Sinatra::Base.environment == :development
require 'awesome_print'
require 'dotenv'
Dotenv.load
...
end
由于您使用的是模块化风格,因此您需要在方法之前添加模块命名空间Sinatra::Base
。
因此您将能够在应用程序的任何位置访问Sinatra::Base.development?
。
Sinatra 文档说在开发环境中 development?
将 return 为真,但我收到一条错误消息,指出方法 development?
未定义。
我尝试跳过 shorthand 并测试 ENV['RAKE_ENV']
变量本身,但它只是零。
这是我遇到的错误:
undefined method `development?' for main:Object (NoMethodError)
这是触发错误的代码:
require 'dm-sqlite-adapter' if development?
我正在使用模块化风格的应用程序。上面的行是一个单独的文件,只管理模型。怎么回事?
我也在这个问题上挣扎过。这是我一路上的发现。
您需要成为 "inside" 继承自 Sinatra::Base 的 class(例如继承自 Base 的 Sinatra::Application)才能使用 development?
方法,定义在base.rb.
在 classic Sinatra 应用程序中,您已经在编写 "inside" 继承自 Sinatra::Base 的 class。所以 development?
会起作用 "anywhere".
在模块化的 Sinatra 中,development?
只能在 Sinatra::Base 子 class 中工作,例如:
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
所以基本上您可以使用继承自 Sinatra::Base 的 ApplicationController class,并在此处检查 development?
。继承自 ApplicationController class:
class UserController < ApplicationController
require 'dotenv' if development?
...
end
对于模块化Sinatra,在(main:Object)代码文本"outside" Sinatra::Base子classes中,需要遵循Arup's说明:
if Sinatra::Base.environment == :development
require 'awesome_print'
require 'dotenv'
Dotenv.load
...
end
由于您使用的是模块化风格,因此您需要在方法之前添加模块命名空间Sinatra::Base
。
因此您将能够在应用程序的任何位置访问Sinatra::Base.development?
。