【问题标题】:Ruby Sinatra NoMethodErrorRuby Sinatra NoMethodError
【发布时间】:2013-09-08 11:07:46
【问题描述】:

我试过用 Ruby 1.9.3 和 2.0 运行它,但似乎没有任何效果。代码如下:

require 'sinatra'

before do
   set :display_string, 'Welcome from Ruby!'
end
get '/' do
   settings.display_string
end

错误是:

NoMethodError at /
undefined method `set' for (sinatra application code here)

这段代码:

set :display_string, 'Welcome from Ruby!'

似乎是导致问题的原因。我正在运行 Thin 1.5.1 ,最新版本的 Sinatra 1.4.3

编辑:如果 set 不在“before do/end”块内,这似乎效果很好。所以这与集合在 before do/end 块中有关。

【问题讨论】:

    标签: ruby sinatra


    【解决方案1】:

    我认为您应该使用配置块设置您的配置:

    before do
      configure do
        set :display_string, 'Welcome from Ruby!'
      end
    end
    

    see more sinatra docs about the configure

    【讨论】:

      猜你喜欢
      • 2015-10-15
      • 1970-01-01
      • 2011-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多