Tag: 西纳特拉

阅读Sinatra的身体流

我正在尝试使用带有Sinatra的PUT方法上传带有XHR请求的文件。 我的第一个想法是上传文件并将流直接写入MongoDB GridFS @fs.open(“test.iso”, “w”) do |f| f.write request.body.read end 它可以工作,但是,它将整个文件加载到RAM中并将其写入MongoDB GridFS。 我想通过将其连续写入GridFS(流文件,不加载它并将其放入GridFS)而不将整个文件加载到RAM中来避免这种行为:因为对于大文件(如1GB或更多),它是显然是一种不好的做法(由于RAM的消耗)。 我怎样才能做到这一点 ? 编辑: 我可以让Sinatra / Rack不将整个请求体读入内存吗? 方法是创建一个TempFile,我想只使用流来优化服务器端的内存消耗。 就像php://input在PHP中一样。 编辑2: 以下是我目前正在处理它的方式: HTML / JS部分 Ruby / Sinatra部分

如何从Sinatra访问网页?

我想从Sinatra运行这个PHP脚本http://db2express/imacs/radek/3.1/rationalTest.php?mode=create 。 Sinatra在不同的盒子上运行。 有没有Sinatra内置的方法怎么做? 我知道我可以使用mechanize ,只是想知道是否还有其他用途?

处理.php路由时Sinatra错误

我正在创建一个Sinatra应用程序来取代传统的基于PHP的应用程序。 get ‘/page.php’ do # … do something end 我正在尝试定义这样的路线,但我得到“Sinatra不知道这个小曲。” 错误页面。 在页面顶部,我有这个 configure do mime_type :php, ‘text/html’ end 知道怎么告诉Sinatra使用包括文件扩展名在内的整个路径吗?

如何从url添加两个参数

如何在我的网页中显示int1 + int2的结果? 我可以知道它是整数还是字符串? 这是我的代码: require ‘sinatra’ get ‘/add/:int1/:int2’ do puts #{params[:int1]} + #{params[:int2]} end

如何在Sinatra中过滤之前创建类似Rails的?

class Foo def do_before … end def do_something … 有没有办法在Foo类中的每个其他方法之前运行do_before方法(比如do_something )? 看起来块之前的Sinatra before每个HTTP请求before运行,这与此类无关。 编辑:正如Michael在评论中指出的那样,Rails提供的唯一类似function是在Controller中。 但是,Rails和Sinatra都提供类似于此function的东西。

Sinatra服务器无法启动 – “错误的参数数量”

我想尝试Sinatra,因为我听说它对于新手web-dev比使用rails更好..而且一般来说我更喜欢简约而不是简约。 为了解释这一点,我使用ruby 2以及gem install获得的任何版本的sinatra。 到目前为止我所做的一切都是基本的 require ‘sinatra’ get ‘/’ do ‘Hello, World!’ end 尝试使用ruby basics.rb运行服务器,它会向我抛出: /home/ch35hir3/.rvm/gems/ruby-2.0.0-p247/gems/thin-2.0.0.pre/lib/thin/server.rb:108:in `initialize’: wrong number of arguments (4 for 0..3) (ArgumentError) from /home/ch35hir3/.rvm/gems/ruby-2.0.0-p247/gems/rack- 1.5.2/lib/rack/handler/thin.rb:14:in `new’ from /home/ch35hir3/.rvm/gems/ruby-2.0.0-p247/gems/rack -1.5.2/lib/rack/handler/thin.rb:14:in `run’ from /home/ch35hir3/.rvm/gems/ruby-2.0.0-p247/gems/sinatra-1.4.4/lib/sinatra/base.rb:1488:in `start_server’ from /home/ch35hir3/.rvm/gems/ruby-2.0.0-p247/gems/sinatra-1.4.4/lib/sinatra/base.rb:1426:in `run!’ from /home/ch35hir3/.rvm/gems/ruby-2.0.0-p247/gems/sinatra-1.4.4/lib/sinatra/main.rb:25:in `block in ‘ 当然我试过谷歌搜索错误,只是一般戳,但我真的不知道该怎么做。

如何定义从模块化sinatra应用程序的配置块调用的方法?

