在我的路线中我有
delete 'boshu_hombu/delete_data', :as => :delete_data
在我的控制器中,删除方法
def delete_data
if params[:boshus]
BoshuHombu.where(id: params[:boshus]).destroy_all
end
redirect_to edit_all_path
end
在我的视图中,我使用复选框选择多个要删除的记录,我有一个按钮来调用删除操作
<%= link_to '削除', { action: :delete_data}, method: :delete,
data: { confirm: 'Are you sure?' }, :class => 'btn btn-danger' %>
<% @get_boshu_data.each do |user| %>
<tr>
<td><%= check_box_tag "boshus[]", user.id %></td>
<td><%= user.id %></td>
<td><%= user.kyujin_site %></td>
<tr/>
<% end %>
但如何在我的控制器delete_data
操作中获取所选值。怎么去这个?谢谢
答案 0 :(得分:2)
试试这个:
<%= link_to '削除', { action: :delete_data}, method: :delete,
data: { confirm: 'Are you sure?' }, :class => 'btn btn-danger', id:'delete' %>
<% @get_boshu_data.each do |user| %>
<tr>
<td><%= check_box_tag "boshus[]", user.id, false, class: 'check' %></td>
<td><%= user.id %></td>
<td><%= user.kyujin_site %></td>
<tr/>
<% end %>
<script>
$(document).ready(function(){
var href = $('#delete').attr('href')
$('#delete').on('click',function(){
var newHref = href + '?'
$('.check').each(function(){
if ($(this).prop('checked') == true) newHref+='boshus%5B%5D='+$(this).val()+'&'
});
$('#delete').attr('href',newHref)
});
});
</script>