【问题标题】:routes not agreeing with show action (link)路线不同意显示操作(链接)
【发布时间】:2013-03-20 16:55:15
【问题描述】:

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

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 不能解析这条路线?

【问题讨论】:

    标签: ruby-on-rails ruby


    【解决方案1】:

    我怀疑问题不在于初始请求的路由到您的约会控制器,而是在呈现显示/编辑时,您的约会控制器/视图试图创建一个链接而没有通过有效的约会。如果我是对的,您的堆栈跟踪可能会指向 link_tourl_forappointment_path 调用,但在已经开始运行您的约会控制器的情况下。。 p>

    检验这一理论的另一种方法是清除您的AppointmentController.show/edit 中的所有逻辑并清除其观点。

    【讨论】:

    • 它不会转储任何堆栈/跟踪信息...我只是得到“运行 rake 路由”以获取更多信息的无益建议。
    • 好的。一个快速的尝试是删除 /views/appointments/show.html.erb 中的所有内容,然后访问 /appointments/1 (或其他)。此错误可能会产生误导,因为它可能意味着控制器已找到并运行,但在为视图创建链接时遇到错误。
    • 我认为你最终是对的。在我的 Show 和 Edit 中,我都有使用的链接: |因为我从 timecard 复制了导航选项(类似的逻辑和关系,对吗?)@timecard 没有进来,所以我猜它无法呈现适当的 html 链接 - 因此它爆炸了。
    【解决方案2】:

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

    shallow do
      resources :locations do
        resources :practitioners do
          resources :timecards 
          resources :appointments
        end
      end
    end
    

    【讨论】:

    • 也试过了。但它给了我同样的结果。
    • 但这可能是更好的语法,所以我会喜欢它。
    【解决方案3】:

    我认为这可能会有所帮助Rails 3 link_to routes (edit) nested resources

    【讨论】:

    • 这给了我同样的结果。
    • 我的错误,我刚刚更新了我的答案,看看那个帖子,它可能会有所帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多