如何正确设置路线

时间:2012-07-19 11:59:31

标签: ruby-on-rails ruby-on-rails-3 view path routes

我可以在没有任何问题的情况下解开在索引视图中检查的所有消息 但在show视图中,生成的链接显示了指向/messages/discard.3的链接 这3个可能是ID。

我怎样才能让它发挥作用?它应该链接到messages / discard / 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

1 个答案:

答案 0 :(得分:0)

您可以尝试将其更改为:

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

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

resource :messages do
  member do
    #code
  end

  collection do
    #code
  end
end

更容易阅读和维护