Looks like you permit is missing :id and your destroy is missing params[:id] in the find clause.
Destroy action error: param not found: link
-
23-06-2023 - |
Question
I am building a mock up of reddit. When on my show view that displays the link & title I submit I have included an option(link) to destroy the submission.
When I click on the destroy I get:
ActionController::ParameterMissing in LinksController#destroy
param not found: link
on line:
params.require(:link).permit(:url, :title)
This is my full Links controller:
class LinksController < ApplicationController
def index
@link = Link.all
end
def show
@link = Link.find(params[:id])
end
def new
@link = Link.new
end
def create
@link = Link.new(link_params)
if @link.save
redirect_to @link
else
render action: 'new'
end
end
def destroy
@link = Link.find()
if @link.destroy
redirect_to index: 'action'
else
render show: 'action'
end
end
private
def link_params
params.require(:link).permit(:url, :title)
end
end
This is my show view:
<h1> This is the show view </h1>
<%= @link.title %>
<%= @link.url %>
<%= link_to 'Edit', edit_link_path(@link) %>\
<%= link_to 'Destroy', (@link), method: :delete, data: { confirm: 'Are you sure?' } %>
I'm really confused, have tried multiple things but can't get it to work. How can i fix this?
Solution
OTHER TIPS
Your problem is:
def destroy
@link = Link.find()
...
You need something like:
def destroy
@link = Link.find(params[:id])
What's happening is that your Destroy link is sending a DELETE request to some URL like /link/23 with 23 being params[:id]
. You need to find the appropriate Link object by using Link.find(params[id])
before you destroy it (similar to how you would in show
).
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow