0

我可以毫无问题地清除索引视图中检查的所有消息。
但在显示视图中,生成的链接显示指向 /messages/discard.3 的链接,这 3 可能是 ID。

我怎样才能让它工作?它应该链接到messages/discard/3

我的文件是这样的

路线.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)  %>

消息控制器.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
4

1 回答 1

0

您可以尝试将其更改为:

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

但是我认为您应该做的是将所有路线更改为:

resource :messages do
  member do
    #code
  end

  collection do
    #code
  end
end

更易于阅读和维护

于 2012-07-19T12:14:36.800 回答