student_classes_controller.rb
class StudentClassesController < ApplicationController
before_filter :set_batch
def index
@sections = StudentClass.all
end
def show
@section = StudentClass.find(params[:id])
end
private
def set_batch
@batch = Batch.find(params[:batch_id])
end
end
在student_classes / index.html.erb
视图中 <table class="table table-bordered table-hover">
<thead bgcolor="#B7B7B7">
<th>Section</th>
<th>Number of Students</th>
<th>Details</th>
</thead>
<% @sections.each do |section| %>
<tr>
<td><%= section.name %></td>
<td><%= section.students.count %></td>
<td><%= link_to 'View Details', batch_student_classes_path(@batch , section) %></td>
</tr>
<% end %>
</table>
查看详细信息的网址是http://localhost:3000/batches/1/student_classes.1
但我想把它作为http://localhost:3000/batches/1/student_classes/1
的routes.rb
resources :batches do
resources :student_classes
end
rake routes
batch_student_classes GET /batches/:batch_id/student_classes(.:format) student_classes#index
POST /batches/:batch_id/student_classes(.:format) student_classes#create
new_batch_student_class GET /batches/:batch_id/student_classes/new(.:format) student_classes#new
edit_batch_student_class GET /batches/:batch_id/student_classes/:id/edit(.:format) student_classes#edit
batch_student_class GET /batches/:batch_id/student_classes/:id(.:format) student_classes#show
PUT /batches/:batch_id/student_classes/:id(.:format) student_classes#update
DELETE /batches/:batch_id/student_classes/:id(.:format) student_classes#destroy
答案 0 :(得分:1)
您要调用的路径方法是batch_student_class_path
而不是 batch_student_class * es * _ path 。