【问题标题】:How to delete multiple checked records in rails?如何删除rails中的多个检查记录?
【发布时间】:2012-07-07 18:47:43
【问题描述】:

我想出了如何为每一行显示复选框。 问题是我不知道如何编写 form_tag 和提交标签,以便使用 detele 操作将选中的行参数传递给 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

路由.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

【问题讨论】:

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


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

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

         <% 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
      

      【讨论】:

      • 谢谢,我刚刚复制粘贴了。但它现在不显示它曾经显示的任何记录....:(
      • 你的意思是它不渲染任何消息tr?
      • 确实它会渲染所有内容,但以某种方式记录行。 :(
      • 试试&lt;%= form_tag :url =&gt; { :controller =&gt; :messages, :action =&gt; :trash}, :method =&gt; :delete do %&gt;(注意开场后的=符号
      • 是的,现在可以渲染了:)谢谢!!但是如果我标记了一些复选框,然后点击删除提交按钮,它仍然会显示错误 'No route matches [POST] "/messages/received" ' 然后地址栏上的 url 看起来像这样 '/messages/received?method= delete&url%5Baction%5D=trash&url%5Bcontroller%5D=messages' 这太奇怪了。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多