简体   繁体   中英

Why do I get a routing error when doing a delete action?

When I do a delete , I get a routing error:

Routing Error
No route matches [POST] "/items/10pp-logo"

Don't worry, current_user.items.find(params[:id]) does find the correct record for /items/10pp-logo .

This is the link in the view for my Delete action on my item object:

<td><%= link_to "<i class='fa fa-trash-o'></i>".html_safe, item, method: :destroy, data: { confirm: "Are you sure you want to delete #{item.name}?" } %></td>

This is the action in the controller:

  def destroy
    @item = current_user.items.find(params[:id])
    @item.destroy

    respond_to do |format|
      format.html { redirect_to items_url }
      format.json { head :no_content }
    end
  end

All the JS is being rendered in the footer - ie below where the delete action in the view is rendered (not sure if that makes a difference).

This is in my model - Item.rb :

  belongs_to :owner, :class_name => "User",
  :foreign_key => "user_id" 

This is the items route:

resources :items

I have the 7 RESTful actions in my controller and 2 nonRESTFul ones - for which I have 2 separate routes for.

Thoughts?

I believe the HTTP method should be :delete not :destroy. It defaults to :post, which is what you're getting.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM