2014-06-15 32 views
0

在Sinatra 1.1.0版中,我能够在before块内使用configure。在版本1.4.5中,这是不可能的。而是引发错误。使用configure之前的块

错误:

undefined method 'configure' for #<MySinatraServer:0x3f17a60> 
file: web.rb location: block in <class:MySinatraServer> line:6 

类定义:

require 'sinatra' 

class MySinatraServer < Sinatra::Base 

    before do 
    configure :production do 
     halt 404, "insecure connection not allowed" if !request.secure? 
    end 
    end 

    get '/' do 
    "Hello Cruel World" 
    end 

end 

thin start运行,如下图所示config.ru:

map "/" do 
    run MySinatraServer 
end 

为什么配置内不再工作在块之前?

+0

可能[this](http://stackoverflow.com/questions/10021869/how-to-define-a-method-to-be-called-from-the-configure-block-of-a-modular- sinatr)会有所帮助。 – zishe

+1

这很好,但我不能访问before块之外的'request' var。我想知道是否可以通过Sinatra助手访问它。会给它一个镜头,谢谢 – Rots

+0

@zishe不幸的是这有点混乱...在课堂上访问'请求'变种不是很漂亮。我设法以另一种方式解决了这个问题,这看起来相对简单。看看你的想法。 – Rots

回答

1

before块内访问configure块需要访问settings帮助程序。代码现在可用。

代码在这里:

before do 
    settings.configure :production do 
    halt 404, "insecure connection not allowed" if !request.secure? 
    end 
end 

另一个小问题,就是在after块现在运行,当你halt。啊升级框架版本的乐趣!

相关问题