我无法让我的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]
答案 0 :(得分: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