2012-07-21 128 views
0
  1. 從show view:我想通過顯示的消息的ID放棄行動和垃圾郵件。如何正確地將參數傳遞給控制器​​?

  2. 從索引視圖:我想通過檢查消息的ID放棄行動,並一次性垃圾。

但是我只能立即垃圾一條記錄,即使我檢查多個並從索引視圖提交。
我怎樣才能同時存檔1和2?

路線

match 'messages/discard(/:id)' => 'messages#discard', :via => :post , :as => :discard_messages 

索引視圖

<%= form_tag(:action => discard, :via => 'post') 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(:id => m, :breadcrumb => @box) %></td> 
     </tr> 
    <% end %> 
    <%= submit_tag "discard", :class => 'btn' %> 
    <% end %> 

放映視圖

<%= link_to 'Discard', discard_messages_path(@messages), :class => 'btn', :method => 'post' %> 

控制器

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 

回答

0

使用[]命名在你的HTML,其軌道,然後將提供作爲PARAMS數組

index.html.erb

<td><%= check_box_tag "message_id[]", m.id %></td> 

控制器

# ... 
else 
    conversations = Conversation.where("id IN (?)", params[:message_id][]) 
    # ... 

爲了進一步簡化事情,我會刪除您的動作中的條件並創建兩個單獨的動作

routes

resource :messages do 
    member do 
    post 'discard' # /messages/:id/discard 
    end 
    collection do 
    post 'discard_all' # /messages/discard_all?message_id[]=1&message_id[]=22 
    end 
end 
相關問題