我正在处理嵌套路线,我觉得缺少了一些东西,我收到了这个错误:
No route matches {:action=>"edit", :controller=>"consultations", :id=>nil, :patient_id=>#<Patient id: 2 ...
路线:
resources :patients, shallow: true do
resources :consultations
end
耙路线| grep咨询
patient_consultations GET /patients/:patient_id/consultations(.:format) consultations#index
POST /patients/:patient_id/consultations(.:format) consultations#create
new_patient_consultation GET /patients/:patient_id/consultations/new(.:format) consultations#new
edit_consultation GET /consultations/:id/edit(.:format) consultations#edit
consultation GET /consultations/:id(.:format) consultations#show
PATCH /consultations/:id(.:format) consultations#update
PUT /consultations/:id(.:format) consultations#update
DELETE /consultations/:id(.:format) consultations#destroy
型号:
class Patient < ActiveRecord::Base
belongs_to :medic, :foreign_key => :medic_id
has_many :consultations, :dependent => :destroy
accepts_nested_attributes_for :consultations
end
class Consultation < ActiveRecord::Base
belongs_to :patient, :foreign_key => :patient_id
end
查看: consultation / index.html.erb
<% @patient.consultations.each do |consultation| %>
<%= link_to 'Edit', edit_consultation_path(patient_id: @patient, id: @consultation) %> <---ERROR
<%= link_to 'Show', consultation(patient_id: @patient, id: @consultation) %> <---ERROR
<%= link_to 'Destroy', consultation, method: :delete, data: { confirm: 'Are you sure?' } %> <---ERROR
<% end %>
我是否将错误的参数传递给 edit_consultation_path(patient_id:@patient,id:@consultation)?
有什么想法吗?提前谢谢!
修改
控制器: controller / consultation_controller.rb
def index
@patient.consultations = Consultation.all
end
答案 0 :(得分:1)
你确定你有类似@consultation
的东西吗?
您正在循环所有学生的咨询,并将每个实例作为consultation
。
但是,您将@consultation
作为id
传递,这与consultation
不同。 @consultation
基本上是零。
您应该传递的是consultation
,而不是@consultation
,如下所示:
<% @patient.consultations.each do |consultation| %>
<%= link_to 'Edit', edit_consultation_path(patient_id: @patient, id: consultation) %> <---ERROR
<%= link_to 'Show', consultation_path(patient_id: @patient, id: consultation) %> <---ERROR
<%= link_to 'Destroy', consultation_path(id: consultation.id), method: :delete, data: { confirm: 'Are you sure?' } %> <---ERROR
<% end %>
或者甚至直接咨询id
,如下:
<% @patient.consultations.each do |consultation| %>
<%= link_to 'Edit', edit_consultation_path(patient_id: @patient, id: consultation.id) %> <---ERROR
<%= link_to 'Show', consultation_path(patient_id: @patient, id: consultation.id) %> <---ERROR
<%= link_to 'Destroy', consultation_path(id: consultation.id), method: :delete, data: { confirm: 'Are you sure?' } %> <---ERROR
<% end %>