2013-01-20 63 views
0

我有一個嵌套路由appointmentsschedules 當我嘗試去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 

回答

1

你應該通過雙方的資源(醫生和預約)將helpers路由到嵌套資源。另外,你忘記了_path的刪除鏈接。試試這個:

<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> 

UPDATE:

此外,對於新的任命鏈接:

<%= link_to 'New Appointment', new_doctor_appointment_path(appointment.doctor) %> 
+0

的new_doctor_appointment_path爲好。 –

+0

是的,'new_doctor_appointment_path(appointment.doctor)'爲新約會 – ivalkeen

+0

好!謝謝。 – evanx