Вопрос

Im unable to get my form_for to work for a nested resource and it's driving crazy.

routes:

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 actions all work correctly. Only Edit action fails when calling :

teachers/1/questions/1/edit

Throws:

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]
Это было полезно?

Решение

Basing on the route definition you provided, the edit path for questions should be teacher/lessons/1/questions/1/edit and not teachers/1/questions/1/edit.

You could refer to the guides for how to use namespaces in routes: http://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing

Your form should probably look like this:

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

Or try:

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

If you want to produce this path teachers/1/questions/1/edit, you need to define the route this way:

resources :teachers do
  resources :questions
end
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top