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 Validations to Todo Items

Bader Aljishi
Bader Aljishi
8,749 Points

undefined local variable or method `todo_item'?

I'm having difficulty finding where my code creates this error exactly. According to my output the trace goes back to todo_items_controller.rb and the create_spec.rb files

Failure/Error: click_button "Save" NameError: undefined local variable or method todo_item' for #<TodoItemsController:0xbac7db38> # ./app/controllers/todo_items_controller.rb:13:increate' # ./spec/features/todo_items/create_spec.rb:17:in `block (2 levels) in <top (required)>'

But I've watched the video multiple times and can't find a discrepancy in my code in my file.

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

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

  def create
    @todo_list = TodoList.find(params[:todo_list_id])
    @todo_item = @todo_list.todo_items.new(todo_item.params)
    if @todo_item.save
        flash[:success] = "Added todo list item."
        redirect_to todo_list_todo_items_path
    else
        flash[:error] = "There was a problem adding that todo list item."
        render action: :new
    end
  end

private def todo_item_params params[:todo_item].permit(:content) end end

2 Answers

Bader Aljishi
Bader Aljishi
8,749 Points

Ok I found it, it turns out I put a period where it shouldn't be :/

This: @todo_list.todo_items.new(todo_item.params)

Should be: @todo_list.todo_items.new(todo_item_params)

I changed todo_item.params to todo_item_params and it's now working.

NICE