通过复选框删除多个项目 [英] Delete multiple item by checkbox

查看:176
本文介绍了通过复选框删除多个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这是代码:


$

b $ b

 <%@ products.each do | p | %> 
<%= check_box_tagproduct [],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 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 => 你真的要删除#{p.name}?,:method => 'delete'%>
< / div>
<%end%>
< div id =products_nav>
<%= link_to添加新产品,new_product_path%>
< / div>

复选框给出了正确的值,但是:


  1. 如何给他们不同的id值html代码,他们都有 id =product []


  2. 如何一次删除勾选的项目?

    此部分的含义: product []



解决方案

1:您可以通过将它们作为选项散列的一部分传递来创建自己的ID:

 <%= check_box_tagproduct_ids [],product.id,false,:id => product _#{product.id}%> 

对于2和3,我建议您看看 this Railscast


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

这篇关于通过复选框删除多个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