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!
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
Shawn Wilson
iOS Development Techdegree Student 7,049 Pointsundefined local variable or method error
Good morning / afternoon ruby world!
so am currently in the "Build a todo list application with rails 4 portion of the ruby framework course and have hit a snag. when I run:
Shawns-MacBook-Pro:todo TaurenLTD1$ bin/rspec --format=documentation spec/features/todo_items/edit_spec.rb
in the terminal i get the following error:
Editing todo items is successful with valid content (FAILED - 1)
Failures:
1) Editing todo items is successful with valid content
Failure/Error: click_button "Save"
NameError:
undefined local variable or method todo_item_params' for #<TodoItemsController:0x007f98bcfef888>
# ./app/controllers/todo_items_controller.rb:31:in
update'
# ./spec/features/todo_items/edit_spec.rb:26:in `block (2 levels) in <top (required)>'
so i have gone back to the two .rb files listed in the error and looked at the lines above and below them but to no avail, I have referenced the video found here:
and everything matches up. so I'm not sure where I'm going wrong.
i have included the two files the error references for your review, i have been after this error for a few days now and just cant figure it out.
CODE
error # 1 # ./app/controllers/todo_items_controller.rb:31:in `update'
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_param)
if @todo_item.save
flash[:success] = "Item added to your todo list"
redirect_to todo_list_todo_items_path
else
flash[:error] = "There was a problem adding your item to your list"
render action: :new
end
end
def edit
@todo_list = TodoList.find(params[:todo_list_id])
@todo_item = @todo_list.todo_items.find(params[:id])
end
def update
@todo_list = TodoList.find(params[:todo_list_id])
@todo_item = @todo_list.todo_items.find(params[:id])
if @todo_item.update_attributes(todo_item_params)
flash[:success] = "Saved todo list item"
redirect_to todo_list_todo_items_path
else
flash[:error] = "That todo item could not be saved."
render action: :edit
end
end
def url_options
{ todo_list_id: params[:todo_list_id] }.merge(super)
end
private
def todo_item_param
params[:todo_item].permit(:content)
end
end
error # 2 # ./spec/features/todo_items/edit_spec.rb:26:in `block (2 levels) in <top (required)>'
require 'spec_helper'
describe "Editing todo items" do
# START todo_list object
let!(:todo_list){ TodoList.create(title: "Grocery list", description: "Groceries") }
# END todo_list object
# START 2nd todo_item object
let!(:todo_item){ todo_list.todo_items.create(content: "Milk") }
# END 2nd todo_item object
# START visit_todo method
def visit_todo(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(todo_list)
within("#todo_item_#{todo_item.id}") do
click_link "Edit"
end
fill_in "Content", with: "Lots of Milk"
click_button "Save"
expect(page).to have_content("Saved todo list item.")
todo_item.reload
expect(todo_item.content).to eq("Lots of Milk")
end
end
let me know if you will require anything else and ill add it for your review.
thanks for all your input!
1 Answer

Maciej Czuchnowski
36,441 PointsIt's always the little things :)
Your controller has this line:
def todo_item_param
Notice that it says "param", singular. And your update action uses plural:
if @todo_item.update_attributes(todo_item_params)
The test runs the update action, encounters this "todo_item_params" and has no idea what it is. Hence "undefined local variable or method todo_item_params". Hope this helps.
Shawn Wilson
iOS Development Techdegree Student 7,049 PointsShawn Wilson
iOS Development Techdegree Student 7,049 PointsAhh I see, that makes complete sense! Thanks for the assist!