1

这是我的代码所在,我认为我很接近。

savetomongoid 是一个将 params 数组推送到 mongoid 的 def。

我正在使用 activesupport 来支持复数和分类方法

route :get, :delete, :post, :put, '/*/*?/?*?' do |model, action, id|
  case
  when request.get?
    case action
    when "new" 
      haml '#{model}/new'
    when "show" 
      instance_variable_set('@#{model}', model.classify.find(id))
      haml '#{model}/show'
    when "edit" 
      instance_variable_set('@#{model}', model.classify.find(id))
      haml '#{model}/edit'
     else
      instance_variable_set('@#{model.pluralize}', model.classify.asc(made))
      haml '#{model}/index'
    end
  when request.post? || request.put?
    savetomongoid(model, params[model]) ? (redirect '/#{model}/') : (redirect '/#{model}/new')
  when request.delete?
    model.classify.find(id).delete ? (redirect '/#{model}/') : (puts "uhh ohh")
  end
end

get '/*' do
  haml :silence
end

当我尝试使用任何路径加载它时,我得到一个完全空白的屏幕,没有源,但是 localhost:####/ 将我带到我的 haml :silence ,所以一些路由正在工作。

这三个 splats 应该把它的价值放在模型、动作、id 中。我已经尝试过某事/某事/某事,但我仍然得到无源页面。

假设 splats 和问号的模式可以通过https://github.com/sinatra/sinatra上的自述文件工作

谁能帮我一把?我几乎可以肯定这应该有效。

另外,任何人都可以建议一种方法来检查模型是否存在以过滤掉模型中任意事物的生成残渣吗?

4

1 回答 1

0

您的案例陈述看起来很奇怪。

您正在测试 request_method 的结果,它将根据请求的get?、post? 或 delete?的值返回字符串“GET”、“PUT”、“POST”或“DELETE”。返回truefalse的方法。

似乎您总是会在没有匹配项的情况下退出 case 语句。

可能应该是:

路线 :get, :delete, :post, :put, '/ / ?/?*?' 做 |模型、动作、id|
case request.request_method
when "GET"
...

于 2012-05-01T05:24:52.680 回答