2013-12-23 60 views
2

我無法讓我的form_for工作的嵌套資源,它瘋狂。rails 4嵌套資源無法獲取form_for工作

路線:

namespace :teacher do 
    resources :lessons do 
     resources :questions 
     resources :invites 
     resources :responses 
    end 
end 

應用程序/視圖/教師/問題/ _form.html.haml:

= simple_form_for [:teacher, @question], :html => { :class => 'form-horizontal form-lineup' } do |f| 

    .. 

指數顯示,正確銷燬行動的所有工作。打電話時 只有編輯操作失敗:

teachers/1/questions/1/edit 

拋出:您提供的路由定義

No route matches {:action=>"show", :controller=>"teacher/questions", :teacher_id=>#<Teacher::Question id: 1, teacher_id: 5, user_id: nil, name: "asdf", email: "dsafsd", expire_at: "2013-12-23 19:36:00", created_at: "2013-12-23 19:36:25", updated_at: "2013-12-23 19:36:25">, :id=>nil, :format=>nil} missing required keys: [:id] 

回答

1

基礎,對問題編輯路徑應爲teacher/lessons/1/questions/1/edit,而不是teachers/1/questions/1/edit

你可以參考指南,瞭解如何在路由使用的命名空間:http://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing

你的形式或許應該是這樣的:

= simple_form_for [:teacher, @lesson, @question], :html => { :class => 'form-horizontal form-lineup' } do |f| 

或者試試:

= simple_form_for @question, :html => { :class => 'form-horizontal form-lineup' }, url: edit_teacher_lesson_question_path(@lesson, @question), method: :put do |f| 

如果您想要製作這條路徑teachers/1/questions/1/edit,您需要這樣定義路線:

resources :teachers do 
    resources :questions 
end 
+0

這些文檔還指出了我在此刻實施的多個/雜誌/:magazine_id/ads \t。問題似乎是我的_form中的form_for行,路由本身沒問題(它顯示編輯表單,當我從它移除這個form_for標記時): – Rubytastic

+0

好的,我看到你的觀點,感謝你的寫作。在這種情況下?namespace:teacher do resource:teacher? – Rubytastic

+0

如果你想產生這個路徑'teachers/1/questions/1/edit',你需要這樣定義路徑: 'resources:teachers do resources:問題 結束 – Gjaldon