我正在编写Rails应用程序,以处理大学课程的时间表。当前,我正在尝试建立索引页面以显示数据库中的所有部分,但出现此错误:
No route matches {:action=>"index", :controller=>"sections"}
但是,当我查看路线时,可以看到节和索引的控制器动作对。
bin/rails routes
Prefix Verb URI Pattern Controller#Action
home_index GET /home/index(.:format) home#index
course_sections GET /courses/:course_id/sections(.:format) sections#index
POST /courses/:course_id/sections(.:format) sections#create
new_course_section GET /courses/:course_id/sections/new(.:format) sections#new
edit_course_section GET /courses/:course_id/sections/:id/edit(.:format) sections#edit
course_section GET /courses/:course_id/sections/:id(.:format) sections#show
PATCH /courses/:course_id/sections/:id(.:format) sections#update
PUT /courses/:course_id/sections/:id(.:format) sections#update
DELETE /courses/:course_id/sections/:id(.:format) sections#destroy
courses GET /courses(.:format) courses#index
POST /courses(.:format) courses#create
new_course GET /courses/new(.:format) courses#new
edit_course GET /courses/:id/edit(.:format) courses#edit
course GET /courses/:id(.:format) courses#show
PATCH /courses/:id(.:format) courses#update
PUT /courses/:id(.:format) courses#update
DELETE /courses/:id(.:format) courses#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
PATCH /students/:id(.:format) students#update
PUT /students/:id(.:format) students#update
DELETE /students/:id(.:format) students#destroy
root GET / home#index
rails_service_blob GET /rails/active_storage/blobs/:signed_id/*filename(.:format) active_storage/blobs#show
rails_blob_representation GET /rails/active_storage/representations/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations#show
rails_disk_service GET /rails/active_storage/disk/:encoded_key/*filename(.:format) active_storage/disk#show
update_rails_disk_service PUT /rails/active_storage/disk/:encoded_token(.:format) active_storage/disk#update
rails_direct_uploads POST /rails/active_storage/direct_uploads(.:format) active_storage/direct_uploads#create
为什么路由不正确?
我的部分控制器如下所示:
class SectionsController < ApplicationController
def index
@courses = Course.all
end
def create
@course = Course.find(params[:course_id])
@section = @course.sections.create(section_params)
end
def update
@course = Course.find(params[:course_id])
@section = @course.sections.find(params[:id])
@section.update(section_params)
end
def destroy
@course = Course.find(params[:course_id])
@section = @course.sections.find(params[:id])
@section.destroy
end
private
def section_params
params.require(:section).permit(:section_letter, :section_professor, :section_meetings, :section_capacity)
end
end
我的sections文件夹中的index.html.erb视图如下所示:
<h2>Sections</h2>
<table>
<tr>
<th>Number</th>
<th>Section</th>
<th>Name</th>
<th>Credit Hours</th>
<th>Professor</th>
<th>Meetings</th>
<th>Capacity</th>
</tr>
<% @courses.each do |course| %>
<% course.sections.each do |section| %>
<tr>
<td><%= course.course_num %></td>
<td><%= section.section_letter %></td>
<td><%= course.course_name %></td>
<td><%= course.course_credits %></td>
<td><%= section.section_professor %></td>
<td><%= section.section_meetings %></td>
<td><%= section.section_capacity %></td>
</tr>
<% end %>
<% end %>
</table>
我的路线文件如下所示:
Rails.application.routes.draw do
get 'home/index'
resources :courses do
resources :sections
end
resources :students
root 'home#index'
end
任何帮助表示赞赏!
这是因为section控制器是Course资源下的嵌套资源。有一个,sections#index
但是需要一个:course_id
参数。
换句话说,没有/sections
路径可以直接映射到sections#index
如果是一次性端点,则可以声明此路由
get 'sections' => 'sections#index', as: :sections
然后,您需要更新中的#index
方法,然后SectionsController
像这样拉动所有节
@sections = Section.all
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句