<% 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 %>
In controller
line 13: get check box values1def index
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
@items = Item.find(:all)
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @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
line 15 : delete all items
line 16 : store item's name which already deleted
line 19 : item separate with comma, eg: orchid, rose
line 21 : goto index page after items deleted
Nice the post keep updating the post to save the timeRuby on Rails Online Course Hyderabad
ReplyDeleteso bad example
ReplyDelete