1

I have a simple Model: Practitioners have Appointments and Timecards (how much time was worked on a given day.

Practioners#show presents the practitioner and the associated Appointments/Timecards so I can Show them or Edit them (or add new ones).

View:

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

Routes:

  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

when I run rake routes, I clearly see

edit_appointment GET    /appointments/:id/edit(.:format)    appointments#edit
     appointment GET    /appointments/:id(.:format)         appointments#show

When I click the Edit or Show for timecards, it works fine. When I click the edit or show for appointments, it errors:

No route matches {:action=>"edit", :controller=>"appointments", :id=>nil}

but I can see in my browser that the mouseover for an appointments Show link is: localhost:3000/appointments/6

and for the Edit it gives localhost:3000/appointments/6/edit

Why can't rails resolve this route?

3 Answers3

1

Try doing this and see if anything changes in the console...

shallow do
  resources :locations do
    resources :practitioners do
      resources :timecards 
      resources :appointments
    end
  end
end
econduck
  • 118
  • 1
  • 9
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.

Dan Wich
  • 4,923
  • 1
  • 26
  • 22
  • It doesn't dump any stack/trace info... I just get the unhelpful suggestion to "run rake routes" for more info. – Dennis Stevenson Mar 29 '13 at 23:07
  • Ok. One quick thing to try would be to delete everything out of /views/appointments/show.html.erb and then visit /appointments/1 (or whatever).This error can be misleading because it can mean that the controller was found and run, but encountered an error creating links for the view. – Dan Wich Mar 29 '13 at 23:12
  • I think you were ultimately right. In both my Show and Edit I had links that used: <%= link_to 'Show Appointment', appointment_path(@timecard) %> | because I copied the navigation options from timecard (similar logic and relationships, right?) @timecard was nil coming in, so I guess it was unable to render the appropriate html link - therefore it blew up. – Dennis Stevenson Mar 29 '13 at 23:26
0

I think this may help Rails 3 link_to routes (edit) nested resources

Community
  • 1
  • 1
thesubroot
  • 462
  • 6
  • 16