我正在尝试从Sinatra中的路径获取数据,并使用它来使用Datamapper查找特定记录。 Datamapper docs似乎表明了这一点。
get "/test/:test_path" do
test_get = Intake.get( params[:test_path] )
# Do stuff
erb :blah_blah_blah
end
应找到与该符号相关联的任何记录:test_path
这不起作用。 test_get得到nil。
同时,工作是什么
get "/test/:test_path" do
test_all = Intake.all(:test_path => params[:test_path] )
# Do stuff
erb :blah_blah
end
我的两个问题是:
这是一个Sinatra脚本,用于演示行为。
#!/usr/bin/env ruby
require 'rubygems'
require 'sinatra'
require 'dm-core'
require 'dm-timestamps'
DataMapper.setup(:default, {:adapter => 'yaml', :path => 'db'})
class Intake
include DataMapper::Resource
property :id, Serial
property :created_at, DateTime
property :test_path, String
end
get "/test/:test_path" do
test_all = Intake.all(:test_path => params[:test_path] )
puts 'test_all:' test_all.inspect
test_get = Intake.get( params[:test_path] )
puts 'test_get:' test_get.inspect
"Hello World!"
end
答案 0 :(得分:1)
#get
仅基于主键进行查找,其中id
为Intake.get(params[:test_path])
。所以
params[:test_path]
查找标识为Intake.first(test_path: params[:test_path])
的内容,该内容将失败。使用
{{1}}