我有一个Sinatra应用程序,煮沸了,看起来基本上是这样的: class MyApp < Sinatra::Base configure :production do myConfigVar = read_config_file() end configure :development do myConfigVar = read_config_file() end def read_config_file() # interpret a config file end end 不幸的是,这不起作用。 我得到undefined method read_config_file for MyApp:Class (NoMethodError) read_config_file的逻辑非常重要,因此我不想在两者中都重复。 如何定义可以从我的配置块调用的方法? 或者我只是以完全错误的方式解决这个问题?

如果声明里面有Sinatra模板

我想仅在特定路线/页面上显示消息。 基本上,如果on / route显示消息。 我试过通过Sinatra Docs,但我找不到具体的方法来做到这一点。 是否有一个Ruby方法可以使这个工作? 编辑:这是我想做的一个例子。 get ‘/’ do erb :index end get ‘/page1’ do erb :page1 end get ‘/page2’ do erb :page2 end ******************* 不知道如何使用Ruby / Sinatra定位当前页面并将其结构化为if语句。

Sinatra的通用设置

我在Sinatra有一个class级,我设置了一些设置(来自JSON,因为它发生): class Pavo < Sinatra::Base configure :development do set :config, JSON.parse(File.open(File.dirname(__FILE__) + "/pavo.configuration.development.json", "rb").read) set :config_mtime, File.mtime(File.dirname(__FILE__) + "/pavo.configuration.development.json") end […] get '/' do puts "whatever" end end 该类有一个模型,需要读取这些设置。 class Resolver < Sinatra::Base def get_data(workpid) url_str = settings.config['public']['BOOKS_DATA_SERVICE_URL'].gsub('${WORKPID}', workpid) return Resolver.get_json(url_str) end […] end 但是,Resolver类不能这样做:解析器:Class的未定义方法`config’。 也许我的范围有误,或者我应该使用Sinatra :: Application?

`remove_const’中的Ruby Sinatra Hello World错误:常量URI :: WFKV_未定义(NameError)

只是试图让简单的http服务器运行,并且没有关于ruby的线索 /usr/local/lib/ruby/gems/1.9.1/gems/rack-1.3.5/lib/rack/backports/uri/common_192.rb:53:in `remove_const’: constant URI::WFKV_ not defined (NameError) from /usr/local/lib/ruby/gems/1.9.1/gems/rack-1.3.5/lib/rack/backports/uri/common_192.rb:53:in `’ from /usr/local/lib/ruby/gems/1.9.1/gems/rack-1.3.5/lib/rack/backports/uri/common_192.rb:19:in `’ from /usr/local/lib/ruby/gems/1.9.1/gems/rack-1.3.5/lib/rack/utils.rb:12:in `require’ from /usr/local/lib/ruby/gems/1.9.1/gems/rack-1.3.5/lib/rack/utils.rb:12:in `’ from /usr/local/lib/ruby/gems/1.9.1/gems/rack-1.3.5/lib/rack/request.rb:1:in `require’ from /usr/local/lib/ruby/gems/1.9.1/gems/rack-1.3.5/lib/rack/request.rb:1:in `’ from /usr/local/lib/ruby/gems/1.9.1/gems/rack-1.3.5/lib/rack/showexceptions.rb:3:in `require’ from /usr/local/lib/ruby/gems/1.9.1/gems/rack-1.3.5/lib/rack/showexceptions.rb:3:in `’ from /usr/local/lib/ruby/gems/1.9.1/gems/sinatra-1.3.1/lib/sinatra/showexceptions.rb:1:in `require’ from /usr/local/lib/ruby/gems/1.9.1/gems/sinatra-1.3.1/lib/sinatra/showexceptions.rb:1:in `’ from /usr/local/lib/ruby/gems/1.9.1/gems/sinatra-1.3.1/lib/sinatra/base.rb:12:in `require’ from /usr/local/lib/ruby/gems/1.9.1/gems/sinatra-1.3.1/lib/sinatra/base.rb:12:in `’ from /usr/local/lib/ruby/gems/1.9.1/gems/sinatra-1.3.1/lib/sinatra.rb:4:in `require’ from /usr/local/lib/ruby/gems/1.9.1/gems/sinatra-1.3.1/lib/sinatra.rb:4:in `’ from http.rb:1:in `require’ from http.rb:1:in […]