2015-02-10 35 views
0

我定義爲這樣一個自定義的軌道路線:與其它變量Rails的自定義路徑

resources :scores, path: "seasons/:season_id/scores/:student_id"

這是有道理的,我的應用程序,並會保持標準化的URL。這適用於顯示頁面,我可以正確抓取season_id和:student_id。

但是,當我嘗試路由到任何其他頁面時,出現錯誤。我希望新的和編輯頁面成爲這些路線:

resources :scores, path: "seasons/:season_id/scores/:student_id/new" 

編輯

resources :scores, path: "seasons/:season_id/scores/:student_id/:id/edit" 

因此,他們仍然按照正常的慣例,那就是當我運行會發生什麼耙路線,但擊中任何路線拋出:

ActionController::UrlGenerationError 

No route matches {:action=>"show", :controller=>"scores", :format=>nil, :id=>nil, :season_id=>#<Score id: ... >, :student_id=>nil} missing required keys: [:id, :student_id] 

這兩個帳戶都很奇怪。季節ID正在與分數對象相關聯,並且表示有缺少的鍵。所有按鍵都出現在params哈希表,這是我如何建立我的鏈接:

= link_to "Edit", { controller: :scores, action: :edit, id: score.id, student_id: params[:student_id], season_id: params[:season_id]} , class: "btn btn-success btn-xs" 
+0

錯誤說您要發送行動''「秀」 '',但是你發佈的鏈接代碼是「'」edit「''。你可以仔細檢查,或發佈該鏈接產生的確切錯誤? – dgilperez 2015-02-10 02:56:09

+0

這是確切的錯誤。這就是爲什麼它對我來說毫無意義。 我把它作爲頭部:ActionController :: UrlGenerationError在分數#編輯 以此爲內容:沒有路由匹配{:action =>「show」,:controller =>「scores」,:format => nil ,.....} – 2015-02-10 02:57:09

+0

點擊鏈接或加載包含鏈接的頁面時是否發生這種情況? – dgilperez 2015-02-10 03:00:07

回答

1

這是最好不要使用resourcespath當你需要嵌套的資源。閱讀和理解更容易,不太容易出錯,這是Rails慣例。

你可以寫你的路線是這樣的:

resources :seasons do 
    resources :students do 
    resources :scores 
    end 
end 

然後使用名爲路徑的方法在你的鏈接是這樣的:

link_to "Edit", edit_score_student_season_path(score, params[:student_id], params[:season_id]), class: "btn btn-success btn-xs" 
+0

創建季節/:season_id/students /:student_id /分數,然後是正常路徑? – 2015-02-10 03:13:41

+0

「正常路徑」是什麼意思? – dgilperez 2015-02-10 03:15:23

+0

正常資源邏輯。所以我在我的上面評論中發佈了/ new,/ id,/ id/edit等評分資源的路徑。另外。如果我嵌入這樣的東西,我可以使用資源:它自己的學生?我不能放棄/學生作爲根頁面。 – 2015-02-10 03:18:35