2011-08-26 63 views
2

我添加了一個測試來確認我在我的意見中得到的錯誤。沒有路線匹配與耙路線的錯誤衝突

2) NodesController GET /nodes/:id should be successful 
    Failure/Error: get :show, :id => @node.id 
    ActionController::RoutingError: 
     No route matches {:id=>"nodexyz.example.com", :controller=>"nodes", :action=>"show"} 
    # ./spec/controllers/nodes_controller_spec.rb:21:in `block (3 levels) in <top (required)>' 

然而,耙路似乎要被罰款

nodes GET /nodes(.:format)   {:action=>"index", :controller=>"nodes"} 
    node GET /nodes/:id(.:format)  {:action=>"show", :controller=>"nodes"} 

爲什麼我的測試(和應用程序)認爲沒有路由匹配時,顯然這應該與後者的路線?

+0

什麼當你發生在'通過去除說期間id'簡單?看起來路由器可能認爲ID中的句點實際上是一個無效的格式。 –

+0

你的'routes.rb'的一部分 – Bohdan

回答

0

如果這是你得到的錯誤:

No route matches {:id=>"nodexyz.example.com", :controller=>"nodes", :action=>"show"} 

然後我會說,最有可能的問題是:標識要傳遞到節點控制器「nodexyz.example.com 」。除非另有指定,否則控制器期望您傳遞的缺省值是節點表中與包含字符串「nodexyz.example.com」的記錄對應的記錄標識。

如果你希望它使用字符串而不是數字ID,有幾種不同的方法來處理這個問題。我的建議是使用gem friendly_id,因爲它易於使用,並防止你不得不做一些工作。瑞恩貝茨有一個很好的Railscast如何使用它。

Railscast

Friendly ID at Github