Delete semua record dengan checkbox
Pada View :
<% @collection.each do |c| %>
<%= check_box_tag ‘myrow[id][]‘, c.id, false %>
<% end %>
Pada Controller :
def delete_all
@a = Model.find(params[:myrow][:id])
@a.each { |t| t.destroy }
end
Langkah Tarakhir pada Routes :
map.resources :controller, :collection => {:delete_all => :get}
Atau cara yang lain seperti di bawah ini :
Pada View :
<% form_for :items, @items, :url => { :action => 'destroy' } do %>
<% for item in @items %>
<%= check_box_tag 'item_ids[]', item.id, false %> <%= item.name %>
<% end %>
<%= submit_tag 'Delete', :name =>'commit', :value => 'Delete',
:confirm => 'Are you sure?' %>
<% end %>
Pada Controller :
def index
@items = Item.find(:all)
respond_to do |format|
format.html # index.html.erb
format.xml { render
ml => @items }
end
end
def destroy
i = 0
arr_item = Array.new
@items = Item.find(params[:item_ids])
@items.each do |item|
item.destroy
arr_item[i] = item.name
i += 1
end
@item_name = arr_item*', '
redirect_to(items_url)
flash[:notice] = @item_name + ' was successfully deleted.'
end
Semua cara diatas dapat di gunakan, silahkan pilih salah satu yang termudah bagi anda





