marcamillion
marcamillion

Reputation: 33785

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 - i.e. 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?

Upvotes: 1

Views: 42

Answers (1)

kddeisz
kddeisz

Reputation: 5192

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

Upvotes: 4

Related Questions