Question

I'm still new to Rails, and can't seem to wrap my head around this.

I have table showing of several Products with several attributes in form columns. In my index view I'm showing a shortened table, with just some of the columns. In my products#show I'm telling the full story, but obviously only for the selected Product. What I want to do, is letting users select a number of Products in products#index, and then store the selected IDs in a hash, sending them to another view, which I want the IDs to be presented in a full scale table, telling the whole story, of several products at once. Basically this is for comparing Products.

I'm not sure how to tackle this. As I'm already using DataTables, using javascript would make sense, but I'm unsure on how. And ideas? I've tried several ideas, but none of them got me anywhere.

products#index

    <table id="products"cellpadding="0" cellspacing="0" border="0" class="table.responsive">
<thead>
 <tr>
    <th></th>
   <th>Product Name</th>
   <th>TG</th>
   <th>TD</th>
   <th>DK</th>
   <th>DF</th>
   <th>T260</th>
   <th>T288</th>
   <th>CTI</th>
   <th>Strength</th>
   <th>Rigidity</th>
   <th>Standards</th>
   <th>Manufactorer</th>
 </tr>
</thead>

<tbody>
<% @products.each do |product| %>
 <tr>
    <td><%= check_box_tag "product_ids[]", product.id %></td>
   <td><%= link_to product.name, product %></td>    
   <td><%= product.TG %></td>
   <td><%= product.TD %></td>
   <td><%= product.DK %></td>
   <td><%= product.DF %></td>
   <td><%= product.T260 %></td>
   <td><%= product.T288 %></td>
   <td><%= product.CTI %></td>
   <td><%= product.ElectricStrength %></td>
   <td><%= product.rigidity %></td>
   <td><% product.standards.each do |standard| %>
       <%= link_to standard.name, standard %>, 
       <% end %> </td>
   <td><%= product.producer %></td>      
   <% end %>
 </tr>
</tbody>
</table>

If anyone could spare some time to point me in the right direction, that would be wonderful!

Was it helpful?

Solution

This shouldn't be too tough to tackle, I'll walk you through the basic steps that I would take.

My basic approach would be to take a form of said checkboxes and submit it to an action designed to handle them in the way described, like so:

  • Create a new route to use for all of this. Take a look at 2.10.2 Adding Collection Routes in the docs (assuming you are using resources). Maybe call it something like "compare".

  • Use a form not backed by a model to hold your checkboxes and labels (1 of each for each product) - it should post to your new route.

  • In your controller, add the action for your route. This action should fetch the ids passed in by the form from the params and load the relevant products (remember that find can take an array of ids, so it should be pretty easy).

  • Finally, add a view for your new action, with the relevant logic to present your comparison.

You can easily come in an backfill this with javascript, but it is not required. Honestly, it would be more useful for adding a little extra panache, like disabling the button and showing a spinner, or something, than a full-on javascript submission, which doesn't really buy you anything in this particular case.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top