我希望能够在我的数据库中放置条目,其中制造商将被多次代表但不是制造商和型号的相同组合.所以"索尼(制造商),电视(型号)"还可以"索尼(制造商),OtherTv(型号)",但第三个条目"索尼(制造商),电视(型号)"自制造商和型号组合以来并不合适不是唯一的.我尝试了:key => true验证,但似乎没有用.而且我不能像validates_uniqueness_of :manufacturer AND :model我猜的那样做.你是怎么做到的?
class Tvs
include DataMapper::Resource
property :id, Serial
property :manufacturer, String, :key => true
property :model, String, :key => true
validates_uniqueness_of :
end
Run Code Online (Sandbox Code Playgroud) 我想定义一个错误块(或其他东西),它会以某种方式返回以JSON格式化的所有异常,并返回标准的http代码(例如404表示未找到,303表示auth错误等).
就像是:
error do
e = env['sinatra.error']
json :result => 'error', :message => e.message
end
Run Code Online (Sandbox Code Playgroud)
谢谢!
我正在阅读Heroku Routing文章,并对以下内容感到困惑:
一次一个连接
heroku.com堆栈仅支持单线程请求.即使您的应用程序要分叉并支持一次处理多个请求,路由网格也不会一次为一个dyno提供多个请求.
后来在文章中......
多个同时连接
herokuapp.com路由堆栈可用于希望同时处理多个连接的异步或多线程应用程序.Ruby Web服务器,如Goliath,Thin(具有合适的Web框架,如Async Sinatra),或您自己的自定义EventMachine Web进程就是一些例子.Node.js Web应用程序(例如使用Express构建的那些)几乎总能在单个进程中处理多个连接,大多数Python,Java,Scala和Clojure应用程序也是如此.
那是哪一个呢?或者两位是在谈论不同的事情?(即请求和连接不同)
谢谢
我正在尝试用Capybara测试一个简单的文件上传.这是我的Gemfile.lock
capybara (1.1.2)
mime-types (>= 1.16)
nokogiri (>= 1.3.3)
rack (>= 1.0.0)
rack-test (>= 0.5.4)
selenium-webdriver (~> 2.0)
xpath (~> 0.1.4)
Run Code Online (Sandbox Code Playgroud)
我的selenium-webdriver版本是2.18.这是我的web_steps文件(它是生成的):
When /^(?:|I )attach the file "([^\"]*)" to "([^\"]*)"(?: within "([^\"]*)")?$/ do |path, field, selector|
with_scope(selector) do
attach_file(field, path)
end
end
Run Code Online (Sandbox Code Playgroud)
这是我上传文件的功能:
Then I attach the file "features/resources/empty.file" to "file" within "#uploadForm"
Run Code Online (Sandbox Code Playgroud)
实际上它在线路上运行正常和绿色,但输入没有拾取任何文件,因此测试失败,因为没有选择文件.
这是我的表格:
%form#uploadForm{:action => "/upload", :method => "POST", :enctype => "multipart/form-data"}
%input{:type => "file", :name => "file", :id => "file"}
Run Code Online (Sandbox Code Playgroud)
这是非常基本的,但我不确定为什么它不起作用.
应用程序/控制器/ app.rb
require 'sinatra'
get '/' do
erb :index
end
Run Code Online (Sandbox Code Playgroud)
应用/视图/ index.erb
<html>
<body>
<p>Hello World</p>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
错误:
Errno::ENOENT at /
No such file or directory - .../app/controllers/views/index.erb
Run Code Online (Sandbox Code Playgroud)
如何配置erb app/views而不是app/controllers/views?
我正在使用Sinatra并使用该get '/foo/:bar' {}方法从url获取参数.不幸的是,由于没有路由匹配/ ,因此其中的值:bar可能包含/导致404的讨厌内容/foo/:bar/baz.我URI.escape用来转义URL参数,但它认为/有效的有效字符.正如这里提到的那样,这是因为要检查的默认Regexp不区分不安全字符和保留字符.我想改变这个并做到这一点:
URI.escape("foo_<_>_&_3_#_/_+_%_bar", Regexp.union(URI::REGEXP::UNSAFE, '/'))
Run Code Online (Sandbox Code Playgroud)
只是为了测试它.
URI::REGEXP::UNSAFE是根据Ruby 1.9.3 Documentaton匹配的默认正则表达式:
escape(*arg)
Synopsis
URI.escape(str [, unsafe])
Args
str
String to replaces in.
unsafe
Regexp that matches all symbols that must be replaced with
codes. By default uses REGEXP::UNSAFE. When this argument is
a String, it represents a character set.
Description
Escapes the string, replacing all unsafe characters with codes.
Run Code Online (Sandbox Code Playgroud)
不幸的是我收到了这个错误:
uninitialized …Run Code Online (Sandbox Code Playgroud) bundle update在我的padrino应用程序上运行给了我:
Bundler could not find compatible versions for gem "tilt":
In Gemfile:
padrino (= 0.11.1) ruby depends on
tilt (~> 1.3.0) ruby
padrino (= 0.11.1) ruby depends on
tilt (1.4.0)
Run Code Online (Sandbox Code Playgroud)
这刚刚开始发生:不确定发生了什么变化.
运行Ruby1.9.3-p392
Gemfile 好像:
source 'https://rubygems.org'
ruby '1.9.3'
gem 'unicorn'
gem 'rake'
gem 'bcrypt-ruby', :require => 'bcrypt'
gem 'slim'
gem 'mongoid', '~>3.0.0'
gem 'haml'
gem 'padrino', '0.11.1'
Run Code Online (Sandbox Code Playgroud) 我想尝试Sinatra,因为我听说它对于新手web-dev比使用rails更好..而且一般来说我更喜欢简约而不是简约.
为了解释这一点,我使用了ruby 2以及gem install获得的任何版本的sinatra.
到目前为止我所做的一切都是基本的
require 'sinatra'
get '/' do
'Hello, World!'
end
Run Code Online (Sandbox Code Playgroud)
尝试使用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 <module:Sinatra>'
Run Code Online (Sandbox Code Playgroud)
当然我尝试使用谷歌搜索错误,只是一般戳,但我真的不知道该怎么做.
从用户那里获得输入后,我有一个像这样的字符串:
str = "First line/nSecond line/nThird line"
Run Code Online (Sandbox Code Playgroud)
当我试图在我的erb文件中打印它时:
<%= str %>
Run Code Online (Sandbox Code Playgroud)
我只得到一条没有任何线路断线的线路("第一线第二线第三线")
我的问题是:
如何在erb文件中打印包含换行符的字符串?
(我想我可以用<br>标签替换每个\n 但是我想知道是否有更好的方法来实现它?!)
我的样式表适用于我的应用程序的某些元素而不是其他元素,我不知道为什么.
这是我的文档结构:

目前,样式表正在应用于index.erb和contacts.erb,而不是树中的其他样式表,我不知道为什么.
在我的布局中,我已指定: <link rel="stylesheet" type="text/css" href="style.css">
有任何想法吗?
sinatra ×10
ruby ×9
erb ×2
asynchronous ×1
capybara ×1
css ×1
cucumber ×1
datamapper ×1
escaping ×1
heroku ×1
padrino ×1
ruby-1.9.3 ×1
rubygems ×1
thin ×1
tilt ×1
uri ×1
validation ×1