2009-02-13 168 views
1

這裏的routes.rb中:Rails的嵌套資源

map.resources :assignments, :shallow => true do |assignment| 
    assignment.resources :problems 
end 

我如何獲得url編輯問題(/任務/ XYZ /問題/ ABC /編輯),在代碼中?我已經嘗試了
edit_assignment_problem_path(作業,問題)
和edit_problem_path(問題)。
雖然第一個在我的本地設置上工作,但在服務器上它說方法edit_assignment_problem_path沒有被定義。有任何想法嗎?

回答

11

運行這在你的命令行:

rake routes 

它會告訴你,你已經定義的所有路線,以及它們如何映射。非常便利。

1

另外檢查routing guide,可以教你很多新的東西。

+3

該鏈接已損壞。路由指南現在可以在http://guides.rubyonrails.org/routing.html找到 – chiborg 2010-10-20 17:27:27

2

:在Rails 2.2中引入了shallow => true。您的本地設置可能運行的是早期版本,而您的服務器可能運行2.2或更早版本。對於:index,:create和:new操作(因爲這些操作需要完整的路徑),並且必須使用該路徑(例如/ assignments/a/problems/..)短路線(eg/problems/..)用於:edit,:show,:update和:destroy操作。

如果你想所有路線的完全和短版,唯一的可能是使用嵌套的資源路徑不淺加上短路線如:

map.resources :assignments, has_many => :problems 
map.resources :problems 

注意,在你的榜樣,你也沒必要使用map.resources的塊形式。