Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialAnne Wynn
4,414 PointsIndex_spec.rb - ActiveRecord : Record not found when running rspec
I receive this message when running rspec with index_spec.rb
Failures:
1) Viewing todo items displays no items when a todo list is empty
Failure/Error: visit "/todo_lists"
ActiveRecord::RecordNotFound:
Couldn't find TodoList with 'id'={:params=>[:todo_list_id]}
# ./app/controllers/todo_lists_controller.rb:8:in `index'
# ./spec/features/todo_items/index_spec.rb:29:in `block (2 levels) in <top (required)>'
Finished in 0.16252 seconds
1 example, 1 failure
Failed examples:
rspec ./spec/features/todo_items/index_spec.rb:27 # Viewing todo items displays no items when a todo list is empty
Randomized with seed 42256
Here is the todo_lists_controller:
class TodoListsController < ApplicationController
before_action :set_todo_list, only: [:show, :edit, :update, :destroy]
# GET /todo_lists
# GET /todo_lists.json
def index
@todo_lists = TodoList.all
@todo_list = TodoList.find(params:[:todo_list_id])
end
# GET /todo_lists/1
# GET /todo_lists/1.json
def show
end
# GET /todo_lists/new
def new
@todo_list = TodoList.new
end
# GET /todo_lists/1/edit
def edit
end
# POST /todo_lists
# POST /todo_lists.json
def create
@todo_list = TodoList.new(todo_list_params)
respond_to do |format|
if @todo_list.save
format.html { redirect_to @todo_list, notice: 'Todo list was successfully created.' }
format.json { render :show, status: :created, location: @todo_list }
else
format.html { render :new }
format.json { render json: @todo_list.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /todo_lists/1
# PATCH/PUT /todo_lists/1.json
def update
respond_to do |format|
if @todo_list.update(todo_list_params)
format.html { redirect_to @todo_list, notice: 'Todo list was successfully updated.' }
format.json { render :show, status: :ok, location: @todo_list }
else
format.html { render :edit }
format.json { render json: @todo_list.errors, status: :unprocessable_entity }
end
end
end
# DELETE /todo_lists/1
# DELETE /todo_lists/1.json
def destroy
@todo_list.destroy
respond_to do |format|
format.html { redirect_to todo_lists_url, notice: 'Todo list was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_todo_list
@todo_list = TodoList.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def todo_list_params
params.require(:todo_list).permit(:title, :string, :description, :text)
end
end
Here is index_spec.rb:
require 'spec_helper'
describe "Viewing todo items" do
let!(:todo_list) { TodoList.create(title:"Grocery List", description:"Groceries") }
# def visit_todo_list(list)
# visit "/todo_lists"
# within "#todo_list_#{list.id}" do
# click_link "List Items"
# end
# end
# before do
# visit "/todo_lists"
# within "#todo_list_#{todo_list.id}" do
# click_link "List Items"
# do
# do
# it "displays the title of the todo list" do
# visit_todo_list(todo_list)
# within("h1") do
# expect(page).to have_content(todo_list.title)
# end
# end
it "displays no items when a todo list is empty" do
# visit_todo_list(todo_list)
visit "/todo_lists"
within "#todo_list_#{todo_list_id}" do
click_link "List Items"
end
# expect(page.all("ul.todo_items li").size).to eq(0)
expect(page).to have_content("TodoItems#index")
end
end
2 Answers
David Ker
14,439 PointsThe error message says that the error is occurring on line 8 of TodoListController. Looks like you're looking in the params for an incorrect key...instead of looking for :todo_list_id
, you should only be looking for :id
. So line 8 should be
@todo_list = TodoList.find(params:[:id])
Anne Wynn
4,414 PointsDavid - thanks for responding. I made the change, and I receive the same error message, referencing the same lines, expect it says:
Couldn't find TodoList with 'id' =
It's odd, because it doesn't display a value like it did before.
Do you have any other ideas? Thanks.