我有一个嵌套的路由,appointments
和schedules
当我试图去doctors/1/appointments/
或doctors/1/schedules/
时,我得到一个路由错误。对我来说,它看起来像有一个错误与索引页内的链接,但我检查耙路线,一切似乎都很好。编辑,显示和新建操作运行良好。
我做错了什么?
route.rb
Pgranges::Application.routes.draw do
root :to => "Doctors#index"
resources :doctors do
resources :appointments
resources :schedules
end
resources :appointment_steps
end
这是我的索引文件
<h1>Listing appointments</h1>
<table>
<tr>
<th>Doctor</th>
<th>Adate</th>
<th></th>
<th></th>
<th></th>
</tr>
<% @appointments.each do |appointment| %>
<tr>
<td><%= appointment.doctor_id %></td>
<td><%= appointment.adate %></td>
<td><%= link_to 'Show', doctor_appointment_path %></td>
<td><%= link_to 'Edit', edit_doctor_appointment_path(appointment) %></td>
<td><%= link_to 'Destroy', doctor_appointment, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New Appointment', new_doctor_appointment_path %>
和我的控制器是这样的:
class SchedulesController < ApplicationController
def index
@doctor = Doctor.find(params[:doctor_id])
@schedules = @doctor.schedules.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @schedules }
end
end
def show
@doctor = Doctor.find(params[:doctor_id])
@schedule = @doctor.schedules.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @schedule }
end
end
def new
@doctor = Doctor.find(params[:doctor_id])
@schedule = @doctor.schedules.new
@doctors = Doctor.all
respond_to do |format|
format.html # new.html.erb
format.json { render json: @schedule }
end
end
def edit
@doctor = Doctor.find(params[:doctor_id])
@schedule = @doctor.schedules.find(params[:id])
end
def create
@doctor = Doctor.find(params[:doctor_id])
@schedule = @doctor.schedules.new(params[:schedule])
if @schedule.save
redirect_to doctor_url(@schedule.doctor_id)
else
render action: 'new'
end
end
def update
@schedule = Schedule.find(params[:id])
respond_to do |format|
if @schedule.update_attributes(params[:schedule])
format.html { redirect_to @schedule, notice: 'Schedule was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @schedule.errors, status: :unprocessable_entity }
end
end
end
def destroy
@schedule = Schedule.find(params[:id])
@schedule.destroy
respond_to do |format|
format.html { redirect_to schedules_url }
format.json { head :no_content }
end
end
end
您应该将这两个资源(医生和预约)传递给嵌套资源的路由帮助器。此外,您忘记了删除链接的路径。试试这个:
<td><%= link_to 'Show', doctor_appointment_path(appointment.doctor, appointment) %></td>
<td><%= link_to 'Edit', edit_doctor_appointment_path(appointment.doctor, appointment) %></td>
<td><%= link_to 'Destroy', doctor_appointment_path(appointment.doctor, appointment), method: :delete, data: { confirm: 'Are you sure?' } %></td>
更新:
另外,对于新的约会链接:
<%= link_to 'New Appointment', new_doctor_appointment_path(appointment.doctor) %>