2012-07-07 56 views
1

我想出瞭如何顯示每一行的複選框。 的問題是,我不能找出如何寫的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 

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 

回答

3

修改如下,以滿足您的要求的語法:

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

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

所有你需要做的用form_tag包裝整個消息渲染塊,並在任何你想要的地方添加一個submit_tag柯。我認爲你的控制器是MessagesController,位於空白的命名空間下,並且操作是垃圾。請注意,如果您的控制器處於消息傳遞名稱空間中,則可能需要將:controller =>:消息更改爲: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 
+0

謝謝,我只是複製和粘貼它。但現在不顯示它用來顯示任何記錄.... :( – MKK 2012-07-08 02:50:07

+0

你的意思是它沒有提供任何消息TR? – 2012-07-08 07:28:27

+0

它的確呈現的一切,但創紀錄的行不知何故。:( – MKK 2012-07-08 10:15:21