如何删除rails中的多个已检查记录?

我想出了如何显示每行的复选框。 问题是我无法找到如何编写form_tag和submit标签,以便使用detele操作将checked rows参数传递给messages_controller。 以及删除操作中要写的内容。

请帮帮我!

我的观点是

delete ID Read Date Sender Subject

和控制器应该是这样的(根据这里https://github.com/frodefi/rails-messaging/blob/master/app/controllers/messaging/messages_controller.rb )

 def trash conversation = Conversation.find_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 messages_path(box: params[:current_box]) end 

route.rb

 Example::Application.routes.draw do root :to => "top#index" devise_for :users, :controllers => { :registrations => "registrations" } get 'girls', :to => 'girls#index', :as => :user_root match '/girls/comment' => 'girls#comment', :via => :post get "girls/show" resources :girls resources :home devise_for :users do get 'logout' => 'devise/sessions#destroy' end resources :girls do collection do get 'tag' end end resources :contacts resources :user_profiles match 'messages/new/:username', :to => 'messages#new' get "messages/sent" get "messages/trash" get "messages/received" get "messages/show" get "messages/trash" match '/messages/deliver' => 'messages#deliver', :via => :post end 

修改下面列出的语法以满足您的要求:

 Model.where(:id => [1,2,3,4,5]).destroy_all 

要么

 Model.where(id: params[:id]).destroy_all 

您所要做的就是使用form_tag包装整个消息呈现块,并在您想要的任何地方添加submit_tag。 我假设您的控制器是空白命名空间下的MessagesController,操作是垃圾。 请注意,如果您的控制器位于消息传递命名空间下,您可能需要将:controller =>:messages更改为:controller =>’messaging / messages’。

  <% form_tag :url => { :controller => :messages, :action => :trash}, :method => :delete do %> <% @messages.each do |m| %>  <%= check_box_tag '', m.id, false, class: 'delete_multiple_checkbox', name: "conversations[]" %> <%= m.last_message.id %> <%= 'unread' if m.is_unread?(current_user) %> <%= m.last_message.created_at %> <%= m.last_sender.username %> <%= m.subject %>  <% end %> <%= submit_tag "Trash All Checked" %> <% end %> 

我还假设您的routes.rb接受指定路由的HTTP DELETE方法。 你可以用rake route | grep messages检查一下 rake route | grep messages并validation路由是否已设置。 如果不是,您将不得不添加:

 resources :messages do collection do delete :trash end end