2

I am trying to create a simple app in Rails, where a user can list to-do items and delete them once completed. I'm having trouble with being able to destroy items. Each time I try this, the browser gives the following error:

ActiveRecord::RecordNotFound in UsersController#destroy Couldn't find Item with 'id'=11

I've tried various edits to the controller and _item partial.

Here are a couple links of some previous stack overflow questions/answers that I've tried to implement in order to fix this:

ActiveRecord::RecordNotFound - Couldn't find User without an ID

Couldn't find <Object> without an ID (Deleting a record)

I am using devise, Rails 5.0.0.1, and Ruby 2.3.1 (if that helps).

Here's my code:

class UsersController < ApplicationController

  def show
    @user = User.find(params[:id])
  end

  def destroy
    Item.find(params[:id]).destroy
  end
end


class ItemsController < ApplicationController

  def create
    @item = current_user.items.new(items_param)

    if @item.save
      flash[:notice] = "Item was saved successfully."
      redirect_to current_user
    else
      flash.now[:alert] = "Error creating item. Please try again."
      render :new
    end
  end


  def destroy
    @item = Item.find(params[:id])
    @item.destroy
  end


  private

  def items_param
    params.require(:item).permit(:name)
  end
end

Here is the item partial _item.html.erb:

<%= content_tag :div, class: 'media', id: "item-#{item.id}" do %>
  <%= link_to "", @item, method: :delete, class: 'glyphicon    glyphicon-ok' %>

  <%= item.name %>
<% end %>

Routes.rb:

Rails.application.routes.draw do
  devise_for :users

  resources :users, only: [:show, :destroy] do
    resources :items, only: [:create, :show, :destroy]
  end

  root 'users#show'
end

Browser Error:

ActiveRecord::RecordNotFound in UsersController#destroy Couldn't find Item with 'id'=11

What am I doing wrong?

Community
  • 1
  • 1
  • A controller `destroy` method almost always calls `redirect_to` unless you want to render some page that says "X was deleted". This code also has a problem in that you let anyone delete any item, so watch out for that. You might mean `@user.items.find(...).destroy` instead. – tadman Nov 25 '16 at 19:54

2 Answers2

0

This record does not exist in your database, No user present with ID = 11

This is not any issue , its mistake and you can read this about rails guide and blogs

http://guides.rubyonrails.org/active_record_basics.html

http://api.rubyonrails.org/classes/ActiveRecord/RecordNotFound.html

Do

rails c
>> User.all # Write User.all it will give you all records from db

and in this way you can check the existing records in your users table

And correct you method destory in users_controller

  def destroy
    Item.find(params[:id]).destroy
  end

It should be

  def destroy
    User.find(params[:id]).destroy
  end
Vishal G
  • 1,521
  • 11
  • 30
  • I think this should be comment. – Ajay Barot Nov 25 '16 at 19:16
  • I just tried: `User.all` and `Item.all` and noticed that there isn't an item listed with id: 11. Perhaps its not being saved correctly in the database? However the items are being listed in the browser upon creation. I'll look through those links you gave me. Thanks. – Chris Dougherty Nov 25 '16 at 19:57
  • Yes that exactly right that your records are not saved in database....correct your method destrory in user_controlllers Updated the answer @ChrisDougherty – Vishal G Nov 25 '16 at 20:01
  • @ChrisDougherty for sure you have made some confustion beacause you have deleted the wrong records of wrong table see updated method above – Vishal G Nov 25 '16 at 20:04
0

Maybe this can help you:

Rails 2: Model.find(1) gives ActiveRecord error when id 1 does not exist

Basically: If the db raise an internal error tying to find the record, then Rails raise the error RecordNotFound. That does not means that the record doesn't exist, it means that Rails was not able to get it. maybe you need to look at your db

Community
  • 1
  • 1
Andrés
  • 624
  • 7
  • 12