Delete multiple items by checkbox

I want to be able to select multiple items using checkboxes and delete them in one place.

This is the code:

<% @products.each do |p| %>
<%= check_box_tag "product[]" , p.id %>
<div class="product_image">
    <%= image_tag p.photo.url(:thumb) , :alt => "#{p.name}" %>
</div>
<%= link_to "<h3>#{p.name}</h3>" , edit_product_path(p) %>
<div class="product_desc">
    <%=h truncate(p.description.gsub(/<.*?>/,''),80) %>
</div>
<div class="product_price">
    <%=h p.price %>
</div>
<div class="product_categories">
    <% for category in p.categories.find(:all) %>
        <%=h category.name %>
    <% end %>
</div>
<div id="produt_edit_nav">
    <%= link_to 'Show' , product_path(p) %>
    <%= link_to 'Edit', edit_product_path(p) %>
    <%= link_to 'Remove', product_path(p), :confirm => "Are you really want to delete #{p.name} ?", :method => 'delete' %>
</div>
<% end %>
<div id="products_nav">
    <%= link_to "Add a new Product" , new_product_path %>
</div>

Flags set the correct values, but:

  • How can I give them different id values ​​for html code, all of them have id="product[]"?

  • How can I delete marked items in one click?

  • Also, what is the meaning of this part product[]:?

+5
source share
1 answer

1: you can create your own identifiers by passing them as part of the options hash:

<%= check_box_tag "product_ids[]", product.id, false, :id => "product_#{product.id}" %>

For 2 and 3, I recommend watching this Railscast .

+7
source

All Articles