Why delete method gives me wrong path? with

左心房为你撑大大i 提交于 2019-12-25 08:27:34

问题


I have simple delete link:

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

Gives me error: No route matches [DELETE] "/messages.316"

How to fix this?

I have ruby 1.9.3p0 Rails 3.1.1

My routes.rb

  resources :messages do
    collection do
      get :outbox
    end
  end

when i change this to message_path i recieve wrong number of arguments (0 for 1) -> full_trace: https://gist.github.com/1967988
-> all files: 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"}

EDIT: Releated with How to destroy polymorphic model? Method destroy missing argument


回答1:


You need to say message_path (singular) since it is the path for a single message:

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



回答2:


You're using the wrong URL helper. Just pass the message variable as the second argument for link_to.

Example:

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



回答3:


You want message_path(message) not messages_path. messages_path returns the index.



来源:https://stackoverflow.com/questions/9549439/why-delete-method-gives-me-wrong-path-with

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!