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 trial

Ruby Build a Todo List Application with Rails 4 Build a Todo List Application with Rails 4 Adding Todo Items

Test won't pass! Help! (undefined method `todo_items' for nil:NilClass)

Hi I'm having issues with my controller it looks like. I'm using the most recent rails version.

But anyways here is the error that I am getting from my test. The instructor doesn't get this error at all while testing. I looked online for the last hour and can't seem to find a solution.

Here is the test failure/error

Failure/Error: click_link "New Todo Item"
     NoMethodError:
       undefined method `todo_items' for nil:NilClass
     # ./app/controllers/todo_items_controller.rb:8:in `new'
     # ./spec/features/todo_items/create_spec.rb:16:in `block (2 levels) in <top (required)>'

Here is the is the controller it refers to

class TodoItemsController < ApplicationController
  def index
    @todo_list = TodoList.find(params[:todo_list_id])
  end

  def new
    @todo_item = TodoList.find(params[:todo_list_id])
    @todo_item = @todo_list.todo_items.new  
  end 
end

If I comment out the @todo_item = @todo_list.todo_items.new line it will pass. But he's doesn't really say what this line does and I don't understand why I am getting a no method error for todo_items here.

Here is the test I'm running.

 require 'rails_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

    it "is successful with valid content" do
        visit_todo_list(todo_list)
        click_link "New Todo Item"
        fill_in "Content", with: "Milk"
        click_button "Save"
        expect(page).to have_content("Added Todo List Item.")
        within("ul.todo_items") do
            expect(page).to have_content("Milk")
        end
    end
end

Let me know if you need to see any other code!

Please help! Thank you!

Jackson

2 Answers

Felicia Wijaya
PLUS
Felicia Wijaya
Courses Plus Student 14,748 Points

In the new function, I think the first line should be @todo_list = TodoList.find(params[:todo_list_id]).

you're using @todo_item there. Minor typo?

And that is the solution! Thank you!