2012-07-21 60 views
0

我有兩個模式:團隊和項目Ruby on Rails的3條嵌套的路線:錯誤的行動路線

的routes.rb

resources :teams do 
    resource :projects 
end 

而且兩個問題!

1-根據http://guides.rubyonrails.org/routing.html,我期望得到team /:team_id/projects /:id路徑。然而,這種情況並非如此。

耙路線

  team_projects POST  /teams/:team_id/projects(.:format)  projects#create 
    new_team_projects GET  /teams/:team_id/projects/new(.:format) projects#new 
    edit_team_projects GET  /teams/:team_id/projects/edit(.:format) projects#edit 
         GET  /teams/:team_id/projects(.:format)  projects#show 
         PUT  /teams/:team_id/projects(.:format)  projects#update 
         DELETE /teams/:team_id/projects(.:format)  projects#destroy 

,所以我不得不名路線得到它的工作

match 'teams/:team_id/projects/:id' => 'projects#show', :via => [:get], :as => :show_project 

所以如何能利用軌輔助方法,而不是對其進行命名的?


2-在項目表演動作來看,調試器拋出這些參數對我來說:

action: show 
controller: projects 
team_id: '1' 

這是罰款。但是當我點擊「new_team_projects_path」 URL,它重定向我相同的觀點和調試器拋出這些參數:

controller: projects 
action: show 
team_id: '1' 
id: new 

它不重定向我到新的行動,但它採取了「新」作爲ID!爲什麼?

回答

4
  1. 您需要使用

    resources :teams do 
        resources :projects 
    end 
    

    注意多! resource產生沒有身份證的單一路線。

  2. 與第一次修復不再相關。

+0

嚴重??!我找了幾個小時的解決方案。非常感謝。 – PeaceDefener 2012-07-21 03:08:41

+0

嗯,這是在指南;)但我知道那種感覺。 – Femaref 2012-07-21 12:15:50

+0

這導致我/ magazine /:id/ads將路由到#show而不是#index的問題 – troutinator 2014-12-12 19:30:15