Sinatra可以通过名称轻松访问任何特定的传入表单字段:
post "/" do
params['form_field_name']
end
Run Code Online (Sandbox Code Playgroud)
但是如何枚举请求中的所有表单字段?我在文档中找不到任何内容.我甚至试过了
request.body.split('&')
Run Code Online (Sandbox Code Playgroud)
但是request.body是StringIO的一个实例,而不是一个字符串.
我正在使用Sinatra在Ruby中编写一个小型Web服务.使用http basic auth(在生产中通过https)控制对几乎所有内容的访问.
我希望从需要授权中排除一个特定目录.是否有捷径可寻?
有没有人知道在Heroku的Bamboo堆栈上运行使用DataMapper的Sinatra应用程序所需的神奇咒语?Bamboo堆栈不包含任何预先安装的系统宝石,无论我尝试什么样的宝石组合,我都会遇到此错误:
undefined method `auto_upgrade!' for DataMapper:Module (NoMethodError)
Run Code Online (Sandbox Code Playgroud)
这就是我在我的.gems文件中所拥有的:
sinatra
pg
datamapper
do_postgres
dm-postgres-adapter
Run Code Online (Sandbox Code Playgroud)
这些是我将应用程序推送到Heroku时安装的依赖项:
-----> Heroku receiving push
-----> Sinatra app detected
-----> Installing gem sinatra from http://rubygems.org
Successfully installed sinatra-1.0
1 gem installed
-----> Installing gem pg from http://rubygems.org
Building native extensions. This could take a while...
Successfully installed pg-0.9.0
1 gem installed
-----> Installing gem datamapper from http://rubygems.org
Successfully installed extlib-0.9.15
Successfully installed addressable-2.2.1
Successfully installed dm-core-1.0.2
Successfully installed dm-aggregates-1.0.2
Successfully installed dm-migrations-1.0.2
Successfully installed dm-constraints-1.0.2 …Run Code Online (Sandbox Code Playgroud) 我想在每次击中网址/代码时运行脚本(code.rb).
我该如何运行脚本?
require 'sinatra'
get '/' do
#run the script
end
Run Code Online (Sandbox Code Playgroud) 假设我想使用curl以这种方式将文件放到web服务中
curl -v --location --upload-file file.txt http://localhost:4567/upload/filename
Run Code Online (Sandbox Code Playgroud)
在sinatra我能做到:
#!/usr/bin/env ruby
require 'rubygems'
require 'sinatra'
put '/upload/:id' do
#
# tbd
#
end
Run Code Online (Sandbox Code Playgroud)
我该如何阅读流媒体文件?
或多或少我想要这样的东西:http: //www.php.net/manual/en/features.file-upload.put-method.php#56985
我不确定为什么,但是当我设置此设置时,它无法编译
set :static_cache_control, [:public, :max_age => 300]
这就是我得到的
syntax error, unexpected tASSOC, expecting ']' (SyntaxError)
set :static_cache_control, [:public, :max_age => 300]
^
我只想为css,javaascript和图像文件设置"expires"标头.
谢谢.
我正在使用Sinatra和mongoid驱动程序,现在我正在尝试在mongoid中执行此查询,实际上我有一个名为' geometry ' 的地理空间(Polygon)字段:
db.states.find({
geometry: {
$geoIntersects: {
$geometry: {
type: "Point",
coordinates: [-99.176524, 18.929204]
}
}
}
})
Run Code Online (Sandbox Code Playgroud)
实际上这个查询在mongodb shell中有效.
但是,我想找到与给定点(多边形点)与mongoid或其他红宝石驱动器相交的状态.
任何帮助将不胜感激.
谢谢.
Grunt在Node.js环境中自动执行任务时获得了很多人气.
红宝石(Sinatra)有同等价值吗?
我有一组耗时的操作,这些操作特定于我的应用程序的每个用户,它都被封装在一个方法(例如write_collections方法)中.在这个方法中,程序与Facebook和MongoDB进行通信.我想在每个用户的线程中运行此方法.
在get '/'Sinatra路由中调用此线程,但仅需要线程(数据库中的状态)的结果get '/calculate'.我的想法是运行线程get '/'并加入它get '/calculate'以确保在计算用户启动结果之前,所有用户的数据都已在数据库中正确写入.
为了显示:
get "/" do
@user = @graph.get_object("me")
data_thread = Thread.new do
write_collections(@user)
end
session[:data_thread] = data_thread.object_id
erb :index
end
get "/calculate" do
begin
# Is this safe enough?
if ObjectSpace._id2ref(session[:data_thread]).alive?
data_thread = ObjectSpace._id2ref(session[:data_thread])
data_thread.join
end
rescue RangeError => range_err
# session[:data_thread] is not id value
# direct access to /calculate without session
rescue TypeError => type_err
# session[:data_thread] is nil
end
# do …Run Code Online (Sandbox Code Playgroud) 我想程序流中有一个逻辑错误,返回NoMethodError
首先,一段导致错误的代码.
<input id="#identity" type="number" name="journal[identity]" value="<%= @journal.identity unless @journal.identity.nil? %>" />
#Error Text
NoMethodError at /profile
undefined method `identity' for nil:NilClass
file: journal_form.erb location: block in singleton class line: 2
Run Code Online (Sandbox Code Playgroud)
输入标记内的代码是错误文本中描述的确切代码段.
我的程序流程就是这样.
/profile页面1还可以.用户可以毫无问题地登录和注销.对于第二步,代码就是这样
#profile.erb
<% if session[:user].role == 1 %>
<%= erb :assign_doctor %>
<% elsif session[:user].role == 2 %>
<%= erb :journal_form %>
<% elsif session[:user].role == 3 %>
<pre>
Silence!
</pre>
<% elsif session[:user].role == 4 %>
<%= erb :doctor_screen …Run Code Online (Sandbox Code Playgroud) sinatra ×10
ruby ×8
heroku ×2
caching ×1
datamapper ×1
geospatial ×1
gruntjs ×1
mongodb ×1
mongoid ×1
postgresql ×1
rack ×1