2011-08-05 38 views
0

我試圖創建一個Rails應用程序,它按組和子組劃分用戶,併爲每個子組提供一個自定義的站點。我想做到這一點的最簡單的方法是用命名的參數創建範圍:Rails 3:命名參數路由中斷link_to

scope ":group/:subgroup/" do 
devise_for :users 
    resources :users 
end 

「回扣路線」,那麼產生這樣的:

 users GET /:group/:subgroup/users(.:format)    {:action=>"index", :controller=>"users"} 
      POST /:group/:subgroup/users(.:format)    {:action=>"create", :controller=>"users"} 
    new_user GET /:group/:subgroup/users/new(.:format)   {:action=>"new", :controller=>"users"} 
    edit_user GET /:group/:subgroup/users/:id/edit(.:format) {:action=>"edit", :controller=>"users"} 
     user GET /:group/:subgroup/users/:id(.:format)   {:action=>"show", :controller=>"users"} 
      PUT /:group/:subgroup/users/:id(.:format)   {:action=>"update", :controller=>"users"} 
      DELETE /:group/:subgroup/users/:id(.:format)   {:action=>"destroy", :controller=>"users"} 

它完美 - 除了當我使用的link_to:

link_to current_user.name, current_user 

然後拋出這個錯誤

ActionController::RoutingError in Users#index 
Showing C:/Rails/MyApp/app/views/users/_my_list.html.haml where line #8 raised: 
No route matches {:action=>"show", :controller=>"users", :group=>#<User id: 7, email: "[email protected]", encrypted_password: "$2a$10$GdZeC0b4VaNxdsDXP...", password_salt: "$2a$sj$88gm0nttYE.7a4IHi.BNO", reset_password_token: nil, remember_token: nil, remember_created_at: nil, sign_in_count: 86, current_sign_in_at: "2011-08-05 17:18:19", last_sign_in_at: "2011-08-05 00:14:26", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", confirmation_token: nil, confirmed_at: "2010-12-09 23:08:54", confirmation_sent_at: "2010-12-09 23:08:36", failed_attempts: 0, unlock_token: nil, locked_at: nil, created_at: "2010-12-09 23:08:36", updated_at: "2011-08-05 17:18:19", name: "UserOne">} 

第8行當然是我嘗試使用link_to的地方。我的猜測是,link_to忽略了範圍,它將current_user填充到:group參數中。有沒有辦法阻止這種情況發生,或者我是否完全錯誤地解決了這個問題?

回答

0

您的resource :users在範圍內,因此您沒有/users/:id路線。

你可以試試這個:

scope ":group/:subgroup/" do 
    devise_for :users 
end 
resources :users 

我不知道,如果scopeshallow => true選項(我認爲它和你應該使用它)。

希望幫助

0

問題是你不能沒有通過組和亞組中達到的用戶。讀取你的錯誤可能會理解rails正試圖將你的current_user轉換爲組。