2013-12-23 62 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