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

时间:2012-07-07 18:47:44

标签: ruby-on-rails ruby-on-rails-3 view controller

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

请帮帮我!

我的观点是

<table>
  <tr>
    <th>delete</th>
    <th>ID</th>
    <th>Read</th>
    <th>Date</th>
    <th>Sender</th>
    <th>Subject</th>
  </tr>


<% @messages.each do |m| %>
  <tr>
    <td><%= check_box_tag '', m.id, false, class: 'delete_multiple_checkbox', name: "conversations[]" %>
    <td><%= m.last_message.id %></td>
    <td><%= 'unread' if m.is_unread?(current_user) %></td>
    <td><%= m.last_message.created_at %></td>
    <td><%= m.last_sender.username %></td>
    <td><%= m.subject %></td>
 </tr>
<% end %>
</table>

和控制器应该是这样的(根据这里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

2 个答案:

答案 0 :(得分:4)

修改下面列出的语法以符合您的要求:

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

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

答案 1 :(得分:1)

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

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

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

resources :messages do
    collection do
         delete :trash
    end
end