рельсы: неопределенный метод и form_tags - PullRequest
0 голосов
/ 10 января 2011

У меня есть это в courses.html.erb в app / views / Students

<% if @student.courses.count < Course.count then%>
  <% form_tag(course_add_student_path(@student)) do%>
  <%= select_tag(:course, options_from_collection_for_select(@student.unenrolled_courses, :id, :name))%>
  <%= submit_tag 'Enroll'%>
  <%end%>
  <%else%>
  <p><%=h @student.name%> is enrolled in every course. </p>
 <%end%>

У меня это есть в моем student_controller.rb в разделе app / controllers:

  def course_add
    @student = Student.find(params[:id])
    @course = Course.find(params[:course])

    unless @student.enrolled_in?(@course)
      @student.coursess << @course
      flash[:notice] = 'course added'
    else
      flash[:error] = 'course already enrolled'
    end
    redirect_to :action => courses, :id => @student
  end

А у меня в route.rb:

  resources :students, :has_many => [:awards], :member => {:courses => :get, :course_add => :post, :course_remove => :post}

Однако я получаю эту ошибку:

undefined method `course_add_student_path' for #<#<Class:0x105321d78>:0x1053200e0>

Что мне здесь не хватает?

Вывод маршрутов рейка:

          students GET    /students(.:format)                             {:action=>"index", :controller=>"students"}
                   POST   /students(.:format)                             {:action=>"create", :controller=>"students"}
       new_student GET    /students/new(.:format)                         {:action=>"new", :controller=>"students"}
      edit_student GET    /students/:id/edit(.:format)                    {:action=>"edit", :controller=>"students"}
           student GET    /students/:id(.:format)                         {:action=>"show", :controller=>"students"}
                   PUT    /students/:id(.:format)                         {:action=>"update", :controller=>"students"}
                   DELETE /students/:id(.:format)                         {:action=>"destroy", :controller=>"students"}
           courses GET    /courses(.:format)                              {:action=>"index", :controller=>"courses"}
                   POST   /courses(.:format)                              {:action=>"create", :controller=>"courses"}
        new_course GET    /courses/new(.:format)                          {:action=>"new", :controller=>"courses"}
       edit_course GET    /courses/:id/edit(.:format)                     {:action=>"edit", :controller=>"courses"}
            course GET    /courses/:id(.:format)                          {:action=>"show", :controller=>"courses"}
                   PUT    /courses/:id(.:format)                          {:action=>"update", :controller=>"courses"}
                   DELETE /courses/:id(.:format)                          {:action=>"destroy", :controller=>"courses"}
    student_awards GET    /students/:student_id/awards(.:format)          {:action=>"index", :controller=>"awards"}
                   POST   /students/:student_id/awards(.:format)          {:action=>"create", :controller=>"awards"}
 new_student_award GET    /students/:student_id/awards/new(.:format)      {:action=>"new", :controller=>"awards"}
edit_student_award GET    /students/:student_id/awards/:id/edit(.:format) {:action=>"edit", :controller=>"awards"}
     student_award GET    /students/:student_id/awards/:id(.:format)      {:action=>"show", :controller=>"awards"}
                   PUT    /students/:student_id/awards/:id(.:format)      {:action=>"update", :controller=>"awards"}
                   DELETE /students/:student_id/awards/:id(.:format)      {:action=>"destroy", :controller=>"awards"}
   courses_student GET    /students/:id/courses(.:format)                 {:action=>"courses", :controller=>"students"}
                   GET    /students(.:format)                             {:action=>"index", :controller=>"students"}
                   POST   /students(.:format)                             {:action=>"create", :controller=>"students"}
                   GET    /students/new(.:format)                         {:action=>"new", :controller=>"students"}
                   GET    /students/:id/edit(.:format)                    {:action=>"edit", :controller=>"students"}
                   GET    /students/:id(.:format)                         {:action=>"show", :controller=>"students"}
                   PUT    /students/:id(.:format)                         {:action=>"update", :controller=>"students"}
                   DELETE /students/:id(.:format)                         {:action=>"destroy", :controller=>"students"}

1 Ответ

1 голос
/ 10 января 2011

Ваш синтаксис маршрутов неверен. Попробуйте вместо этого:

resources :students do
  resources :awards
  member do
    get "courses"
    post "course_add"
    post "course_remove"
  end
end
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...