0

我在其中一个表单上使用 cocoon,我想在每一行添加一个显示按钮,它将用户引导到另一个模型的显示页面。然而,虽然一切工作顺利,但我无法让这个按钮显示出来。

所以这是我目前的观点:

<div class = "nested-fields">
<div class="table-responsive">
      <table class= "table table-hover">
      <tr>
       <th> Product ID </th>
       <th> Button </th>  
      </tr>
      <tr>
       <td> <%=f.object.product_id%> </td>
       <td> <%= link_to 'Show', product_path(f.object.product_id), class: "btn btn-outline-success", target: :_blank %> </td>  
      </tr>    
      </table>  
    </div>
</div>

当我尝试打开此页面时,我收到此错误:

没有路线匹配 {:action=>"show", :controller=>"products", :id=>nil},缺少必需的键:[:id]

但是,我知道 product_id 不是空的,因为在桌子上我可以为每一行打印 product_id。而且,我的产品路线绝对没问题,我已经可以使用它们,包括表演动作。而且我知道 product_id 在 products 表中有一个匹配的 id。

此外,如果尝试使用以下方式转到产品的索引页面:

<%= link_to 'Show', products_path(f.object.product_id), class: "btn btn-outline-success", target: :_blank %>

将生成以下网址:

http://localhost:3000/products.97

我只是不明白为什么当我使用它时它无法获取 id product_path。

任何帮助都将被视为。

谢谢。

4

1 回答 1

0

从对话/评论中,我只能得出结论,其中一项product_id实际上是零。确保您的表格/页面仍然呈现的简单方法是将您的视图编写如下

 <td> 
   <% if f.object.product_id.present? %>
     <%= link_to 'Show', product_path(id: f.object.product_id), class: "btn btn-outline-success", target: :_blank %> 
   <% end %>
 </td>  
于 2019-04-05T14:58:47.190 回答