繁体   English   中英

从模型访问Sinatra设置

[英]Access Sinatra settings from a model

我有一个模块化的Sinatra应用程序。 我在配置块中设置了一些自定义变量,并希望在我的模型中访问这些设置。

问题是,我得到一个NoMethodError当我尝试从访问我的自定义设置MyModel 标准设置似乎仍然可以正常工作。 我怎样才能做到这一点?

# app.rb
require_relative 'models/document'

class App < Sinatra::Base
  configure do
    set :resource_path, '/xfiles/i_want_to_believe'
  end

  get '/' do
    @model = MyModel.new
    haml :index
  end
end

# models/my_model.rb
class MyModel
  def initialize
    do_it
  end
  def do_it
    ...
    settings.resource_path # no method error
    ...
    settings.root # works fine
  end
end

我认为你应该可以通过它访问它

Sinatra::Application.settings.documents_path

我最终做了:

#document.rb
class Document
  def self.documents_path=(path)
    @documents_path = path
  end
  def self.documents_path
    @documents_path
  end
  ...
end

#app.rb
configure do
  set :documents_path, settings.root + "/../documents/" 
  Document.documents_path = settings.documents_path
end

然后在我的find方法中使用Document.documents_path。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM