我有一个简单的模型:从业者有约会和时间卡(在某一天工作了多少时间。
Practioners#show提供从业者和相关的约会/时间卡,以便我可以显示或编辑它们(或添加新的)。
查看:
<% if @practitioner.timecard.count > 0 %>
<p><strong>Time Card List</strong></p>
<table>
<tr>
<th>Hours</th>
<th>Date</th>
<th></th>
<th></th>
<th></th>
</tr>
<% @practitioner.timecard.order("activity_date ASC").each do |t| %>
<tr>
<td><%= t.hours_worked %></td>
<td><%= t.activity_date %></td>
<td />
<td><%= link_to 'Edit', edit_timecard_path([t.id]) %></td>
<td><%= link_to 'Show', timecard_path([t.id]) %></td>
<% end %>
</table>
<% else %>
<p><strong>No Time Cards to display</strong></p>
<% end %>
<%= link_to "[Add Time Card]", new_practitioner_timecard_path(@practitioner) %>
<% if @practitioner.appointment.count > 0 %>
<p><strong>Appointment List</strong></p>
<table>
<tr>
<th>Name</th>
<th>Date</th>
<th>Duration</th>
<th>New?</th>
<th></th>
<th></th>
<th></th>
</tr>
<% @practitioner.appointment.order("appointment_date ASC").each do |r| %>
<tr>
<td><%= r.patient.name %></td>
<td><%= r.appointment_date %></td>
<td><%= r.duration %> </td>
<td><%= r.first_time %></td>
<td />
<td><%= link_to 'Edit', edit_appointment_path(r.id) %></td>
<td><%= link_to 'Show', appointment_path(r.id) %></td>
<% end %>
</table>
<% else %>
<p><strong>No Appointments to display</strong></p>
<% end %>
路线:
get "welcome/index"
get "admin/index"
resources :patients
resources :locations, :shallow => true do
resources :practitioners, :shallow => true do
resources :timecards, :shallow => true
resources :appointments, :shallow => true
end
end
当我运行rake路线时,我清楚地看到
edit_appointment GET /appointments/:id/edit(.:format) appointments#edit
appointment GET /appointments/:id(.:format) appointments#show
当我点击编辑或显示时间卡时,它可以正常工作。当我单击编辑或显示约会时,它会出错:
No route matches {:action=>"edit", :controller=>"appointments", :id=>nil}
但我可以在浏览器中看到鼠标悬停的约会显示链接是: 本地主机:3000 /约会/ 6
并且编辑它给出 本地主机:3000 /约会/ 6 /编辑
为什么rails无法解析此路由?
答案 0 :(得分:1)
尝试这样做,看看控制台是否有任何变化......
shallow do
resources :locations do
resources :practitioners do
resources :timecards
resources :appointments
end
end
end
答案 1 :(得分:1)
我怀疑问题不在于为初始请求路由到您的约会控制器,而是在呈现显示/编辑时,您的约会控制器/视图正在尝试创建链接而不传递有效约会。如果我正确,您的堆栈跟踪可能会指向link_to
,url_for
或appointment_path
来电,但会在已开始运行预约的情况下控制器强>
测试这一理论的另一种方法是清除AppointmentController.show/edit
中的任何逻辑并清除其观点。
答案 2 :(得分:0)