0

我有以下代码部分:

<%= link_to 'Edit', edit_computer_path(@computer) %> |
<%= link_to 'Back', computers_path %>

<div>
  <h2>New config</h2>
  <%= render "kernel_configs/computerkernelconfig", :locals => { :computer => @computer } %>
</div>

<div>
  <h2>Submitted Configs</h2>
  <table>
    <tr>
      <th>Rating</th>
      <th>Title</th>
      <th>Contributor</th>
      <th>Features</th>
      <th>Config file</th>
      <th></th>
    </tr>

    <%= render @computer.kernel_configs %>

  </table>
</div>

抱怨没有路线:

ActionController::RoutingError (没有路由匹配 {:action=>"edit", :controller=>"kernel_configs", :id=>#}):
app/views/kernel_configs/_kernel_config.html.erb:13:in _app_views_kernel_configs__kernel_config_html_erb___3252059691242213705_26424640' app/views/computers/show.html.erb:34:in _app_views_computers_show_html_erb___4341746115390966607_23819760'
app/controllers/computers_controller.rb:18:in `show'

如果我在第二个之后移动第一个渲染,则页面渲染良好而没有错误。所以我很确定有一条路线,实际上可能是其中之一:

 edit_kernel_config GET    /kernel_configs/:id/edit(.:format)   kernel_configs#edit
 edit_computer_kernel_config GET    /computers/:computer_id/kernel_configs/:id/edit(.:format)    kernel_configs#edit

作为参考,部分在这里:

<%= form_for( [@computer,@computer.kernel_configs.build], :multipart => true ) do |form| %>

  <div class="field">
    <%= form.label :title %>
    <%= form.text_field :title %>
  </div>

  <div class="field">
    <%= form.label :contributed_by %>
    <%= form.text_field :contributed_by %>
  </div>

  <div class="field">
    <%= form.label :features %><br />
    <%= form.text_area :features %>
  </div>

  <div class="field">
    <%= form.label "Upload File" %><br />
    <%= form.file_field :kernelconfig %>
  </div>

  <div class="field">
    <%= form.submit %>
  </div>

<% end %>

_kernel_congif 部分:

<tr>

  <td><%= kernel_config.rating %></td>

  <td><%= kernel_config.title %></td>

  <td><%= kernel_config.contributed_by %></td>

  <td><%= kernel_config.features %></td>

  <td><%= truncate kernel_config.kernelconfig %></td>

  <td><%= link_to 'Edit', edit_kernel_config_path(kernel_config) %></td>

</tr>
4

1 回答 1

0

正在发生的事情是,由于您在列表之前有表单,因此您正在调用@computer.kernel_configs.build它将新的空记录添加到@computer.kernel_configs.

您的部分可能会崩溃,因为您正试图显示这个新的空记录。

这就是为什么当您将表单部分移动到集合部分下方时,它工作得很好。

编辑:

正如预期的那样,您正在尝试创建指向空对象的链接。

错误发生在以下行:

# _kernel_config.html.erb
<td><%= link_to 'Edit', edit_kernel_config_path(kernel_config) %></td>

edit_kernel_config_path找不到空的路径kernel_config

于 2013-04-22T03:30:39.913 回答