I'm building a very basic rails application, that provides a form, and gets user submissions.
On the delete action, I keep getting the error: Couldn't find <object>
(in this case, question) without an id.
I understand the delete action is asking to find the params
by ID
, and I'm not sure why it doesn't have an ID
.
I added the field question_id
to the table questions, hoping that would do it. Have read many other similar questions, the answer isn't clicking. THX in advance.
Index page
<% @questions.each do |display| %>
<table>
<th>Your answer</th></br>
<tr><%= display.answer %></tr></br>
<tr><%= link_to("Delete Action Item", {:action => 'delete'}) %></tr></br>
<% end %>
</table>
Delete view
<%= form_for(:question, :url => {:action => 'destroy', :id => @question.id}) do |f| %>
<p>You're deleting this action item forever - Ok?</p>
<%= submit_tag("Do it - Delete it") %>
<% end %>
Questions Controller
class QuestionsController < ApplicationController
def index
@questions = Question.all
end
def delete
@question = Question.find(params[:id])
end
def destroy
@question = Question.find(params[:id]).destroy
flash[:notice] = "Deleted Action - Nice job"
redirect_to(:action => 'new')
end
private
def question_params
params.require(:question).permit(:answer, :question_id)
end
end