2012-07-19 73 views
0

我可以在沒有任何問題的情況下取消所有在索引視圖中檢查的消息。
但是在顯示視圖中,生成的鏈接顯示到/messages/discard.3 的鏈接,這3個可以是ID。如何正確設置路由

我該如何讓它工作?它應該被鏈接到消息/丟棄/ 3

我的文件是這樣的

的routes.rb

get "messages/received" 
    get "messages/sent" 
    get "messages/trash" 
    get 'messages/:id' => 'messages#show', :as => :show_messages 

    match 'messages/new/:username', :to => 'messages#new', :as => :new_messages 
    match 'messages/deliver' => 'messages#deliver', :via => :post 
    match 'messages/discard' => 'messages#discard', :via => :post, :as => :discard_messages 
    match 'messages/untrash' => 'messages#untrash', :via => :post 

視圖1(index.html.erb)

<%= form_tag(:action=> discard) do %> 
    <% @messages.each do |m| %> 
     <tr> 
     <td><%= check_box_tag "checked_items[#{m.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 %></td> 
     <td><%= m.last_sender.username %></td> 
     <td><%= link_to m.subject, show_messages_path(m) %></td> 
     </tr> 
    <% end %> 

視圖2(show.html.erb)

<%= link_to 'Trash', discard_messages_path(@messages) %> 

messages_controller.rb

def discard 
     conversation = Conversation.find_all_by_id(params[:checked_items].keys) 
    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

你可以嘗試將其更改爲:

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

不過,我想你應該對您的所有路由改變什麼:

resource :messages do 
    member do 
    #code 
    end 

    collection do 
    #code 
    end 
end 

這是更容易閱讀和維護

+0

謝謝!我改變了你的第一個建議。但它表示路由錯誤沒有路由匹配{:action =>「discard」,:controller =>「messages」} :( – MKK 2012-07-19 12:32:14