2

我无法让我的 form_for 为嵌套资源工作,而且它快疯了。

路线:

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

app/views/teacher/questions/_form.html.haml:

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

  ..

index、show、destroy 动作都可以正常工作。调用时只有编辑操作失败:

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]
4

1 回答 1

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
于 2013-12-23T19:58:19.597 回答