1

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
Community
  • 1
  • 1
  • why do you need `delete` when you have `destroy` ? – mohameddiaa27 Nov 04 '14 at 03:11
  • Good question. Can you explain what you mean further? I'm super new to Rails, so as far as I know, I need both delete and destroy actions. I am displaying a confirmation page that says "are you sure you want to delete this," which is at: localhost:3000/questions/delete. That might be why. – Ryan Ballow Nov 04 '14 at 15:54

1 Answers1

0

In your index where you are creating the link, you aren't specifying the :id as an argument so your delete action has no params[:id]

Turn this:

<tr><%= link_to("Delete Action Item", {:action => 'delete'}) %></tr></br>

Into this:

<tr><%= link_to("Delete Action Item", {:action => 'delete', :id => display.id}) %></tr></br>
Philip Hallstrom
  • 18,803
  • 1
  • 38
  • 42
  • Phillip, - That answered it! Just.. have to put display.id, not displayed (typo on your part, I'm sure). But thanks. I knew it was something simple. – Ryan Ballow Nov 04 '14 at 15:53