为什么删除方法给我错误的路径?同

时间:2012-03-03 20:06:53

标签: ruby-on-rails ruby-on-rails-3 haml

我有简单的删除链接:

= link_to "Delete", messages_path(message.id), :method => :delete

给我错误:No route matches [DELETE] "/messages.316"

如何解决这个问题?

我有ruby 1.9.3p0 Rails 3.1.1

我的routes.rb

  resources :messages do
    collection do
      get :outbox
    end
  end

当我将其更改为message_path我收到wrong number of arguments (0 for 1) - > full_trace:https://gist.github.com/1967988
- >所有文件:https://gist.github.com/1967994

   outbox_messages GET    /messages/outbox(.:format)          {:action=>"outbox", :controller=>"messages"}
           messages GET    /messages(.:format)                 {:action=>"index", :controller=>"messages"}
                    POST   /messages(.:format)                 {:action=>"create", :controller=>"messages"}
        new_message GET    /messages/new(.:format)             {:action=>"new", :controller=>"messages"}
       edit_message GET    /messages/:id/edit(.:format)        {:action=>"edit", :controller=>"messages"}
            message GET    /messages/:id(.:format)             {:action=>"show", :controller=>"messages"}
                    PUT    /messages/:id(.:format)             {:action=>"update", :controller=>"messages"}
                    DELETE /messages/:id(.:format)             {:action=>"destroy", :controller=>"messages"}

修改 发布时为How to destroy polymorphic model? Method destroy missing argument

3 个答案:

答案 0 :(得分:4)

您需要说message_path(单数),因为它是消息的路径:

link_to "Delete", message_path(message.id), :method => :delete

答案 1 :(得分:1)

您使用的是错误的网址帮助程序。只需将message变量作为link_to的第二个参数传递。

示例:

= link_to "Delete", message, :method => :delete

答案 2 :(得分:0)

您希望message_path(message)不是messages_path。 messages_path返回索引。