I’m trying to destroy links that belong to a category model (through my browser). I can successfully delete a link that has an id of 1 and category_id of 1, but when I try to delete a link that has any other id I get hit with:
ActiveRecord::RecordNotFound in LinksController#destroy
Couldn't find Link with id=1 [WHERE "links"."category_id" = 1]
Very frustrating, because I’m not trying to delete a link that has an id of 1! But I see in the request parameters that it’s always trying to delete a link with "category_id"=>"1",, no matter what link I click on. Looking at the links in my development database confirms they exist and have ids that are not 1…
"id"=>"1"}
Here’s the code for LinksController#destroy:
class LinksController < ApplicationController
def destroy
@category = Category.find(params[:id])
@link = @category.links.find(params[:id])
@link.destroy
redirect_to category_url(@category)
end
Here’s the code for the view where I’m trying to delete these pesky links:
<h1><%= @category.category %></h1>
<p><%= @category.description %></p>
<ul>
<% @category.links.each do |link| %>
<li>
<%= link_to link.title, link.url %> |
<%= link_to "delete", category_link_path(@category), :method => :delete %>
</li>
<% end %>
Edit: Added parameters
{"_method"=>"delete",
"authenticity_token"=>"[removed]",
"category_id"=>"1",
"id"=>"1"}
Edit: Added config/routes.rb:
LinkManager::Application.routes.draw do
resources :categories do
resources :links, only: [:create, :destroy]
end
root :to => 'categories#index'
What am I overlooking? Is there anything I can try?
Your call to
category_link_pathis ommitting something. Perhaps it should look like this?It looks like you’re fulfilling the
category_idpart of the route by passing in@category, but you also need to providelinkas theid.Then you need to fix your
destroyaction as such: