5

I want have this ability to select multiple items with 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>

The checkboxes give me right values, but:

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

  2. How can I delete the checked items in one click?

  3. Also, what's the meaning of this part: product[]?

1 Answer 1

7

1: You can create your own Ids 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'd recommend looking at this Railscast.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your help, but why we use "product_ids[]" for the name, is this have a reason ?
The square brackets at the end of the field name cause all values to be placed in an array. So when you do params[:product_ids] it will be an array of all checked fields. If you didn't have the square brackets it would only return one checked value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.