1

我有一个简单的模型:从业者有约会和考勤卡(一天工作了多少时间。

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}

但我可以在我的浏览器中看到约会显示链接的鼠标悬停是:localhost:3000/appointments/6

对于编辑它给出 localhost:3000/appointments/6/edit

为什么rails不能解决这条路线?

4

3 回答 3

1

尝试这样做,看看控制台中是否有任何变化......

shallow do
  resources :locations do
    resources :practitioners do
      resources :timecards 
      resources :appointments
    end
  end
end
于 2013-03-29T22:58:47.277 回答
1

I suspect the problem is not in routing to your appointment controller for the initial request, but rather that when rendering the show/edit, your appointment controller/view is trying to create a link without passing a valid appointment. If I'm right, your stack trace will probably point to a link_to, url_for, or appointment_path call, but in the context of having already started running your appointment controller.

Another way to test this theory would be to clear out any logic in your AppointmentController.show/edit and also clear out its views.

于 2013-03-29T23:01:50.087 回答
0

我认为这可能有助于Rails 3 link_to 路由(编辑)嵌套资源

于 2013-03-29T23:00:42.243 回答