2012-05-01 38 views
1

下面是我在使用我的代碼的地方,我想我很接近。Sinatra CRUD每個模型的路線問題

savetomongoid是一個def,將params數組推向mongoid。

我使用的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 

當我試着使用任何路徑來加載,我得到一個完全空白的屏幕,沒有源代碼,但是本地主機:#### /帶來我對我的哈姆:沉默,所以一些路線工作。

三個splats應該把它的價值模型,動作,id。我嘗試了一些東西/某些東西,但我仍然得到了無源頁面。

提示圖標和問號的模式假定通過自述上下工夫https://github.com/sinatra/sinatra

誰能給我個忙嗎?我幾乎肯定這應該工作。

此外,任何人都可以提出一種方法來檢查模型是否存在過濾掉模型中的任意東西生成crud?

回答

0

您的案例陳述看起來很奇怪。

您正在測試request_method的結果,該結果將根據Request的get?,post?或delete?的值返回字符串「GET」,「PUT」,「POST」或「DELETE」返回的方法truefalse

看起來像你一樣,總是會遇到沒有匹配的情況。

可能應該是:

路線:得到,:刪除:帖子,: '???/ //*' 說, do | model,action,id |
情況下request.request_method
當 「GET」
...

+0

你說得對,我拿出request.request_method,現在評估情況的方法。但是,在路線上有一個問題。它捕獲第一個圖示的值,但不是第二個或第三個。所以,無論我有什麼事,行動=「」......任何想法? – Theta