If I check multiple records in my view then hit the delete button,
discard action will be called.
Now I only can delete(untrash) 1 record at once.
Why can’t I delete all of them at once even if I check multiple records???
view
<%= form_tag(:action => discard, :via => 'put') do %>
<% @messages.each do |m| %>
<tr>
<td><%= check_box_tag "id",m.id %></td>
<td><%= m.last_message.id %></td>
<td><%= 'unread' if m.is_unread?(current_user) %></td>
<td><%= m.last_message.created_at.to_s(:jp) %></td>
<td><%= m.last_sender.username %></td>
<td><%= link_to m.subject, show_messages_path(m) %></td>
</tr>
<% end %>
<%= submit_tag "delete", :class => 'btn' %>
<% end %>
controller
def discard
conversation = Conversation.find_all_by_id(params[:id])
if conversation
current_user.trash(conversation)
flash[:notice] = "Message sent to trash."
else
conversations = Conversation.find(params[:conversations])
conversations.each { |c| current_user.trash(c) }
flash[:notice] = "Messages sent to trash."
end
redirect_to :back
end
routes
match 'messages/discard(/:id)' => 'messages#discard' , :as => :discard_messages
When there are multiple inputs of the same name the last one ‘wins’ –
params[:id]will be the value of the last submitted input, so only one message is deleted (this is easily visible by inspecting the value of the params hash)If the input name ends with
[](i.e. in your case set the name toid[]then rails will instead collect all the values into an array.