Son Kurc
Son Kurc

Reputation: 5

Show button to another page using cocoon gem

I am using cocoon on one of forms and i want to add a show button to each row which will direct users to another model's show page. However, while everything is working smoothly i can not make this button to show up.

So this my current view:

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

When i try to open this page, i am getting this error:

No route matches {:action=>"show", :controller=>"products", :id=>nil}, missing required keys: [:id]

However, i know that product_id is not nill because, on the table i can print the product_id for each line. And, my product routes are definitely fine, i can already use them including with show action. And i know that product_id has a matching id in products table.

Also, if try to go to products' index page by using:

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

the following url is going to be generated:

http://localhost:3000/products.97

I just cannot understand why it cannot get the id when i use it product_path.

Any help will be regarded.

Thanks.

Upvotes: 0

Views: 76

Answers (1)

nathanvda
nathanvda

Reputation: 50057

From the conversation/comments I can only conclude one of the items product_id is actually really nil. Easy way to make sure your table/page still renders is write your view as follows

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

Upvotes: 0

Related Questions