0

我收到一个路由错误:没有路由匹配 [POST] "/students/1" 我无法弄清楚。这是详细信息。

查看代码:

<% @students.each do |student| %>
.
.
    <td><%= link_to 'Show', student %></td>
    <td><%= link_to 'Edit', edit_student_path(student) %></td>
    <td><%= link_to 'Select Subjects', select_path(student) %></td>  # error occurs here

在我的学生控制器中:

def select
  .
  .
end

routes.rb: HomeSchool::Application.routes.draw 做

  resources :notes

  resources :assignments

  resources :subjects do
    resources :assignments, :only => [:create, :index, :new]
  end

  resources :students

  resources :resources

  match "students/:id/select" => "students#select", :as => :select

  root :to => 'students#index'

end

rake 路由的输出为:

                       GET    /students/:id/select(.:format)                  students/:id#select
                 notes GET    /notes(.:format)                                notes#index
                       POST   /notes(.:format)                                notes#create
              new_note GET    /notes/new(.:format)                            notes#new
             edit_note GET    /notes/:id/edit(.:format)                       notes#edit
                  note GET    /notes/:id(.:format)                            notes#show
                       PUT    /notes/:id(.:format)                            notes#update
                       DELETE /notes/:id(.:format)                            notes#destroy
           assignments GET    /assignments(.:format)                          assignments#index
                       POST   /assignments(.:format)                          assignments#create
        new_assignment GET    /assignments/new(.:format)                      assignments#new
       edit_assignment GET    /assignments/:id/edit(.:format)                 assignments#edit
            assignment GET    /assignments/:id(.:format)                      assignments#show
                       PUT    /assignments/:id(.:format)                      assignments#update
                       DELETE /assignments/:id(.:format)                      assignments#destroy
   subject_assignments GET    /subjects/:subject_id/assignments(.:format)     assignments#index
                       POST   /subjects/:subject_id/assignments(.:format)     assignments#create
new_subject_assignment GET    /subjects/:subject_id/assignments/new(.:format) assignments#new
              subjects GET    /subjects(.:format)                             subjects#index
                       POST   /subjects(.:format)                             subjects#create
           new_subject GET    /subjects/new(.:format)                         subjects#new
          edit_subject GET    /subjects/:id/edit(.:format)                    subjects#edit
               subject GET    /subjects/:id(.:format)                         subjects#show
                       PUT    /subjects/:id(.:format)                         subjects#update
                       DELETE /subjects/:id(.:format)                         subjects#destroy
              students GET    /students(.:format)                             students#index
                       POST   /students(.:format)                             students#create
           new_student GET    /students/new(.:format)                         students#new
          edit_student GET    /students/:id/edit(.:format)                    students#edit
               student GET    /students/:id(.:format)                         students#show
                       PUT    /students/:id(.:format)                         students#update
                       DELETE /students/:id(.:format)                         students#destroy
             resources GET    /resources(.:format)                            resources#index
                       POST   /resources(.:format)                            resources#create
          new_resource GET    /resources/new(.:format)                        resources#new
         edit_resource GET    /resources/:id/edit(.:format)                   resources#edit
              resource GET    /resources/:id(.:format)                        resources#show
                       PUT    /resources/:id(.:format)                        resources#update
                       DELETE /resources/:id(.:format)                        resources#destroy
                select        /students/:id/select(.:format)                  students#select
                  root        /                                               students#index

有什么建议么?我很难准确掌握rails中的路由应该如何工作,我还没有找到任何关于它的论文,但我很确定没有为我的选择路线列出任何方法至少是我的问题。

谢谢,

4

2 回答 2

1

尝试删除文件中的并将您的match资源更改为:routes.rbstudents

resources :students do
  member do
    get 'select'
  end
end

还要更新您的视图以调用select_student_path(student).

于 2012-08-13T04:48:10.087 回答
0

发生这种情况是因为您的 routes.rb 文件。在“/students/new”中没有 post 方法的回调

纠正这一点。转到 routes.rb 并post 'students/new' to: 'students#new'在结束之前添加。

并检查rails routes。您会在那里找到帖子的路由路径。

于 2020-02-14T16:51:42.087 回答