mardi 16 avril 2019

How to select rows in table using checkboxes and pass as parameters to controller

I have a table which displays a list of items. I'm trying to be able to select a few of the items in this table and pass to my controller where I hope to only render the specifically selected items.

# 'products/index.html.haml'
%table.table
  %thead
    %tr
      %th Select
      %th Id
      %th Short description

  %tbody
    - @products.each do |product|
      %tr
        %td
          %input{ :type=>"checkbox", :checked=>"checked", :name=>"selected_products[]", :value=>product.id}
        %td
        %td= product.id
        %td= product.short_description

= link_to 'View selected', product_path(:selected_ids=>SELECTED_PRODUCT_IDS)

As shown above it displays a table where the first column is a selected checkbox with its value being its corresponding product.id - I'm trying to pass an array of those id's selected into the parameters - i.e the array SELECTED_PRODUCT_IDS.

# 'controllers/product_controller.rb'
def index
   product_ids = params[:selected_form_datums]
   ...

Above shows my controller getting access to this array. I've seen a few answers to similar questions suggesting to put this into a 'form_for' tag however all my attempts have doing this have so-far failed.

Would appreciate any help.

Aucun commentaire:

Enregistrer un commentaire